*** empty log message ***
[findutils.git] / m4 / strftime.m4
blobc389a32207ed0ff4473b05dcd4d8bd72b467267d
1 #serial 9
3 dnl This macro is intended to be used solely in this file.
4 dnl These are the prerequisite macros for GNU's strftime.c replacement.
5 AC_DEFUN(_jm_STRFTIME_PREREQS,
7  dnl strftime.c uses localtime_r if it exists.  Check for it.
8  AC_CHECK_FUNCS(localtime_r)
10  AC_CHECK_HEADERS(limits.h)
11  AC_CHECK_FUNCS(bcopy tzset mempcpy memcpy memset)
13  # This defines (or not) HAVE_TZNAME and HAVE_TM_ZONE.
14  AC_STRUCT_TIMEZONE
16  AC_CHECK_FUNCS(mblen mbrlen)
18  AC_CHECK_MEMBER(struct tm.tm_gmtoff,
19                  [AC_DEFINE(HAVE_TM_GMTOFF, 1,
20                             [Define if struct tm has the tm_gmtoff member.])],
21                  ,
22                  [#include <time.h>])
25 dnl Determine if the strftime function has all the features of the GNU one.
26 dnl
27 dnl From Jim Meyering.
28 dnl
29 AC_DEFUN(jm_FUNC_GNU_STRFTIME,
30 [AC_REQUIRE([AC_HEADER_TIME])dnl
32  _jm_STRFTIME_PREREQS
34  AC_REQUIRE([AC_C_CONST])dnl
35  AC_REQUIRE([AC_HEADER_STDC])dnl
36  AC_CHECK_HEADERS(sys/time.h)
37  AC_CACHE_CHECK([for working GNU strftime], jm_cv_func_working_gnu_strftime,
38   [# Set TZ to GMT0 to make strftime give consistent results during the test.
39    ac_save_TZ="${TZ-GMT0}"
40    TZ=GMT0
41    export TZ
42    AC_TRY_RUN(
43 [ /* Ulrich Drepper provided parts of the test program.  */
44 #if STDC_HEADERS
45 # include <stdlib.h>
46 #endif
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
50 # include <time.h>
51 #else
52 # if HAVE_SYS_TIME_H
53 #  include <sys/time.h>
54 # else
55 #  include <time.h>
56 # endif
57 #endif
59 static int
60 compare (const char *fmt, const struct tm *tm, const char *expected)
62   char buf[99];
63   strftime (buf, 99, fmt, tm);
64   if (strcmp (buf, expected))
65     {
66 #ifdef SHOW_FAILURES
67       printf ("fmt: \"%s\", expected \"%s\", got \"%s\"\n",
68               fmt, expected, buf);
69 #endif
70       return 1;
71     }
72   return 0;
75 int
76 main ()
78   int n_fail = 0;
79   struct tm *tm;
80   time_t t = 738367; /* Fri Jan  9 13:06:07 1970 */
81   tm = gmtime (&t);
83 #undef CMP
84 #define CMP(Fmt, Expected) n_fail += compare ((Fmt), tm, (Expected))
86   CMP ("%-m", "1");             /* GNU */
87   CMP ("%A", "Friday");
88   CMP ("%^A", "FRIDAY");        /* The ^ is a GNU extension.  */
89   CMP ("%B", "January");
90   CMP ("%^B", "JANUARY");
91   CMP ("%C", "19");             /* POSIX.2 */
92   CMP ("%D", "01/09/70");       /* POSIX.2 */
93   CMP ("%F", "1970-01-09");
94   CMP ("%G", "1970");           /* GNU */
95   CMP ("%H", "13");
96   CMP ("%I", "01");
97   CMP ("%M", "06");
98   CMP ("%M", "06");
99   CMP ("%R", "13:06");          /* POSIX.2 */
100   CMP ("%S", "07");
101   CMP ("%T", "13:06:07");       /* POSIX.2 */
102   CMP ("%U", "01");
103   CMP ("%V", "02");
104   CMP ("%W", "01");
105   CMP ("%X", "13:06:07");
106   CMP ("%Y", "1970");
107   CMP ("%Z", "GMT");
108   CMP ("%_m", " 1");            /* GNU */
109   CMP ("%a", "Fri");
110   CMP ("%^a", "FRI");
111   CMP ("%b", "Jan");
112   CMP ("%^b", "JAN");
113   CMP ("%c", "Fri Jan  9 13:06:07 1970");
114   CMP ("%^c", "FRI JAN  9 13:06:07 1970");
115   CMP ("%d", "09");
116   CMP ("%e", " 9");             /* POSIX.2 */
117   CMP ("%g", "70");             /* GNU */
118   CMP ("%h", "Jan");            /* POSIX.2 */
119   CMP ("%^h", "JAN");
120   CMP ("%j", "009");
121   CMP ("%k", "13");             /* GNU */
122   CMP ("%l", " 1");             /* GNU */
123   CMP ("%m", "01");
124   CMP ("%n", "\n");             /* POSIX.2 */
125   CMP ("%p", "PM");
126   CMP ("%r", "01:06:07 PM");    /* POSIX.2 */
127   CMP ("%s", "738367");         /* GNU */
128   CMP ("%t", "\t");             /* POSIX.2 */
129   CMP ("%u", "5");              /* POSIX.2 */
130   CMP ("%w", "5");
131   CMP ("%x", "01/09/70");
132   CMP ("%y", "70");
133   CMP ("%z", "+0000");          /* GNU */
135   exit (n_fail ? 1 : 0);
137              jm_cv_func_working_gnu_strftime=yes,
138              jm_cv_func_working_gnu_strftime=no,
139              dnl When crosscompiling, assume strftime is missing or broken.
140              jm_cv_func_working_gnu_strftime=no)
141    dnl If TZ wasn't set before, this sets it to GMT0.  No real harm done.
142    TZ="$ac_save_TZ"
143   ])
144   if test $jm_cv_func_working_gnu_strftime = no; then
145     AC_SUBST(LIBOBJS)
146     LIBOBJS="$LIBOBJS strftime.$ac_objext"
147     AC_DEFINE_UNQUOTED(strftime, gnu_strftime,
148       [Define to gnu_strftime if the replacement function should be used.])
149   fi
152 AC_DEFUN(jm_FUNC_STRFTIME,
154   _jm_STRFTIME_PREREQS
155   AC_REPLACE_FUNCS(strftime)