From ab0fa4e4ba0b2b93a9ef007842523d8d5f9eb758 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 3 May 2012 13:04:29 -0700 Subject: [PATCH] Do not limit current-time-string to years 1000..9999. * src/editfns.c (TM_YEAR_IN_ASCTIME_RANGE): Remove. (Fcurrent_time_string): Support any year that is supported by the underlying localtime representation. Don't use asctime, as it has undefined behavior for years outside the range -999..9999. * doc/lispref/os.texi (Time of Day): Do not limit current-time-string to years 1000..9999. * etc/NEWS: Do not limit current-time-string to years 1000..9999. --- doc/lispref/ChangeLog | 5 +++++ doc/lispref/os.texi | 13 ++++++++----- etc/ChangeLog | 4 ++++ etc/NEWS | 6 ++++++ src/ChangeLog | 8 ++++++++ src/editfns.c | 39 ++++++++++++++++++++------------------- 6 files changed, 51 insertions(+), 24 deletions(-) diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index c3eadfc8558..3be41afe975 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog @@ -1,3 +1,8 @@ +2012-05-03 Paul Eggert + + * os.texi (Time of Day): Do not limit current-time-string + to years 1000..9999. + 2012-05-02 Chong Yidong * display.texi (Font Lookup): diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi index d825a3f18c4..0fdb3e20694 100644 --- a/doc/lispref/os.texi +++ b/doc/lispref/os.texi @@ -1196,11 +1196,14 @@ sections. @defun current-time-string &optional time-value This function returns the current time and date as a human-readable -string. The format of the string is unvarying; the number of -characters used for each part is always the same, so you can reliably -use @code{substring} to extract pieces of it. You should count +string. The format does not vary for the initial part of the string, +which contains the day of week, month, day of month, and time of day +in that order: the number of characters used for these fields is +always the same, so you can reliably +use @code{substring} to extract them. You should count characters from the beginning of the string rather than from the end, -as additional information may some day be added at the end. +as the year might not have exactly four digits, and additional +information may some day be added at the end. The argument @var{time-value}, if given, specifies a time to format (represented as a list of integers), instead of the current time. @@ -2301,7 +2304,7 @@ the notification never expires. Default value is -1. @item :urgency @var{urgency} The urgency level. It can be @code{low}, @code{normal}, or @code{critical}. -@item :action-items +@item :action-items When this keyword is given, the @var{title} string of the actions is interpreted as icon name. diff --git a/etc/ChangeLog b/etc/ChangeLog index f34e5d6d688..2a6cd719220 100644 --- a/etc/ChangeLog +++ b/etc/ChangeLog @@ -1,3 +1,7 @@ +2012-05-03 Paul Eggert + + * NEWS: Do not limit current-time-string to years 1000..9999. + 2012-04-27 Jambunathan K * org/OrgOdtStyles.xml (OrgDescriptionList): Modify style. With diff --git a/etc/NEWS b/etc/NEWS index cd15273c3db..a9e4a7832ed 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -197,6 +197,12 @@ in the presence of quoting, such as file completion in shell buffers. *** New function `completion-table-subvert' to use an existing completion table, but with a different prefix. + +** Time + +*** `current-time-string' no longer requires that its argument's year +must be in the range 1000..9999. It now works with any year supported +by the underlying C implementation. * Changes in Emacs 24.2 on non-free operating systems diff --git a/src/ChangeLog b/src/ChangeLog index bf297616e82..2c2902e937a 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2012-05-03 Paul Eggert + + Do not limit current-time-string to years 1000..9999. + * editfns.c (TM_YEAR_IN_ASCTIME_RANGE): Remove. + (Fcurrent_time_string): Support any year that is supported by the + underlying localtime representation. Don't use asctime, as it + has undefined behavior for years outside the range -999..9999. + 2012-05-02 Paul Eggert Fix race conditions involving setenv, gmtime, localtime, asctime. diff --git a/src/editfns.c b/src/editfns.c index b52bc0c2a99..d266ca9951d 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -73,13 +73,6 @@ extern char **environ; #define TM_YEAR_BASE 1900 -/* Nonzero if TM_YEAR is a struct tm's tm_year value that causes - asctime to have well-defined behavior. */ -#ifndef TM_YEAR_IN_ASCTIME_RANGE -# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \ - (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE) -#endif - #ifdef WINDOWSNT extern Lisp_Object w32_get_internal_run_time (void); #endif @@ -1943,29 +1936,37 @@ but this is considered obsolete. */) { time_t value; struct tm *tm; - char *tem = NULL; - char buf[sizeof "Mon Apr 30 12:49:17 2012" - 1]; + char buf[sizeof "Mon Apr 30 12:49:17 " + INT_STRLEN_BOUND (int) + 1]; + int len IF_LINT (= 0); if (! lisp_time_argument (specified_time, &value, NULL)) error ("Invalid time specification"); - /* Convert to a string, checking for out-of-range time stamps. - Omit the trailing newline. - Don't use 'ctime', as that might dump core if VALUE is out of - range. */ + /* Convert to a string in ctime format, except without the trailing + newline, and without the 4-digit year limit. Don't use asctime + or ctime, as they might dump core if the year is outside the + range -999 .. 9999. */ BLOCK_INPUT; tm = localtime (&value); - if (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)) + if (tm) { - tem = asctime (tm); - if (tem) - memcpy (buf, tem, sizeof buf); + static char const wday_name[][4] = + { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; + static char const mon_name[][4] = + { "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + printmax_t year_base = TM_YEAR_BASE; + + len = sprintf (buf, "%s %s%3d %02d:%02d:%02d %"pMd, + wday_name[tm->tm_wday], mon_name[tm->tm_mon], tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec, + tm->tm_year + year_base); } UNBLOCK_INPUT; - if (! tem) + if (! tm) time_overflow (); - return make_unibyte_string (buf, sizeof buf); + return make_unibyte_string (buf, len); } /* Yield A - B, measured in seconds. -- 2.39.2