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