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