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