This new module implements the POSIX function getdate(). It's an odd API, because it relies on an environment variable that points to a file. But GNU clisp needs it.
2026-11-23 Bruno Haible <[email protected]> getdate: Add tests. * tests/test-getdate.c: New file. * modules/getdate-tests: New file. getdate: New module. * lib/time.in.h (getdate, _gl_getdate_location): New declarations. (getdate_err): New macro. * lib/getdate.c: New file, based on glibc/time/getdate.c. * m4/getdate.m4: New file. * m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS): Initialize GNULIB_GETDATE. (gl_TIME_H_DEFAULTS): Initialize HAVE_GETDATE, REPLACE_GETDATE. * modules/time-h (Makefile.am): Substitute GNULIB_GETDATE, HAVE_GETDATE, REPLACE_GETDATE. * modules/getdate: New file. * config/srclist.txt: Mention getdate.c. * doc/posix-functions/getdate.texi: Mention the new module.
From 5391f5b4676a2eed9c53e478e9447843d4b139a4 Mon Sep 17 00:00:00 2001 From: Bruno Haible <[email protected]> Date: Mon, 23 Nov 2026 16:59:31 +0100 Subject: [PATCH 1/2] getdate: New module. * lib/time.in.h (getdate, _gl_getdate_location): New declarations. (getdate_err): New macro. * lib/getdate.c: New file, based on glibc/time/getdate.c. * m4/getdate.m4: New file. * m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS): Initialize GNULIB_GETDATE. (gl_TIME_H_DEFAULTS): Initialize HAVE_GETDATE, REPLACE_GETDATE. * modules/time-h (Makefile.am): Substitute GNULIB_GETDATE, HAVE_GETDATE, REPLACE_GETDATE. * modules/getdate: New file. * config/srclist.txt: Mention getdate.c. * doc/posix-functions/getdate.texi: Mention the new module. --- ChangeLog | 15 ++ config/srclist.txt | 1 + doc/posix-functions/getdate.texi | 11 +- lib/getdate.c | 346 +++++++++++++++++++++++++++++++ lib/time.in.h | 38 ++++ m4/getdate.m4 | 107 ++++++++++ m4/time_h.m4 | 5 +- modules/getdate | 42 ++++ modules/time-h | 3 + 9 files changed, 562 insertions(+), 6 deletions(-) create mode 100644 lib/getdate.c create mode 100644 m4/getdate.m4 create mode 100644 modules/getdate diff --git a/ChangeLog b/ChangeLog index 81d02b0931..f19edf93f6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2026-11-23 Bruno Haible <[email protected]> + + getdate: New module. + * lib/time.in.h (getdate, _gl_getdate_location): New declarations. + (getdate_err): New macro. + * lib/getdate.c: New file, based on glibc/time/getdate.c. + * m4/getdate.m4: New file. + * m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS): Initialize GNULIB_GETDATE. + (gl_TIME_H_DEFAULTS): Initialize HAVE_GETDATE, REPLACE_GETDATE. + * modules/time-h (Makefile.am): Substitute GNULIB_GETDATE, HAVE_GETDATE, + REPLACE_GETDATE. + * modules/getdate: New file. + * config/srclist.txt: Mention getdate.c. + * doc/posix-functions/getdate.texi: Mention the new module. + 2026-11-23 Pádraig Brady <[email protected]> fcntl-h: Provide a HAVE_WORKING_FSTAT_O_PATH config define diff --git a/config/srclist.txt b/config/srclist.txt index a19a8b78ba..735e896303 100644 --- a/config/srclist.txt +++ b/config/srclist.txt @@ -93,6 +93,7 @@ $LIBCSRC sysdeps/generic/min-eloop-threshold.h lib #$LIBCSRC time/timegm.c lib #$LIBCSRC time/mktime.c lib #$LIBCSRC time/mktime-internal.h lib +#$LIBCSRC time/getdate.c lib # # All below here commented out in forlorn hope of future syncs. diff --git a/doc/posix-functions/getdate.texi b/doc/posix-functions/getdate.texi index 30a9c6d6b8..24e81cba27 100644 --- a/doc/posix-functions/getdate.texi +++ b/doc/posix-functions/getdate.texi @@ -4,14 +4,11 @@ POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9799919799/functions/getdate.html} -Gnulib module: --- +Gnulib module: getdate +@mindex getdate Portability problems fixed by Gnulib: @itemize -@end itemize - -Portability problems not fixed by Gnulib: -@itemize @item This function is missing on some platforms: FreeBSD 15.1, NetBSD 5.0, OpenBSD 7.9, Minix 3.1.8, Cygwin 3.6.x, mingw, MSVC 14, Android 9.0. @@ -30,6 +27,10 @@ Solaris 11 OmniOS. @end itemize +Portability problems not fixed by Gnulib: +@itemize +@end itemize + @mindex parse-datetime Gnulib provides a module @code{parse-datetime} that contains a function @code{parse_datetime} diff --git a/lib/getdate.c b/lib/getdate.c new file mode 100644 index 0000000000..f909f55930 --- /dev/null +++ b/lib/getdate.c @@ -0,0 +1,346 @@ +/* Convert a string representation of time to a time value. + Copyright (C) 1997-2026 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#ifndef _LIBC +# include <libc-config.h> +#endif + +/* Specification. */ +#include <time.h> + +#include <limits.h> +#include <stdio.h> +#if defined _LIBC +# include <stdio_ext.h> +#endif +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <unistd.h> +#include <sys/stat.h> +#include <ctype.h> + +#ifndef _LIBC +# undef __isleap +# define __isleap(year) \ + ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) +# define __getdate_r _gl_getdate_r +# define __time64_t time_t +# define __stat64_t64 stat +# define __stat64_time64 stat +# define __access access +# define __fsetlocking(fp,flags) (void)0 +# define __feof_unlocked feof +# define __ferror_unlocked ferror +# define __strndup strndup +# define __getline getline +# define time64_now() time (NULL) +# define __localtime64_r localtime_r +# define __mktime64 mktime +#endif + +#define TM_YEAR_BASE 1900 + + +/* Prototypes for local functions. */ +static int first_wday (int year, int mon, int wday); +static int check_mday (int year, int mon, int mday); + + +/* Set to one of the following values to indicate an error. + 1 the DATEMSK environment variable is null or undefined, + 2 the template file cannot be opened for reading, + 3 failed to get file status information, + 4 the template file is not a regular file, + 5 an error is encountered while reading the template file, + 6 memory allocation failed (not enough memory available), + 7 there is no line in the template that matches the input, + 8 invalid input specification Example: February 31 or a time is + specified that can not be represented in a time_t (representing + the time in seconds since 00:00:00 UTC, January 1, 1970) */ +#if defined _LIBC +int getdate_err; +#else /* use outside libc, via gnulib */ +# if !HAVE_GETDATE +static int _getdate_err; +int * +_gl_getdate_location (void) +{ + return &_getdate_err; +} +# endif +#endif + + +/* Returns the first weekday WDAY of month MON in the year YEAR. */ +static int +first_wday (int year, int mon, int wday) +{ + struct tm tm; + + if (wday == INT_MIN) + return 1; + + memset (&tm, 0, sizeof (struct tm)); + tm.tm_year = year; + tm.tm_mon = mon; + tm.tm_mday = 1; + mktime (&tm); + + return (1 + (wday - tm.tm_wday + 7) % 7); +} + + +/* Returns 1 if MDAY is a valid day of the month in month MON of year + YEAR, and 0 if it is not. */ +static int +check_mday (int year, int mon, int mday) +{ + switch (mon) + { + case 0: + case 2: + case 4: + case 6: + case 7: + case 9: + case 11: + if (mday >= 1 && mday <= 31) + return 1; + break; + case 3: + case 5: + case 8: + case 10: + if (mday >= 1 && mday <= 30) + return 1; + break; + case 1: + if (mday >= 1 && mday <= (__isleap (year) ? 29 : 28)) + return 1; + break; + } + + return 0; +} + + +int +__getdate_r (const char *string, struct tm *tp) +{ + FILE *fp; + char *line; + size_t len; + char *datemsk; + char *result = NULL; + __time64_t timer; + struct tm tm; + struct __stat64_t64 st; + bool mday_ok = false; + bool found = false; + + datemsk = getenv ("DATEMSK"); + if (datemsk == NULL || *datemsk == '\0') + return 1; + + if (__stat64_time64 (datemsk, &st) < 0) + return 3; + + if (!S_ISREG (st.st_mode)) + return 4; + + if (__access (datemsk, R_OK) < 0) + return 2; + + /* Open the template file. */ +#if defined _LIBC + fp = fopen (datemsk, "rce"); +#else + fp = fopen (datemsk, "re"); +#endif + if (fp == NULL) + return 2; + + /* No threads reading this stream. */ + __fsetlocking (fp, FSETLOCKING_BYCALLER); + + /* Skip leading whitespace. */ + while (isspace (*string)) + string++; + + size_t inlen, oldlen; + + oldlen = inlen = strlen (string); + + /* Skip trailing whitespace. */ + while (inlen > 0 && isspace (string[inlen - 1])) + inlen--; + + char *instr = NULL; + + if (inlen < oldlen) + { + instr = __strndup(string, inlen); + if (instr == NULL) + { + fclose(fp); + return 6; + } + + string = instr; + } + + line = NULL; + len = 0; + do + { + ssize_t n; + + n = __getline (&line, &len, fp); + if (n < 0) + break; + if (line[n - 1] == '\n') + line[n - 1] = '\0'; + + /* Do the conversion. */ + tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN; + tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN; + tp->tm_isdst = -1; +#if defined _LIBC || HAVE_STRUCT_TM_TM_GMTOFF + tp->tm_gmtoff = 0; +#endif +#if defined _LIBC || HAVE_STRUCT_TM_TM_ZONE + tp->tm_zone = NULL; +#endif + result = strptime (string, line, tp); + if ((found = (result && *result == '\0'))) + break; + } + while (!__feof_unlocked (fp)); + + free (instr); + + /* Free the buffer. */ + free (line); + + /* Check for errors. */ + if (__ferror_unlocked (fp)) + { + fclose (fp); + return 5; + } + + /* Close template file. */ + fclose (fp); + + if (!found) + return 7; + + /* Get current time. */ + timer = time64_now (); + __localtime64_r (&timer, &tm); + + /* If only the weekday is given, today is assumed if the given day + is equal to the current day and next week if it is less. */ + if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN + && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN) + { + tp->tm_year = tm.tm_year; + tp->tm_mon = tm.tm_mon; + tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7; + mday_ok = true; + } + + /* If only the month is given, the current month is assumed if the + given month is equal to the current month and next year if it is + less and no year is given (the first day of month is assumed if + no day is given. */ + if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN) + { + if (tp->tm_year == INT_MIN) + tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0); + tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday); + mday_ok = true; + } + + /* If no hour, minute and second are given the current hour, minute + and second are assumed. */ + if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN) + { + tp->tm_hour = tm.tm_hour; + tp->tm_min = tm.tm_min; + tp->tm_sec = tm.tm_sec; + } + + /* Fill in the gaps. */ + if (tp->tm_hour == INT_MIN) + tp->tm_hour = 0; + if (tp->tm_min == INT_MIN) + tp->tm_min = 0; + if (tp->tm_sec == INT_MIN) + tp->tm_sec = 0; + + /* If no date is given, today is assumed if the given hour is + greater than the current hour and tomorrow is assumed if + it is less. */ + if (tp->tm_hour >= 0 && tp->tm_hour <= 23 + && tp->tm_mon == INT_MIN + && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN) + { + tp->tm_mon = tm.tm_mon; + tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0); + mday_ok = 1; + } + + /* More fillers. */ + if (tp->tm_year == INT_MIN) + tp->tm_year = tm.tm_year; + if (tp->tm_mon == INT_MIN) + tp->tm_mon = tm.tm_mon; + + /* Check if the day of month is within range, and if the time can be + represented in a time_t. We make use of the fact that the mktime + call normalizes the struct tm. But do not normalize *TP, because + that could increment TP->tm_hour due to DST, and this is undesired + here. */ + struct tm tmp; + if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon, + tp->tm_mday)) + || (tmp = *tp, __mktime64 (&tmp) == (time_t) -1)) + return 8; + + return 0; +} +weak_alias (__getdate_r, getdate_r) +libc_hidden_def (__getdate_r) + +struct tm * +getdate (const char *string) +{ + /* Buffer returned by getdate. */ + static struct tm tmbuf; + int errval = __getdate_r (string, &tmbuf); + + if (errval != 0) + { + getdate_err = errval; + return NULL; + } + + return &tmbuf; +} diff --git a/lib/time.in.h b/lib/time.in.h index b0c9e77f10..3e91fbf29b 100644 --- a/lib/time.in.h +++ b/lib/time.in.h @@ -444,6 +444,44 @@ _GL_WARN_ON_USE (strptime, "strptime is unportable - " # endif # endif +/* Parse STRING as a timestamp, assuming its layout matches one of the patterns + contained in the file named by getenv ("DATEMSK"), and store the resulting + broken-down time into TM. See + <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getdate.html>. */ +# if @GNULIB_GETDATE@ +# if @REPLACE_GETDATE@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# define getdate rpl_getdate +# endif +_GL_FUNCDECL_RPL (getdate, struct tm *, (const char *__string), + _GL_ARG_NONNULL ((1))); +_GL_CXXALIAS_RPL (getdate, struct tm *, (const char *__string)); +# else +# if ! @HAVE_GETDATE@ +_GL_FUNCDECL_SYS (getdate, struct tm *, (const char *__string), + _GL_ARG_NONNULL ((1))); +# endif +_GL_CXXALIAS_SYS (getdate, struct tm *, (const char *__string)); +# endif +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN (getdate); +# endif +/* Define getdate_err through a function macro, to avoid problems with + shared libraries on Windows. */ +# if ! @HAVE_GETDATE@ +# if !GNULIB_defined_getdate_err +_GL_EXTERN_C int *_gl_getdate_location (void); +# define getdate_err (* _gl_getdate_location ()) +# define GNULIB_defined_getdate_err 1 +# endif +# endif +# elif defined GNULIB_POSIXCHECK +# if HAVE_RAW_DECL_GETDATE +_GL_WARN_ON_USE (getdate, "getdate is unportable - " + "use gnulib module getdate for portability"); +# endif +# endif + /* Convert *TP to a date and time string. See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */ # if @GNULIB_CTIME@ diff --git a/m4/getdate.m4 b/m4/getdate.m4 new file mode 100644 index 0000000000..867af645d7 --- /dev/null +++ b/m4/getdate.m4 @@ -0,0 +1,107 @@ +# getdate.m4 +# serial 1 +dnl Copyright (C) 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_GETDATE], +[ + AC_REQUIRE([AC_CANONICAL_HOST]) + dnl Persuade glibc and Solaris <time.h> to declare getdate. + AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) + + AC_REQUIRE([gl_TIME_H_DEFAULTS]) + + AC_CHECK_FUNCS_ONCE([getdate]) + if test $ac_cv_func_getdate = yes; then + AC_CACHE_CHECK([whether getdate works], + [gl_cv_func_getdate_works], + [rm -f config.msk + cat > config.msk <<\EOF +%m +%A %B %d, %Y, %H:%M:%S +%A +%B +%m/%d/%y %I %p +%d,%m,%Y %H:%M +%m/%d/%y +%d.%m.%y +%y-%m-%d +%A %H:%M:%S +%a +%b %a +%b %a %Y +%a %H +%b %H:%S +%H:%M +EOF + DATEMSK=config.msk + export DATEMSK + AC_RUN_IFELSE( + [AC_LANG_PROGRAM( + [[#include <stddef.h> + #include <time.h> + ]], + [[int result = 0; + struct tm *t; + /* This test fails on macOS, NetBSD, Solaris 11 OmniOS. */ + t = getdate ("10/1/87 4 PM"); + if (!(t != NULL + && t->tm_year == 1987 - 1900 + && t->tm_mon == 10 - 1 + && t->tm_mday == 1 + && t->tm_hour == 16 && t->tm_min == 0 && t->tm_sec == 0)) + result |= 1; + /* This test fails on musl libc, NetBSD, Solaris 11 OmniOS. */ + t = getdate ("24,9,1986 10:30"); + if (!(t != NULL + && t->tm_year == 1986 - 1900 + && t->tm_mon == 9 - 1 + && t->tm_mday == 24 + && t->tm_hour == 10 && t->tm_min == 30 && t->tm_sec == 0)) + result |= 2; + /* This test fails on musl libc, AIX, Solaris 11 OmniOS. */ + t = getdate ("13:30"); + if (!(t != NULL + && t->tm_mon >= 0 && t->tm_mon < 12 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_hour == 13 && t->tm_min == 30 && t->tm_sec == 0)) + result |= 4; + getdate_err = -1; + return result; + ]])], + [gl_cv_func_getdate_works=yes], + [gl_cv_func_getdate_works=no], + [case "$host_os" in + # Guess yes on glibc systems. + *-gnu* | gnu*) + gl_cv_func_getdate_works="guessing yes" ;; + # Guess no on musl. + linux* | midipix*) + gl_cv_func_getdate_works="guessing no" ;; + # Guess no on macOS, NetBSD, AIX, Solaris 11 OmniOS. + darwin* | nstbsd* | aix* | solaris*) + gl_cv_func_getdate_works="guessing no" ;; + # If we don't know, obey --enable-cross-guesses. + *) gl_cv_func_getdate_works="$gl_cross_guess_normal" ;; + esac + ]) + unset DATEMSK + rm -f config.msk + ]) + case "$gl_cv_func_getdate_works" in + *no) REPLACE_GETDATE=1 ;; + esac + else + HAVE_GETDATE=0 + fi +]) + +# Prerequisites of lib/getdate.c. +AC_DEFUN([gl_PREREQ_GETDATE], +[ + AC_REQUIRE([gl_TM_GMTOFF]) + : +]) diff --git a/m4/time_h.m4 b/m4/time_h.m4 index b7ff1bf988..b611172979 100644 --- a/m4/time_h.m4 +++ b/m4/time_h.m4 @@ -1,5 +1,5 @@ # time_h.m4 -# serial 29 +# serial 30 dnl Copyright (C) 2000-2001, 2003-2007, 2009-2026 Free Software Foundation, dnl Inc. dnl This file is free software; the Free Software Foundation @@ -136,6 +136,7 @@ AC_DEFUN([gl_TIME_H_REQUIRE_DEFAULTS] [ m4_defun(GL_MODULE_INDICATOR_PREFIX[_TIME_H_MODULE_INDICATOR_DEFAULTS], [ gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_CTIME]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETDATE]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MKTIME]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_LOCALTIME]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_NANOSLEEP]) @@ -160,6 +161,7 @@ AC_DEFUN([gl_TIME_H_DEFAULTS] [ dnl Assume proper GNU behavior unless another module says otherwise. HAVE_DECL_LOCALTIME_R=1; AC_SUBST([HAVE_DECL_LOCALTIME_R]) + HAVE_GETDATE=1; AC_SUBST([HAVE_GETDATE]) HAVE_NANOSLEEP=1; AC_SUBST([HAVE_NANOSLEEP]) HAVE_STRPTIME=1; AC_SUBST([HAVE_STRPTIME]) HAVE_TIMEGM=1; AC_SUBST([HAVE_TIMEGM]) @@ -169,6 +171,7 @@ AC_DEFUN([gl_TIME_H_DEFAULTS] HAVE_TIMEZONE_T=0; AC_SUBST([HAVE_TIMEZONE_T]) HAVE_TZALLOC=0; AC_SUBST([HAVE_TZALLOC]) REPLACE_CTIME=0; AC_SUBST([REPLACE_CTIME]) + REPLACE_GETDATE=0; AC_SUBST([REPLACE_GETDATE]) REPLACE_GMTIME=0; AC_SUBST([REPLACE_GMTIME]) REPLACE_LOCALTIME=0; AC_SUBST([REPLACE_LOCALTIME]) REPLACE_LOCALTIME_R=0; AC_SUBST([REPLACE_LOCALTIME_R]) diff --git a/modules/getdate b/modules/getdate new file mode 100644 index 0000000000..31ed549ef0 --- /dev/null +++ b/modules/getdate @@ -0,0 +1,42 @@ +Description: +Convert a string representation of time to a tm structure, +using a file of patterns. + +Files: +lib/getdate.c +m4/getdate.m4 +m4/tm_gmtoff.m4 + +Depends-on: +time-h +extensions +stat [test $HAVE_GETDATE = 0 || test $REPLACE_GETDATE = 1] +fopen-gnu [test $HAVE_GETDATE = 0 || test $REPLACE_GETDATE = 1] +strptime-gnu [test $HAVE_GETDATE = 0 || test $REPLACE_GETDATE = 1] +strndup [test $HAVE_GETDATE = 0 || test $REPLACE_GETDATE = 1] +getline [test $HAVE_GETDATE = 0 || test $REPLACE_GETDATE = 1] +time_r [test $HAVE_GETDATE = 0 || test $REPLACE_GETDATE = 1] +mktime [test $HAVE_GETDATE = 0 || test $REPLACE_GETDATE = 1] + +configure.ac: +gl_FUNC_GETDATE +gl_CONDITIONAL([GL_COND_OBJ_GETDATE], + [test $HAVE_GETDATE = 0 || test $REPLACE_GETDATE = 1]) +AM_COND_IF([GL_COND_OBJ_GETDATE], [ + gl_PREREQ_GETDATE +]) +gl_TIME_MODULE_INDICATOR([getdate]) + +Makefile.am: +if GL_COND_OBJ_GETDATE +lib_SOURCES += getdate.c +endif + +Include: +<time.h> + +License: +LGPLv2+ + +Maintainer: +all diff --git a/modules/time-h b/modules/time-h index 810aa98971..a51dd222be 100644 --- a/modules/time-h +++ b/modules/time-h @@ -33,6 +33,7 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \ -e 's|@''NEXT_TIME_H''@|$(NEXT_TIME_H)|g' \ -e 's/@''GNULIB_CTIME''@/$(GNULIB_CTIME)/g' \ + -e 's/@''GNULIB_GETDATE''@/$(GNULIB_GETDATE)/g' \ -e 's/@''GNULIB_LOCALTIME''@/$(GNULIB_LOCALTIME)/g' \ -e 's/@''GNULIB_MKTIME''@/$(GNULIB_MKTIME)/g' \ -e 's/@''GNULIB_NANOSLEEP''@/$(GNULIB_NANOSLEEP)/g' \ @@ -48,6 +49,7 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's/@''GNULIB_TZSET''@/$(GNULIB_TZSET)/g' \ -e 's/@''GNULIB_MDA_TZSET''@/$(GNULIB_MDA_TZSET)/g' \ -e 's|@''HAVE_DECL_LOCALTIME_R''@|$(HAVE_DECL_LOCALTIME_R)|g' \ + -e 's|@''HAVE_GETDATE''@|$(HAVE_GETDATE)|g' \ -e 's|@''HAVE_NANOSLEEP''@|$(HAVE_NANOSLEEP)|g' \ -e 's|@''HAVE_STRPTIME''@|$(HAVE_STRPTIME)|g' \ -e 's|@''HAVE_TIMEGM''@|$(HAVE_TIMEGM)|g' \ @@ -56,6 +58,7 @@ time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''HAVE_TIMEZONE_T''@|$(HAVE_TIMEZONE_T)|g' \ -e 's|@''HAVE_TZALLOC''@|$(HAVE_TZALLOC)|g' \ -e 's|@''REPLACE_CTIME''@|$(REPLACE_CTIME)|g' \ + -e 's|@''REPLACE_GETDATE''@|$(REPLACE_GETDATE)|g' \ -e 's|@''REPLACE_GMTIME''@|$(REPLACE_GMTIME)|g' \ -e 's|@''REPLACE_LOCALTIME''@|$(REPLACE_LOCALTIME)|g' \ -e 's|@''REPLACE_LOCALTIME_R''@|$(REPLACE_LOCALTIME_R)|g' \ -- 2.53.0
>From 20bda4508475735a2b3f99b75b2c93326822f7bf Mon Sep 17 00:00:00 2001 From: Bruno Haible <[email protected]> Date: Mon, 23 Nov 2026 17:01:21 +0100 Subject: [PATCH 2/2] getdate: Add tests. * tests/test-getdate.c: New file. * modules/getdate-tests: New file. --- ChangeLog | 4 + modules/getdate-tests | 12 ++ tests/test-getdate.c | 323 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 339 insertions(+) create mode 100644 modules/getdate-tests create mode 100644 tests/test-getdate.c diff --git a/ChangeLog b/ChangeLog index f19edf93f6..836ab9af47 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2026-11-23 Bruno Haible <[email protected]> + getdate: Add tests. + * tests/test-getdate.c: New file. + * modules/getdate-tests: New file. + getdate: New module. * lib/time.in.h (getdate, _gl_getdate_location): New declarations. (getdate_err): New macro. diff --git a/modules/getdate-tests b/modules/getdate-tests new file mode 100644 index 0000000000..7ac9a7d45f --- /dev/null +++ b/modules/getdate-tests @@ -0,0 +1,12 @@ +Files: +tests/test-getdate.c +tests/signature.h +tests/macros.h + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-getdate +check_PROGRAMS += test-getdate diff --git a/tests/test-getdate.c b/tests/test-getdate.c new file mode 100644 index 0000000000..77a5a7a3d6 --- /dev/null +++ b/tests/test-getdate.c @@ -0,0 +1,323 @@ +/* Test of getdate() function. + Copyright (C) 2023-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. */ + +#include <config.h> + +/* Specification. */ +#include <time.h> + +#include "signature.h" +SIGNATURE_CHECK (getdate, struct tm *, (const char *)); + +#include <stdio.h> + +#include "macros.h" + +int +main (void) +{ + /* Preparations. */ + { + const char *filename = "datemsk"; + + FILE *fp = fopen (filename, "w"); + ASSERT (fp != NULL); + const char *contents = + "%m\n" + "%A %B %d, %Y, %H:%M:%S\n" + "%A\n" + "%B\n" + "%m/%d/%y %I %p\n" + "%d,%m,%Y %H:%M\n" + "%m/%d/%y\n" + "%d.%m.%y\n" + "%y-%m-%d\n" + "%A %H:%M:%S\n" + "%a\n" + "%b %a\n" + "%b %a %Y\n" + "%a %H\n" + "%b %H:%S\n" + "%H:%M\n" + ; + ASSERT (fputs (contents, fp) != EOF); + ASSERT (fclose (fp) == 0); + + ASSERT (setenv ("DATEMSK", filename, 1) == 0); + } + + /* This test case originally failed on macOS, NetBSD, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("10/1/87 4 PM"); + ASSERT (t != NULL); + ASSERT (t->tm_year == 1987 - 1900 + && t->tm_mon == 10 - 1 + && t->tm_mday == 1 + && t->tm_hour == 16 && t->tm_min == 0 && t->tm_sec == 0); + } + + /* This test case originally failed on Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Friday September 18, 1987, 10:30:30"); + ASSERT (t != NULL); + ASSERT (t->tm_year == 1987 - 1900 + && t->tm_mon == 9 - 1 + && t->tm_mday == 18 + && t->tm_hour == 10 && t->tm_min == 30 && t->tm_sec == 30); + } + + /* This test case originally failed on musl libc, NetBSD, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("24,9,1986 10:30"); + ASSERT (t != NULL); + ASSERT (t->tm_year == 1986 - 1900 + && t->tm_mon == 9 - 1 + && t->tm_mday == 24 + && t->tm_hour == 10 && t->tm_min == 30 && t->tm_sec == 0); + } + + /* This test case originally failed on NetBSD, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("11/27/86"); + ASSERT (t != NULL); + ASSERT (t->tm_year == 1986 - 1900 + && t->tm_mon == 11 - 1 + && t->tm_mday == 27 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on NetBSD. */ + { + struct tm *t = getdate ("27.11.86"); + ASSERT (t != NULL); + ASSERT (t->tm_year == 1986 - 1900 + && t->tm_mon == 11 - 1 + && t->tm_mday == 27 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on NetBSD. */ + { + struct tm *t = getdate ("86-11-27"); + ASSERT (t != NULL); + ASSERT (t->tm_year == 1986 - 1900 + && t->tm_mon == 11 - 1 + && t->tm_mday == 27 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on NetBSD, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Friday 12:00:00"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon >= 0 && t->tm_mon < 12 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_wday == 5 + && t->tm_hour == 12 && t->tm_min == 0 && t->tm_sec == 0); + } + + /* This test case originally failed on Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Friday"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon >= 0 && t->tm_mon < 12 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_wday == 5 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Mon"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon >= 0 && t->tm_mon < 12 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_wday == 1 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Sun"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon >= 0 && t->tm_mon < 12 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_wday == 0 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Fri"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon >= 0 && t->tm_mon < 12 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_wday == 5 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("September"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon == 9 - 1 + && t->tm_mday >= 1 && t->tm_mday <= 30 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("January"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon == 1 - 1 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("December"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon == 12 - 1 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on NetBSD, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Sep Mon"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon == 9 - 1 + && t->tm_mday >= 1 && t->tm_mday <= 30 + && t->tm_wday == 1 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on NetBSD, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Jan Fri"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon == 1 - 1 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_wday == 5 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on NetBSD, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Dec Mon"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon == 12 - 1 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_wday == 1 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on NetBSD, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Jan Wed 1989"); + ASSERT (t != NULL); + ASSERT (t->tm_year == 1989 - 1900 + && t->tm_mon == 1 - 1 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_wday == 3 + && t->tm_hour >= 0 && t->tm_hour < 24 + && t->tm_min >= 0 && t->tm_min < 60 + && t->tm_sec >= 0 && t->tm_sec < 60); + } + + /* This test case originally failed on NetBSD, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Fri 9"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon >= 0 && t->tm_mon < 12 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_wday == 5 + && t->tm_hour == 9 && t->tm_min == 0 && t->tm_sec == 0); + } + + /* This test case originally failed on NetBSD, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("Feb 10:30"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon == 2 - 1 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_hour == 10 && t->tm_min == 0 && t->tm_sec == 30); + } + + /* This test case originally failed on NetBSD, AIX, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("10:30"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon >= 0 && t->tm_mon < 12 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_hour == 10 && t->tm_min == 30 && t->tm_sec == 0); + } + + /* This test case originally failed on musl libc, AIX, Solaris 11 OmniOS. */ + { + struct tm *t = getdate ("13:30"); + ASSERT (t != NULL); + ASSERT (t->tm_year >= 1970 - 1900 + && t->tm_mon >= 0 && t->tm_mon < 12 + && t->tm_mday >= 1 && t->tm_mday <= 31 + && t->tm_hour == 13 && t->tm_min == 30 && t->tm_sec == 0); + } + + return test_exit_status; +} -- 2.53.0
