(_jm_STRFTIME_PREREQS): Check for strftime,
[pspp] / m4 / strftime.m4
1 #serial 11
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 and the underyling system strftime
8  dnl if they exist.
9  AC_CHECK_FUNCS(localtime_r strftime)
10
11  AC_CHECK_HEADERS(limits.h)
12  AC_CHECK_FUNCS(bcopy tzset mempcpy memcpy memset)
13
14  # This defines (or not) HAVE_TZNAME and HAVE_TM_ZONE.
15  AC_STRUCT_TIMEZONE
16
17  AC_CHECK_FUNCS(mblen mbrlen)
18
19  AC_CHECK_MEMBER(struct tm.tm_gmtoff,
20                  [AC_DEFINE(HAVE_TM_GMTOFF, 1,
21                             [Define if struct tm has the tm_gmtoff member.])],
22                  ,
23                  [#include <time.h>])
24 ])
25
26 dnl Determine if the strftime function has all the features of the GNU one.
27 dnl
28 dnl From Jim Meyering.
29 dnl
30 AC_DEFUN(jm_FUNC_GNU_STRFTIME,
31 [AC_REQUIRE([AC_HEADER_TIME])dnl
32
33  _jm_STRFTIME_PREREQS
34
35  AC_REQUIRE([AC_C_CONST])dnl
36  AC_REQUIRE([AC_HEADER_STDC])dnl
37  AC_CHECK_HEADERS(sys/time.h)
38  AC_CACHE_CHECK([for working GNU strftime], jm_cv_func_working_gnu_strftime,
39   [# Set TZ to GMT0 to make strftime give consistent results during the test.
40    ac_save_TZ="${TZ-GMT0}"
41    TZ=GMT0
42    export TZ
43    AC_TRY_RUN(
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 #undef CMP
85 #define CMP(Fmt, Expected) n_fail += compare ((Fmt), tm, (Expected))
86
87   CMP ("%-m", "1");             /* GNU */
88   CMP ("%A", "Friday");
89   CMP ("%^A", "FRIDAY");        /* The ^ is a GNU extension.  */
90   CMP ("%B", "January");
91   CMP ("%^B", "JANUARY");
92   CMP ("%C", "19");             /* POSIX.2 */
93   CMP ("%D", "01/09/70");       /* POSIX.2 */
94   CMP ("%F", "1970-01-09");
95   CMP ("%G", "1970");           /* GNU */
96   CMP ("%H", "13");
97   CMP ("%I", "01");
98   CMP ("%M", "06");
99   CMP ("%M", "06");
100   CMP ("%R", "13:06");          /* POSIX.2 */
101   CMP ("%S", "07");
102   CMP ("%T", "13:06:07");       /* POSIX.2 */
103   CMP ("%U", "01");
104   CMP ("%V", "02");
105   CMP ("%W", "01");
106   CMP ("%X", "13:06:07");
107   CMP ("%Y", "1970");
108   CMP ("%Z", "GMT");
109   CMP ("%_m", " 1");            /* GNU */
110   CMP ("%a", "Fri");
111   CMP ("%^a", "FRI");
112   CMP ("%b", "Jan");
113   CMP ("%^b", "JAN");
114   CMP ("%c", "Fri Jan  9 13:06:07 1970");
115   CMP ("%^c", "FRI JAN  9 13:06:07 1970");
116   CMP ("%d", "09");
117   CMP ("%e", " 9");             /* POSIX.2 */
118   CMP ("%g", "70");             /* GNU */
119   CMP ("%h", "Jan");            /* POSIX.2 */
120   CMP ("%^h", "JAN");
121   CMP ("%j", "009");
122   CMP ("%k", "13");             /* GNU */
123   CMP ("%l", " 1");             /* GNU */
124   CMP ("%m", "01");
125   CMP ("%n", "\n");             /* POSIX.2 */
126   CMP ("%p", "PM");
127   CMP ("%r", "01:06:07 PM");    /* POSIX.2 */
128   CMP ("%s", "738367");         /* GNU */
129   CMP ("%t", "\t");             /* POSIX.2 */
130   CMP ("%u", "5");              /* POSIX.2 */
131   CMP ("%w", "5");
132   CMP ("%x", "01/09/70");
133   CMP ("%y", "70");
134   CMP ("%z", "+0000");          /* GNU */
135
136   exit (n_fail ? 1 : 0);
137 }],
138              jm_cv_func_working_gnu_strftime=yes,
139              jm_cv_func_working_gnu_strftime=no,
140              dnl When crosscompiling, assume strftime is missing or broken.
141              jm_cv_func_working_gnu_strftime=no)
142    dnl If TZ wasn't set before, this sets it to GMT0.  No real harm done.
143    TZ="$ac_save_TZ"
144   ])
145   if test $jm_cv_func_working_gnu_strftime = no; then
146     AC_SUBST(LIBOBJS)
147     LIBOBJS="$LIBOBJS strftime.$ac_objext"
148     AC_DEFINE_UNQUOTED(strftime, gnu_strftime,
149       [Define to gnu_strftime if the replacement function should be used.])
150   fi
151 ])
152
153 AC_DEFUN(jm_FUNC_STRFTIME,
154 [
155   _jm_STRFTIME_PREREQS
156   AC_REPLACE_FUNCS(strftime)
157 ])