From a89e3449698f97ea0f9c5fbd73aa054009858471 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 23 Jul 2015 18:28:13 -0700 Subject: [PATCH] fprintftime, strftime: use timezone_t args * NEWS: Document the change. * lib/fprintftime.h (fprintftime): * lib/strftime.c (extra_args) [my_strftime]: * lib/strftime.h (nstrftime): Time zone arg is now of type timezone_t, not int. * lib/strftime.c (mktime_z) [_LIBC]: New macro. (__gmtime_r, __localtime_r) [!HAVE_TM_GMTOFF]: Remove; no longer used. (my_strftime) [emacs && !my_strftime]: (emacs_strftimeu) [emacs && !FPRINTFTIME]: Remove; Emacs doesn't need this any more. (HAVE_TZSET) [my_strftime]: Unset, since we no longer want fprintftime and nstrftime to call tzset. (ut) [!my_strftime]: Remove, replacing with ... (tz) [!my_stftime]: ... this new macro. All uses changed. (strftime_case_): Use localtime_rz and mktime_z instead of localtime_r and mktime. * modules/fprintftime (Depends-on): Add time_rz. * modules/strftime (Depends-on): Add time_rz. Remove time_r. * tests/test-strftime.c (main): Adjust to new nstrftime API. --- ChangeLog | 21 ++++++++++++++++++ NEWS | 5 +++++ lib/fprintftime.h | 4 ++-- lib/strftime.c | 51 ++++++++++--------------------------------- lib/strftime.h | 7 +++--- modules/fprintftime | 1 + modules/strftime | 2 +- tests/test-strftime.c | 3 +-- 8 files changed, 46 insertions(+), 48 deletions(-) diff --git a/ChangeLog b/ChangeLog index 49993624c4..0b0fc02ae9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,26 @@ 2015-07-23 Paul Eggert + fprintftime, strftime: use timezone_t args + * NEWS: Document the change. + * lib/fprintftime.h (fprintftime): + * lib/strftime.c (extra_args) [my_strftime]: + * lib/strftime.h (nstrftime): + Time zone arg is now of type timezone_t, not int. + * lib/strftime.c (mktime_z) [_LIBC]: New macro. + (__gmtime_r, __localtime_r) [!HAVE_TM_GMTOFF]: Remove; no longer used. + (my_strftime) [emacs && !my_strftime]: + (emacs_strftimeu) [emacs && !FPRINTFTIME]: + Remove; Emacs doesn't need this any more. + (HAVE_TZSET) [my_strftime]: Unset, since we no longer want + fprintftime and nstrftime to call tzset. + (ut) [!my_strftime]: Remove, replacing with ... + (tz) [!my_stftime]: ... this new macro. All uses changed. + (strftime_case_): Use localtime_rz and mktime_z instead + of localtime_r and mktime. + * modules/fprintftime (Depends-on): Add time_rz. + * modules/strftime (Depends-on): Add time_rz. Remove time_r. + * tests/test-strftime.c (main): Adjust to new nstrftime API. + time_rz: new module * MODULES.html.sh: Add time_rz. * lib/time_rz.c, m4/time_rz.m4, modules/time_rz: New files. diff --git a/NEWS b/NEWS index 1e36206e34..b294edd984 100644 --- a/NEWS +++ b/NEWS @@ -42,6 +42,11 @@ User visible incompatible changes Date Modules Changes +2015-07-24 fprintftime Exported functions' time zone arguments are now of + strftime type timezone_t (with NULL denoting UTC) instead of + type int (with nonzero denoting UTC). These + modules now depend on time_rz. + 2015-04-03 hash hash_insert0 function removed (deprecated in 2011). 2014-10-29 obstack The obstack functions are no longer limited to diff --git a/lib/fprintftime.h b/lib/fprintftime.h index 0034fc8dd5..6f76d16acd 100644 --- a/lib/fprintftime.h +++ b/lib/fprintftime.h @@ -23,7 +23,7 @@ large buffer that nstrftime would require. Output to stream FP the result of formatting (according to the - nstrftime format string, FMT) the time data, *TM, and the UTC + nstrftime format string, FMT) the time data, *TM, and the ZONE and NANOSECONDS values. */ size_t fprintftime (FILE *fp, char const *fmt, struct tm const *tm, - int utc, int nanoseconds); + timezone_t zone, int nanoseconds); diff --git a/lib/strftime.c b/lib/strftime.c index 2426aae705..3cc8477a59 100644 --- a/lib/strftime.c +++ b/lib/strftime.c @@ -121,22 +121,11 @@ extern char *tzname[]; #ifdef _LIBC +# define mktime_z(tz, tm) mktime (tm) # define tzname __tzname # define tzset __tzset #endif -#if !HAVE_TM_GMTOFF -/* Portable standalone applications should supply a "time.h" that - declares a POSIX-compliant localtime_r, for the benefit of older - implementations that lack localtime_r or have a nonstandard one. - See the gnulib time_r module for one way to implement this. */ -# undef __gmtime_r -# undef __localtime_r -# define __gmtime_r gmtime_r -# define __localtime_r localtime_r -#endif - - #ifndef FPRINTFTIME # define FPRINTFTIME 0 #endif @@ -385,12 +374,7 @@ iso_week_days (int yday, int wday) /* When compiling this file, GNU applications can #define my_strftime to a symbol (typically nstrftime) to get an extended strftime with - extra arguments UT and NS. Emacs is a special case for now, but - this Emacs-specific code can be removed once Emacs's config.h - defines my_strftime. */ -#if defined emacs && !defined my_strftime -# define my_strftime nstrftime -#endif + extra arguments TZ and NS. */ #if FPRINTFTIME # undef my_strftime @@ -398,8 +382,9 @@ iso_week_days (int yday, int wday) #endif #ifdef my_strftime -# define extra_args , ut, ns -# define extra_args_spec , int ut, int ns +# undef HAVE_TZSET +# define extra_args , tz, ns +# define extra_args_spec , timezone_t tz, int ns #else # if defined COMPILE_WIDE # define my_strftime wcsftime @@ -411,7 +396,7 @@ iso_week_days (int yday, int wday) # define extra_args # define extra_args_spec /* We don't have this information in general. */ -# define ut 0 +# define tz 1 # define ns 0 #endif @@ -483,7 +468,7 @@ strftime_case_ (bool upcase, STREAM_OR_CHAR_T *s, zone = (const char *) tp->tm_zone; #endif #if HAVE_TZNAME - if (ut) + if (!tz) { if (! (zone && *zone)) zone = "GMT"; @@ -1144,7 +1129,7 @@ strftime_case_ (bool upcase, STREAM_OR_CHAR_T *s, time_t t; ltm = *tp; - t = mktime (<m); + t = mktime_z (tz, <m); /* Generate string value for T using time_t arithmetic; this works even if sizeof (long) < sizeof (time_t). */ @@ -1366,7 +1351,7 @@ strftime_case_ (bool upcase, STREAM_OR_CHAR_T *s, #if HAVE_TM_GMTOFF diff = tp->tm_gmtoff; #else - if (ut) + if (!tz) diff = 0; else { @@ -1375,7 +1360,7 @@ strftime_case_ (bool upcase, STREAM_OR_CHAR_T *s, time_t lt; ltm = *tp; - lt = mktime (<m); + lt = mktime_z (tz, <m); if (lt == (time_t) -1) { @@ -1384,7 +1369,7 @@ strftime_case_ (bool upcase, STREAM_OR_CHAR_T *s, occurred. */ struct tm tm; - if (! __localtime_r (<, &tm) + if (! localtime_rz (tz, <, &tm) || ((ltm.tm_sec ^ tm.tm_sec) | (ltm.tm_min ^ tm.tm_min) | (ltm.tm_hour ^ tm.tm_hour) @@ -1394,7 +1379,7 @@ strftime_case_ (bool upcase, STREAM_OR_CHAR_T *s, break; } - if (! __gmtime_r (<, >m)) + if (! localtime_rz (0, <, >m)) break; diff = tm_diff (<m, >m); @@ -1473,15 +1458,3 @@ my_strftime (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize) #if defined _LIBC && ! FPRINTFTIME libc_hidden_def (my_strftime) #endif - - -#if defined emacs && ! FPRINTFTIME -/* For Emacs we have a separate interface which corresponds to the normal - strftime function plus the ut argument, but without the ns argument. */ -size_t -emacs_strftimeu (char *s, size_t maxsize, const char *format, - const struct tm *tp, int ut) -{ - return my_strftime (s, maxsize, format, tp, ut, 0); -} -#endif diff --git a/lib/strftime.h b/lib/strftime.h index 3967afc494..2ce6cc5768 100644 --- a/lib/strftime.h +++ b/lib/strftime.h @@ -23,11 +23,10 @@ extern "C" { /* Just like strftime, but with two more arguments: POSIX requires that strftime use the local timezone information. - When __UTC is nonzero and tm->tm_zone is NULL or the empty string, - use UTC instead. Use __NS as the number of nanoseconds in the - %N directive. */ + Use the timezone __TZ instead. Use __NS as the number of + nanoseconds in the %N directive. */ size_t nstrftime (char *, size_t, char const *, struct tm const *, - int __utc, int __ns); + timezone_t __tz, int __ns); #ifdef __cplusplus } diff --git a/modules/fprintftime b/modules/fprintftime index f1c2b8ad76..4067bc3acd 100644 --- a/modules/fprintftime +++ b/modules/fprintftime @@ -8,6 +8,7 @@ lib/fprintftime.c Depends-on: stdio strftime +time_rz configure.ac: diff --git a/modules/strftime b/modules/strftime index 451e565772..b54f44eda0 100644 --- a/modules/strftime +++ b/modules/strftime @@ -10,7 +10,7 @@ m4/strftime.m4 Depends-on: extensions stdbool -time_r +time_rz configure.ac: gl_FUNC_GNU_STRFTIME diff --git a/tests/test-strftime.c b/tests/test-strftime.c index c98ad64161..9ce6096a23 100644 --- a/tests/test-strftime.c +++ b/tests/test-strftime.c @@ -54,11 +54,10 @@ main (void) time_t t = T[i].in; struct tm *tm = gmtime (&t); size_t n; - int utc = 1; ASSERT (tm); - n = nstrftime (buf, sizeof buf, T[i].fmt, tm, utc, T[i].in_ns); + n = nstrftime (buf, sizeof buf, T[i].fmt, tm, 0, T[i].in_ns); if (n == 0) { fail = 1; -- 2.39.5