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