Add checks for most of the cpp macros tested in GNU's strftime.c.
[gnulib.git] / m4 / strftime.m4
1 #serial 7
2
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,
6 [
7  dnl strftime.c uses localtime_r if it exists.  Check for it.
8  AC_CHECK_FUNCS(localtime_r)
9
10  AC_CHECK_HEADERS(limits.h)
11  AC_CHECK_FUNCS(bcopy tzset mempcpy memcpy memset)
12
13  # This defines (or not) HAVE_TZNAME and HAVE_TM_ZONE.
14  AC_STRUCT_TIMEZONE
15
16  AC_CHECK_FUNCS(mblen mbrlen)
17
18  AC_CHECK_MEMBER(struct tm.tm_gmtoff,
19                  [AC_DEFINE(HAVE_TM_GMTOFF)],
20                  ,
21                  dnl FIXME: having to put the closing square bracket on the
22                  dnl FIXME: next line is a gross kludge to work around an
23                  dnl FIXME: infelicity in how the new autoconf works.
24                  dnl FIXME: This should be fixed pretty soon.
25                  [#include <time.h>
26                  ])
27 ])
28
29 dnl Determine if the strftime function has all the features of the GNU one.
30 dnl
31 dnl From Jim Meyering.
32 dnl
33 AC_DEFUN(jm_FUNC_GNU_STRFTIME,
34 [AC_REQUIRE([AC_HEADER_TIME])dnl
35
36  _jm_STRFTIME_PREREQS
37
38  AC_REQUIRE([AC_C_CONST])dnl
39  AC_REQUIRE([AC_HEADER_STDC])dnl
40  AC_CHECK_HEADERS(sys/time.h)
41  AC_CACHE_CHECK([for working GNU strftime], jm_cv_func_working_gnu_strftime,
42   [AC_TRY_RUN(
43 changequote(<<, >>)dnl
44 << /* Ulrich Drepper provided parts of the test program.  */
45 #if STDC_HEADERS
46 # include <stdlib.h>
47 #endif
48
49 #if TIME_WITH_SYS_TIME
50 # include <sys/time.h>
51 # include <time.h>
52 #else
53 # if HAVE_SYS_TIME_H
54 #  include <sys/time.h>
55 # else
56 #  include <time.h>
57 # endif
58 #endif
59
60 static int
61 compare (const char *fmt, const struct tm *tm, const char *expected)
62 {
63   char buf[99];
64   strftime (buf, 99, fmt, tm);
65   if (strcmp (buf, expected))
66     {
67 #ifdef SHOW_FAILURES
68       printf ("fmt: \"%s\", expected \"%s\", got \"%s\"\n",
69               fmt, expected, buf);
70 #endif
71       return 1;
72     }
73   return 0;
74 }
75
76 int
77 main ()
78 {
79   int n_fail = 0;
80   struct tm *tm;
81   time_t t = 738367; /* Fri Jan  9 13:06:07 1970 */
82   tm = gmtime (&t);
83
84   /* This is necessary to make strftime give consistent zone strings and
85      e.g., seconds since the epoch (%s).  */
86   putenv ("TZ=GMT0");
87
88 #undef CMP
89 #define CMP(Fmt, Expected) n_fail += compare ((Fmt), tm, (Expected))
90
91   CMP ("%-m", "1");             /* GNU */
92   CMP ("%A", "Friday");
93   CMP ("%^A", "FRIDAY");        /* The ^ is a GNU extension.  */
94   CMP ("%B", "January");
95   CMP ("%^B", "JANUARY");
96   CMP ("%C", "19");             /* POSIX.2 */
97   CMP ("%D", "01/09/70");       /* POSIX.2 */
98   CMP ("%F", "1970-01-09");
99   CMP ("%G", "1970");           /* GNU */
100   CMP ("%H", "13");
101   CMP ("%I", "01");
102   CMP ("%M", "06");
103   CMP ("%M", "06");
104   CMP ("%R", "13:06");          /* POSIX.2 */
105   CMP ("%S", "07");
106   CMP ("%T", "13:06:07");       /* POSIX.2 */
107   CMP ("%U", "01");
108   CMP ("%V", "02");
109   CMP ("%W", "01");
110   CMP ("%X", "13:06:07");
111   CMP ("%Y", "1970");
112   CMP ("%Z", "GMT");
113   CMP ("%_m", " 1");            /* GNU */
114   CMP ("%a", "Fri");
115   CMP ("%^a", "FRI");
116   CMP ("%b", "Jan");
117   CMP ("%^b", "JAN");
118   CMP ("%c", "Fri Jan  9 13:06:07 1970");
119   CMP ("%^c", "FRI JAN  9 13:06:07 1970");
120   CMP ("%d", "09");
121   CMP ("%e", " 9");             /* POSIX.2 */
122   CMP ("%g", "70");             /* GNU */
123   CMP ("%h", "Jan");            /* POSIX.2 */
124   CMP ("%^h", "JAN");
125   CMP ("%j", "009");
126   CMP ("%k", "13");             /* GNU */
127   CMP ("%l", " 1");             /* GNU */
128   CMP ("%m", "01");
129   CMP ("%n", "\n");             /* POSIX.2 */
130   CMP ("%p", "PM");
131   CMP ("%r", "01:06:07 PM");    /* POSIX.2 */
132   CMP ("%s", "738367");         /* GNU */
133   CMP ("%t", "\t");             /* POSIX.2 */
134   CMP ("%u", "5");              /* POSIX.2 */
135   CMP ("%w", "5");
136   CMP ("%x", "01/09/70");
137   CMP ("%y", "70");
138   CMP ("%z", "+0000");          /* GNU */
139
140   exit (n_fail ? 1 : 0);
141 }
142               >>,
143 changequote([, ])dnl
144              jm_cv_func_working_gnu_strftime=yes,
145              jm_cv_func_working_gnu_strftime=no,
146              dnl When crosscompiling, assume strftime is missing or broken.
147              jm_cv_func_working_gnu_strftime=no)
148   ])
149   if test $jm_cv_func_working_gnu_strftime = no; then
150     AC_SUBST(LIBOBJS)
151     LIBOBJS="$LIBOBJS strftime.$ac_objext"
152     AC_DEFINE_UNQUOTED(strftime, gnu_strftime,
153       [Define to gnu_strftime if the replacement function should be used.])
154   fi
155 ])
156
157 AC_DEFUN(jm_FUNC_STRFTIME,
158 [
159   _jm_STRFTIME_PREREQS
160   AC_REPLACE_FUNCS(strftime)
161 ])