The function strptime() has a major portability problem. POSIX <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strptime.html> says: "It is unspecified whether multiple calls to strptime() using the same tm structure will update the current contents of the structure or overwrite all contents of the structure. Conforming applications should make a single call to strptime() with a format and all data needed to completely specify the date and time being converted."
The glibc documentation <https://sourceware.org/glibc/manual/2.44/html_node/Low_002dLevel-Time-String-Parsing.html> says it like this: "The specification of the function in the XPG standard is rather vague, leaving out a few important pieces of information. Most importantly, it does not specify what happens to those elements of tm which are not directly initialized by the different formats. The implementations on different Unix systems vary here." The systems which don't support partial format strings are: Solaris. The workaround is to compile and use the GNU implementation of strptime on such systems. The new module 'strptime-gnu' does this. The old module 'strptime' is renamed to 'strptime-posix'. Different applications have different needs: - oath-toolkit and wget need only strptime-posix. - gawk and octave can make use of strptime-gnu. - clisp needs strptime-gnu. 2026-11-23 Bruno Haible <[email protected]> strptime-gnu: Add tests. * tests/test-strptime-gnu.c: New file. * modules/strptime-gnu-tests: New file. strptime-gnu: New module. * m4/time_h.m4 (gl_TIME_H_DEFAULTS): Initialize REPLACE_STRPTIME. * m4/strptime.m4 (gl_FUNC_STRPTIME_POSIX): Define through AC_DEFUN_ONCE. (gl_FUNC_STRPTIME_GNU): New macro. * modules/time-h (Makefile.am): Substitute REPLACE_STRPTIME. * lib/time.in.h (strptime): Consider REPLACE_STRPTIME. * modules/strptime-posix (Depends-on, configure.ac): Test also REPLACE_STRPTIME. * modules/strptime-gnu: New file. * doc/posix-functions/strptime.texi: Mention the new module. 2026-11-23 Bruno Haible <[email protected]> strptime-posix: Add tests. * tests/test-strptime-posix.c: New file. * modules/strptime-posix-tests: New file. 2026-11-23 Bruno Haible <[email protected]> strptime-posix: Rename from strptime. * modules/strptime-posix: Renamed from modules/strptime. * modules/strptime: Now just a deprecated indirection to module 'strptime-posix'. * m4/strptime.m4 (gl_FUNC_STRPTIME_POSIX): Renamed from gl_FUNC_STRPTIME. * lib/time.in.h (strptime): Update. * m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS): Update. * modules/time-h (Makefile.am): Update. * doc/posix-functions/strptime.texi: Mention module 'strptime-posix' instead of 'strptime'. * MODULES.html.sh (func_all_modules): Update.
>From 9e527ddbc498f2aed954a403e6f67d7d9472d933 Mon Sep 17 00:00:00 2001 From: Bruno Haible <[email protected]> Date: Mon, 23 Nov 2026 08:51:28 +0100 Subject: [PATCH 1/4] strptime-posix: Rename from strptime. * modules/strptime-posix: Renamed from modules/strptime. * modules/strptime: Now just a deprecated indirection to module 'strptime-posix'. * m4/strptime.m4 (gl_FUNC_STRPTIME_POSIX): Renamed from gl_FUNC_STRPTIME. * lib/time.in.h (strptime): Update. * m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS): Update. * modules/time-h (Makefile.am): Update. * doc/posix-functions/strptime.texi: Mention module 'strptime-posix' instead of 'strptime'. * MODULES.html.sh (func_all_modules): Update. --- ChangeLog | 15 ++++++++++++ MODULES.html.sh | 2 +- doc/posix-functions/strptime.texi | 4 ++-- lib/time.in.h | 2 +- m4/strptime.m4 | 4 ++-- m4/time_h.m4 | 4 ++-- modules/strptime | 27 ++++++--------------- modules/strptime-posix | 39 +++++++++++++++++++++++++++++++ modules/time-h | 2 +- 9 files changed, 70 insertions(+), 29 deletions(-) create mode 100644 modules/strptime-posix diff --git a/ChangeLog b/ChangeLog index 50b324be40..275b1b9767 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2026-11-23 Bruno Haible <[email protected]> + + strptime-posix: Rename from strptime. + * modules/strptime-posix: Renamed from modules/strptime. + * modules/strptime: Now just a deprecated indirection to module + 'strptime-posix'. + * m4/strptime.m4 (gl_FUNC_STRPTIME_POSIX): Renamed from + gl_FUNC_STRPTIME. + * lib/time.in.h (strptime): Update. + * m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS): Update. + * modules/time-h (Makefile.am): Update. + * doc/posix-functions/strptime.texi: Mention module 'strptime-posix' + instead of 'strptime'. + * MODULES.html.sh (func_all_modules): Update. + 2026-11-22 Bruno Haible <[email protected]> getdate: Document portability problems. diff --git a/MODULES.html.sh b/MODULES.html.sh index 6cb0e2c5a0..dd741146e5 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -2675,7 +2675,7 @@ func_all_modules () func_module sigprocmask func_module socklen func_module ssize_t - func_module strptime + func_module strptime-posix func_module strtok_r func_module sys_select-h func_module sys_socket-h diff --git a/doc/posix-functions/strptime.texi b/doc/posix-functions/strptime.texi index 57af508032..9e65e71ff0 100644 --- a/doc/posix-functions/strptime.texi +++ b/doc/posix-functions/strptime.texi @@ -6,8 +6,8 @@ LSB specification:@* @url{https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/baselib-strptime-3.html} -Gnulib module: strptime -@mindex strptime +Gnulib module: strptime-posix +@mindex strptime-posix Portability problems fixed by Gnulib: @itemize diff --git a/lib/time.in.h b/lib/time.in.h index 2f470dd4cc..17a3bb2b4f 100644 --- a/lib/time.in.h +++ b/lib/time.in.h @@ -411,7 +411,7 @@ _GL_CXXALIASWARN (gmtime); /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store the resulting broken-down time into TM. See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>. */ -# if @GNULIB_STRPTIME@ +# if @GNULIB_STRPTIME_POSIX@ # if ! @HAVE_STRPTIME@ _GL_FUNCDECL_SYS (strptime, char *, (char const *restrict __buf, char const *restrict __format, diff --git a/m4/strptime.m4 b/m4/strptime.m4 index 3d18126428..757834b6dc 100644 --- a/m4/strptime.m4 +++ b/m4/strptime.m4 @@ -1,12 +1,12 @@ # strptime.m4 -# serial 8 +# serial 9 dnl Copyright (C) 2007, 2009-2026 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. dnl This file is offered as-is, without any warranty. -AC_DEFUN([gl_FUNC_STRPTIME], +AC_DEFUN([gl_FUNC_STRPTIME_POSIX], [ AC_REQUIRE([gl_TIME_H_DEFAULTS]) AC_REQUIRE([AC_C_RESTRICT]) diff --git a/m4/time_h.m4 b/m4/time_h.m4 index 8d896ea526..0beb9ffb4b 100644 --- a/m4/time_h.m4 +++ b/m4/time_h.m4 @@ -1,5 +1,5 @@ # time_h.m4 -# serial 27 +# serial 28 dnl Copyright (C) 2000-2001, 2003-2007, 2009-2026 Free Software Foundation, dnl Inc. dnl This file is free software; the Free Software Foundation @@ -140,7 +140,7 @@ AC_DEFUN([gl_TIME_H_REQUIRE_DEFAULTS] gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_LOCALTIME]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_NANOSLEEP]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRFTIME]) - gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRPTIME]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRPTIME_POSIX]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIME]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIMEGM]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TIMESPEC_GET]) diff --git a/modules/strptime b/modules/strptime index 67296f76c8..58dc609e11 100644 --- a/modules/strptime +++ b/modules/strptime @@ -1,32 +1,20 @@ Description: Convert a string representation of time to a tm structure. +Status: +deprecated + +Notice: +This module is deprecated. Use the module 'strptime-posix' instead. + Files: -lib/strptime.c -m4/strptime.m4 -m4/tm_gmtoff.m4 Depends-on: -time-h -extensions -sys_time-h [test $HAVE_STRPTIME = 0] -string-h [test $HAVE_STRPTIME = 0] -strncasecmp [test $HAVE_STRPTIME = 0] -bool [test $HAVE_STRPTIME = 0] -time_r [test $HAVE_STRPTIME = 0] +strptime-posix configure.ac: -gl_FUNC_STRPTIME -gl_CONDITIONAL([GL_COND_OBJ_STRPTIME], [test $HAVE_STRPTIME = 0]) -AM_COND_IF([GL_COND_OBJ_STRPTIME], [ - gl_PREREQ_STRPTIME -]) -gl_TIME_MODULE_INDICATOR([strptime]) Makefile.am: -if GL_COND_OBJ_STRPTIME -lib_SOURCES += strptime.c -endif Include: <time.h> @@ -35,4 +23,3 @@ License: LGPLv2+ Maintainer: -glibc diff --git a/modules/strptime-posix b/modules/strptime-posix new file mode 100644 index 0000000000..142fb2d865 --- /dev/null +++ b/modules/strptime-posix @@ -0,0 +1,39 @@ +Description: +Convert a string representation of time to a tm structure, +using a format string that specifles all 6 essential tm fields. + +Files: +lib/strptime.c +m4/strptime.m4 +m4/tm_gmtoff.m4 + +Depends-on: +time-h +extensions +sys_time-h [test $HAVE_STRPTIME = 0] +string-h [test $HAVE_STRPTIME = 0] +strncasecmp [test $HAVE_STRPTIME = 0] +bool [test $HAVE_STRPTIME = 0] +time_r [test $HAVE_STRPTIME = 0] + +configure.ac: +gl_FUNC_STRPTIME_POSIX +gl_CONDITIONAL([GL_COND_OBJ_STRPTIME], [test $HAVE_STRPTIME = 0]) +AM_COND_IF([GL_COND_OBJ_STRPTIME], [ + gl_PREREQ_STRPTIME +]) +gl_TIME_MODULE_INDICATOR([strptime-posix]) + +Makefile.am: +if GL_COND_OBJ_STRPTIME +lib_SOURCES += strptime.c +endif + +Include: +<time.h> + +License: +LGPLv2+ + +Maintainer: +glibc diff --git a/modules/time-h b/modules/time-h index b3affe3433..f71d3c6f75 100644 --- a/modules/time-h +++ b/modules/time-h @@ -37,7 +37,7 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's/@''GNULIB_MKTIME''@/$(GNULIB_MKTIME)/g' \ -e 's/@''GNULIB_NANOSLEEP''@/$(GNULIB_NANOSLEEP)/g' \ -e 's/@''GNULIB_STRFTIME''@/$(GNULIB_STRFTIME)/g' \ - -e 's/@''GNULIB_STRPTIME''@/$(GNULIB_STRPTIME)/g' \ + -e 's/@''GNULIB_STRPTIME_POSIX''@/$(GNULIB_STRPTIME_POSIX)/g' \ -e 's/@''GNULIB_TIME''@/$(GNULIB_TIME)/g' \ -e 's/@''GNULIB_TIMEGM''@/$(GNULIB_TIMEGM)/g' \ -e 's/@''GNULIB_TIMESPEC_GET''@/$(GNULIB_TIMESPEC_GET)/g' \ -- 2.53.0
>From b235f97159397f82d880100490e3b1a7fc96489b Mon Sep 17 00:00:00 2001 From: Bruno Haible <[email protected]> Date: Mon, 23 Nov 2026 12:12:36 +0100 Subject: [PATCH 2/4] strptime-posix: Add tests. * tests/test-strptime-posix.c: New file. * modules/strptime-posix-tests: New file. --- ChangeLog | 6 ++++ modules/strptime-posix-tests | 12 +++++++ tests/test-strptime-posix.c | 61 ++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 modules/strptime-posix-tests create mode 100644 tests/test-strptime-posix.c diff --git a/ChangeLog b/ChangeLog index 275b1b9767..661c306661 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2026-11-23 Bruno Haible <[email protected]> + + strptime-posix: Add tests. + * tests/test-strptime-posix.c: New file. + * modules/strptime-posix-tests: New file. + 2026-11-23 Bruno Haible <[email protected]> strptime-posix: Rename from strptime. diff --git a/modules/strptime-posix-tests b/modules/strptime-posix-tests new file mode 100644 index 0000000000..362369ad05 --- /dev/null +++ b/modules/strptime-posix-tests @@ -0,0 +1,12 @@ +Files: +tests/test-strptime-posix.c +tests/signature.h +tests/macros.h + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-strptime-posix +check_PROGRAMS += test-strptime-posix diff --git a/tests/test-strptime-posix.c b/tests/test-strptime-posix.c new file mode 100644 index 0000000000..14190464d1 --- /dev/null +++ b/tests/test-strptime-posix.c @@ -0,0 +1,61 @@ +/* Test of conversion of a time string using a complete format string. + Copyright (C) 2026 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <[email protected]>, 2026. */ + +#include <config.h> + +/* Specification. */ +#include <time.h> + +#include "signature.h" +SIGNATURE_CHECK (strptime, char *, (const char *, const char *, struct tm *)); + +#include <string.h> + +#include "macros.h" + +/* POSIX + <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strptime.html> + says: + "It is unspecified whether multiple calls to strptime() using the same tm + structure will update the current contents of the structure or overwrite + all contents of the structure." + All implementations except Solaris update the contents of the structure. + Only Solaris overwrites all contents of the structure. + And then: + "Conforming applications should make a single call to strptime() with a + format and all data needed to completely specify the date and time being + converted." + This is what we test here. */ + +int +main () +{ + struct tm t; + memset (&t, '\0', sizeof (t)); + t.tm_isdst = -1; + const char *end = + strptime ("Friday September 18, 1987, 10:29:30", "%A %B %d, %Y, %H:%M:%S", + &t); + ASSERT (end != NULL && *end == '\0'); + ASSERT (t.tm_year == 1987 - 1900 + && t.tm_mon == 9 - 1 + && t.tm_mday == 18 + && t.tm_hour == 10 && t.tm_min == 29 && t.tm_sec == 30); + + return test_exit_status; +} -- 2.53.0
>From 151d73cf18049797973cf36472e47b74258a6caf Mon Sep 17 00:00:00 2001 From: Bruno Haible <[email protected]> Date: Mon, 23 Nov 2026 12:58:35 +0100 Subject: [PATCH 3/4] strptime-gnu: New module. * m4/time_h.m4 (gl_TIME_H_DEFAULTS): Initialize REPLACE_STRPTIME. * m4/strptime.m4 (gl_FUNC_STRPTIME_POSIX): Define through AC_DEFUN_ONCE. (gl_FUNC_STRPTIME_GNU): New macro. * modules/time-h (Makefile.am): Substitute REPLACE_STRPTIME. * lib/time.in.h (strptime): Consider REPLACE_STRPTIME. * modules/strptime-posix (Depends-on, configure.ac): Test also REPLACE_STRPTIME. * modules/strptime-gnu: New file. * doc/posix-functions/strptime.texi: Mention the new module. --- ChangeLog | 13 +++++++ doc/posix-functions/strptime.texi | 14 ++++++- lib/time.in.h | 19 +++++++++- m4/strptime.m4 | 61 ++++++++++++++++++++++++++++++- m4/time_h.m4 | 3 +- modules/strptime-gnu | 22 +++++++++++ modules/strptime-posix | 13 ++++--- modules/time-h | 1 + 8 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 modules/strptime-gnu diff --git a/ChangeLog b/ChangeLog index 661c306661..4326e1404c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2026-11-23 Bruno Haible <[email protected]> + + strptime-gnu: New module. + * m4/time_h.m4 (gl_TIME_H_DEFAULTS): Initialize REPLACE_STRPTIME. + * m4/strptime.m4 (gl_FUNC_STRPTIME_POSIX): Define through AC_DEFUN_ONCE. + (gl_FUNC_STRPTIME_GNU): New macro. + * modules/time-h (Makefile.am): Substitute REPLACE_STRPTIME. + * lib/time.in.h (strptime): Consider REPLACE_STRPTIME. + * modules/strptime-posix (Depends-on, configure.ac): Test also + REPLACE_STRPTIME. + * modules/strptime-gnu: New file. + * doc/posix-functions/strptime.texi: Mention the new module. + 2026-11-23 Bruno Haible <[email protected]> strptime-posix: Add tests. diff --git a/doc/posix-functions/strptime.texi b/doc/posix-functions/strptime.texi index 9e65e71ff0..e6cec9fda4 100644 --- a/doc/posix-functions/strptime.texi +++ b/doc/posix-functions/strptime.texi @@ -6,16 +6,26 @@ LSB specification:@* @url{https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/baselib-strptime-3.html} -Gnulib module: strptime-posix +Gnulib module: strptime-posix or strptime-gnu @mindex strptime-posix +@mindex strptime-gnu -Portability problems fixed by Gnulib: +Portability problems fixed by either Gnulib module @code{strptime-posix} or @code{strptime-gnu}: @itemize @item This function is missing on some platforms: mingw, MSVC 14. @end itemize +Portability problems fixed by Gnulib module @code{strptime-gnu}: +@itemize +@item +This function does not support format strings +that specify fewer than the 6 essential @code{tm} fields, +as it initializes unspecified fields to zero, on some platforms: +Solaris 11. +@end itemize + Portability problems not fixed by Gnulib: @itemize @end itemize diff --git a/lib/time.in.h b/lib/time.in.h index 17a3bb2b4f..b0c9e77f10 100644 --- a/lib/time.in.h +++ b/lib/time.in.h @@ -412,16 +412,31 @@ _GL_CXXALIASWARN (gmtime); the resulting broken-down time into TM. See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>. */ # if @GNULIB_STRPTIME_POSIX@ -# if ! @HAVE_STRPTIME@ +# if @REPLACE_STRPTIME@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# define strptime rpl_strptime +# endif +_GL_FUNCDECL_RPL (strptime, char *, (char const *restrict __buf, + char const *restrict __format, + struct tm *restrict __tm), + _GL_ARG_NONNULL ((1, 2, 3))); +_GL_CXXALIAS_RPL (strptime, char *, (char const *restrict __buf, + char const *restrict __format, + struct tm *restrict __tm)); +# else +# if ! @HAVE_STRPTIME@ _GL_FUNCDECL_SYS (strptime, char *, (char const *restrict __buf, char const *restrict __format, struct tm *restrict __tm), _GL_ARG_NONNULL ((1, 2, 3))); -# endif +# endif _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf, char const *restrict __format, struct tm *restrict __tm)); +# endif +# if __GLIBC__ >= 2 _GL_CXXALIASWARN (strptime); +# endif # elif defined GNULIB_POSIXCHECK # if HAVE_RAW_DECL_STRPTIME _GL_WARN_ON_USE (strptime, "strptime is unportable - " diff --git a/m4/strptime.m4 b/m4/strptime.m4 index 757834b6dc..b0c2d5bfc2 100644 --- a/m4/strptime.m4 +++ b/m4/strptime.m4 @@ -1,12 +1,12 @@ # strptime.m4 -# serial 9 +# serial 10 dnl Copyright (C) 2007, 2009-2026 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. dnl This file is offered as-is, without any warranty. -AC_DEFUN([gl_FUNC_STRPTIME_POSIX], +AC_DEFUN_ONCE([gl_FUNC_STRPTIME_POSIX], [ AC_REQUIRE([gl_TIME_H_DEFAULTS]) AC_REQUIRE([AC_C_RESTRICT]) @@ -16,6 +16,63 @@ AC_DEFUN([gl_FUNC_STRPTIME_POSIX] fi ]) +AC_DEFUN([gl_FUNC_STRPTIME_GNU], +[ + AC_REQUIRE([AC_CANONICAL_HOST]) + dnl Persuade glibc and Solaris <time.h> to declare strptime. + AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) + + gl_FUNC_STRPTIME_POSIX + if test $ac_cv_func_strptime = yes; then + AC_CACHE_CHECK([whether strptime works with incomplete format strings], + [gl_cv_func_strptime_gnu], + [AC_RUN_IFELSE( + [AC_LANG_PROGRAM( + [[#include <string.h> + #include <time.h> + ]], + [[int result = 0; + { /* This test fails on Solaris. */ + struct tm t; + memset (&t, '\0', sizeof (t)); + t.tm_year = -9999; t.tm_mon = -9999; t.tm_mday = -9999; + t.tm_hour = -9999; t.tm_min = -9999; t.tm_sec = -9999; + t.tm_isdst = -1; + const char *end = + strptime ("Friday September 18, 1987", "%A %B %d, %Y", &t); + if (!(end != NULL && *end == '\0' + && t.tm_hour == -9999 && t.tm_min == -9999 && t.tm_sec == -9999)) + result |= 1; + } + { /* This test fails on Solaris. */ + struct tm t; + memset (&t, '\0', sizeof (t)); + t.tm_year = -9999; t.tm_mon = -9999; t.tm_mday = -9999; + t.tm_hour = -9999; t.tm_min = -9999; t.tm_sec = -9999; + t.tm_isdst = -1; + const char *end = strptime ("10:29:30", "%H:%M:%S", &t); + if (!(end != NULL && *end == '\0' + && t.tm_year == -9999 && t.tm_mon == -9999 && t.tm_mday == -9999)) + result |= 2; + } + return result; + ]])], + [gl_cv_func_strptime_gnu=yes], + [gl_cv_func_strptime_gnu=no], + [case "$host_os" in + # Guess no on Solaris. + solaris*) gl_cv_func_strptime_gnu="guessing no" ;; + # If we don't know, obey --enable-cross-guesses. + *) gl_cv_func_strptime_gnu="$gl_cross_guess_normal" ;; + esac + ]) + ]) + case "$gl_cv_func_strptime_gnu" in + *no) REPLACE_STRPTIME=1 ;; + esac + fi +]) + # Prerequisites of lib/strptime.c. AC_DEFUN([gl_PREREQ_STRPTIME], [ diff --git a/m4/time_h.m4 b/m4/time_h.m4 index 0beb9ffb4b..b7ff1bf988 100644 --- a/m4/time_h.m4 +++ b/m4/time_h.m4 @@ -1,5 +1,5 @@ # time_h.m4 -# serial 28 +# serial 29 dnl Copyright (C) 2000-2001, 2003-2007, 2009-2026 Free Software Foundation, dnl Inc. dnl This file is free software; the Free Software Foundation @@ -177,6 +177,7 @@ AC_DEFUN([gl_TIME_H_DEFAULTS] REPLACE_MKTIME_Z=0; AC_SUBST([REPLACE_MKTIME_Z]) REPLACE_NANOSLEEP=0; AC_SUBST([REPLACE_NANOSLEEP]) REPLACE_STRFTIME=0; AC_SUBST([REPLACE_STRFTIME]) + REPLACE_STRPTIME=0; AC_SUBST([REPLACE_STRPTIME]) REPLACE_TIME=0; AC_SUBST([REPLACE_TIME]) REPLACE_TIMEGM=0; AC_SUBST([REPLACE_TIMEGM]) REPLACE_TIMESPEC_GET=0; AC_SUBST([REPLACE_TIMESPEC_GET]) diff --git a/modules/strptime-gnu b/modules/strptime-gnu new file mode 100644 index 0000000000..7da90f6ab9 --- /dev/null +++ b/modules/strptime-gnu @@ -0,0 +1,22 @@ +Description: +Convert a string representation of time to a tm structure, +using a format string that specifles some tm fields. + +Files: + +Depends-on: +strptime-posix + +configure.ac: +gl_FUNC_STRPTIME_GNU + +Makefile.am: + +Include: +<time.h> + +License: +LGPLv2+ + +Maintainer: +all diff --git a/modules/strptime-posix b/modules/strptime-posix index 142fb2d865..3112c7583a 100644 --- a/modules/strptime-posix +++ b/modules/strptime-posix @@ -10,15 +10,16 @@ m4/tm_gmtoff.m4 Depends-on: time-h extensions -sys_time-h [test $HAVE_STRPTIME = 0] -string-h [test $HAVE_STRPTIME = 0] -strncasecmp [test $HAVE_STRPTIME = 0] -bool [test $HAVE_STRPTIME = 0] -time_r [test $HAVE_STRPTIME = 0] +sys_time-h [test $HAVE_STRPTIME = 0 || test $REPLACE_STRPTIME = 1] +string-h [test $HAVE_STRPTIME = 0 || test $REPLACE_STRPTIME = 1] +strncasecmp [test $HAVE_STRPTIME = 0 || test $REPLACE_STRPTIME = 1] +bool [test $HAVE_STRPTIME = 0 || test $REPLACE_STRPTIME = 1] +time_r [test $HAVE_STRPTIME = 0 || test $REPLACE_STRPTIME = 1] configure.ac: gl_FUNC_STRPTIME_POSIX -gl_CONDITIONAL([GL_COND_OBJ_STRPTIME], [test $HAVE_STRPTIME = 0]) +gl_CONDITIONAL([GL_COND_OBJ_STRPTIME], + [test $HAVE_STRPTIME = 0 || test $REPLACE_STRPTIME = 1]) AM_COND_IF([GL_COND_OBJ_STRPTIME], [ gl_PREREQ_STRPTIME ]) diff --git a/modules/time-h b/modules/time-h index f71d3c6f75..810aa98971 100644 --- a/modules/time-h +++ b/modules/time-h @@ -64,6 +64,7 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''REPLACE_MKTIME_Z''@|$(REPLACE_MKTIME_Z)|g' \ -e 's|@''REPLACE_NANOSLEEP''@|$(REPLACE_NANOSLEEP)|g' \ -e 's|@''REPLACE_STRFTIME''@|$(REPLACE_STRFTIME)|g' \ + -e 's|@''REPLACE_STRPTIME''@|$(REPLACE_STRPTIME)|g' \ -e 's|@''REPLACE_TIME''@|$(REPLACE_TIME)|g' \ -e 's|@''REPLACE_TIMEGM''@|$(REPLACE_TIMEGM)|g' \ -e 's|@''REPLACE_TIMESPEC_GET''@|$(REPLACE_TIMESPEC_GET)|g' \ -- 2.53.0
>From ee22f4005fe9c100484817c0c546816f7da9bcfb Mon Sep 17 00:00:00 2001 From: Bruno Haible <[email protected]> Date: Mon, 23 Nov 2026 12:59:52 +0100 Subject: [PATCH 4/4] strptime-gnu: Add tests. * tests/test-strptime-gnu.c: New file. * modules/strptime-gnu-tests: New file. --- ChangeLog | 4 +++ modules/strptime-gnu-tests | 11 +++++++ tests/test-strptime-gnu.c | 63 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 modules/strptime-gnu-tests create mode 100644 tests/test-strptime-gnu.c diff --git a/ChangeLog b/ChangeLog index 4326e1404c..7ce33444e6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2026-11-23 Bruno Haible <[email protected]> + strptime-gnu: Add tests. + * tests/test-strptime-gnu.c: New file. + * modules/strptime-gnu-tests: New file. + strptime-gnu: New module. * m4/time_h.m4 (gl_TIME_H_DEFAULTS): Initialize REPLACE_STRPTIME. * m4/strptime.m4 (gl_FUNC_STRPTIME_POSIX): Define through AC_DEFUN_ONCE. diff --git a/modules/strptime-gnu-tests b/modules/strptime-gnu-tests new file mode 100644 index 0000000000..d8fe63362d --- /dev/null +++ b/modules/strptime-gnu-tests @@ -0,0 +1,11 @@ +Files: +tests/test-strptime-gnu.c +tests/macros.h + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-strptime-gnu +check_PROGRAMS += test-strptime-gnu diff --git a/tests/test-strptime-gnu.c b/tests/test-strptime-gnu.c new file mode 100644 index 0000000000..f8c74edb9a --- /dev/null +++ b/tests/test-strptime-gnu.c @@ -0,0 +1,63 @@ +/* Test of conversion of a time string using an incomplete format string. + Copyright (C) 2026 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <[email protected]>, 2026. */ + +#include <config.h> + +/* Specification. */ +#include <time.h> + +#include <string.h> + +#include "macros.h" + +/* POSIX + <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strptime.html> + says: + "It is unspecified whether multiple calls to strptime() using the same tm + structure will update the current contents of the structure or overwrite + all contents of the structure." + All implementations except Solaris update the contents of the structure. + Only Solaris overwrites all contents of the structure. + Here we test for the "update the contents" behaviour. */ + +int +main () +{ + { + struct tm t; + memset (&t, '\0', sizeof (t)); + t.tm_year = -9999; t.tm_mon = -9999; t.tm_mday = -9999; + t.tm_hour = -9999; t.tm_min = -9999; t.tm_sec = -9999; + t.tm_isdst = -1; + const char *end = strptime ("Friday September 18, 1987", "%A %B %d, %Y", &t); + ASSERT (end != NULL && *end == '\0'); + ASSERT (t.tm_hour == -9999 && t.tm_min == -9999 && t.tm_sec == -9999); + } + { + struct tm t; + memset (&t, '\0', sizeof (t)); + t.tm_year = -9999; t.tm_mon = -9999; t.tm_mday = -9999; + t.tm_hour = -9999; t.tm_min = -9999; t.tm_sec = -9999; + t.tm_isdst = -1; + const char *end = strptime ("10:29:30", "%H:%M:%S", &t); + ASSERT (end != NULL && *end == '\0'); + ASSERT (t.tm_year == -9999 && t.tm_mon == -9999 && t.tm_mday == -9999); + } + + return test_exit_status; +} -- 2.53.0
