Support POSIX flush semantics on all platforms.
[m4.git] / modules / time.c
blob962626fea0a7044b90df67443dd05401b60bd487
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1999, 2000, 2001, 2006, 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA
20 #include <config.h>
22 #if TM_IN_SYS_TIME
23 # include <sys/time.h>
24 #else
25 # include <time.h>
26 #endif /* TM_IN_SYS_TIME */
28 /* Build using only the exported interfaces, unless NDEBUG is set, in
29 which case use private symbols to speed things up as much as possible. */
30 #ifndef NDEBUG
31 # include <m4/m4module.h>
32 #else
33 # include "m4private.h"
34 #endif
36 /* Rename exported symbols for dlpreload()ing. */
37 #define m4_builtin_table time_LTX_m4_builtin_table
39 /* function macros blind side minargs maxargs */
40 #define builtin_functions \
41 BUILTIN (currenttime, false, false, false, 0, 0 ) \
42 BUILTIN (ctime, false, false, false, 0, 1 ) \
43 BUILTIN (gmtime, false, true, false, 1, 1 ) \
44 BUILTIN (localtime, false, true, false, 1, 1 ) \
46 #define mktime_functions \
47 BUILTIN (mktime, false, true, false, 6, 7 ) \
49 #define strftime_functions \
50 BUILTIN (strftime, false, true, false, 2, 2 ) \
53 #define BUILTIN(handler, macros, blind, side, min, max) M4BUILTIN(handler)
54 builtin_functions
55 # if HAVE_MKTIME
56 mktime_functions
57 # endif
58 # if HAVE_STRFTIME
59 strftime_functions
60 # endif
61 #undef BUILTIN
63 m4_builtin m4_builtin_table[] =
65 #define BUILTIN(handler, macros, blind, side, min, max) \
66 { CONC(builtin_, handler), STR(handler), \
67 ((macros ? M4_BUILTIN_GROKS_MACRO : 0) \
68 | (blind ? M4_BUILTIN_BLIND : 0) \
69 | (side ? M4_BUILTIN_SIDE_EFFECT : 0)), \
70 min, max },
72 builtin_functions
73 # if HAVE_MKTIME
74 mktime_functions
75 # endif
76 # if HAVE_STRFTIME
77 strftime_functions
78 # endif
79 #undef BUILTIN
81 { NULL, NULL, 0, 0, 0 },
84 /**
85 * currenttime()
86 **/
87 M4BUILTIN_HANDLER (currenttime)
89 char buf[64];
90 time_t now;
91 int l;
93 now = time (0L);
94 l = sprintf (buf, "%ld", now);
96 obstack_grow (obs, buf, l);
99 /**
100 * ctime([SECONDS])
102 M4BUILTIN_HANDLER (ctime)
104 time_t t;
105 int i;
106 const char *s;
108 if (argc == 2)
110 m4_numeric_arg (context, argc, argv, 1, &i);
111 t = i;
113 else
114 t = time (0L);
116 s = ctime (&t);
117 obstack_grow (obs, s, 24);
120 static void
121 format_tm (m4_obstack *obs, struct tm *tm)
123 m4_shipout_int (obs, tm->tm_sec);
124 obstack_1grow (obs, ',');
126 m4_shipout_int (obs, tm->tm_min);
127 obstack_1grow (obs, ',');
129 m4_shipout_int (obs, tm->tm_hour);
130 obstack_1grow (obs, ',');
132 m4_shipout_int (obs, tm->tm_mday);
133 obstack_1grow (obs, ',');
135 m4_shipout_int (obs, tm->tm_mon);
136 obstack_1grow (obs, ',');
138 m4_shipout_int (obs, tm->tm_year);
139 obstack_1grow (obs, ',');
141 m4_shipout_int (obs, tm->tm_wday);
142 obstack_1grow (obs, ',');
144 m4_shipout_int (obs, tm->tm_yday);
145 obstack_1grow (obs, ',');
147 m4_shipout_int (obs, tm->tm_isdst);
151 * gmtime(SECONDS)
153 M4BUILTIN_HANDLER (gmtime)
155 time_t t;
156 int i;
158 if (!m4_numeric_arg (context, argc, argv, 1, &i))
159 return;
161 t = i;
162 format_tm (obs, gmtime (&t));
166 * localtime(SECONDS)
168 M4BUILTIN_HANDLER (localtime)
170 time_t t;
171 int i;
173 if (!m4_numeric_arg (context, argc, argv, 1, &i))
174 return;
176 t = i;
177 format_tm (obs, localtime (&t));
180 #if HAVE_MKTIME
182 * mktime(SEC, MIN, HOUR, MDAY, MONTH, YEAR, [ISDST])
184 M4BUILTIN_HANDLER (mktime)
186 struct tm tm;
187 time_t t;
189 if (!m4_numeric_arg (context, argc, argv, 1, &tm.tm_sec))
190 return;
191 if (!m4_numeric_arg (context, argc, argv, 2, &tm.tm_min))
192 return;
193 if (!m4_numeric_arg (context, argc, argv, 3, &tm.tm_hour))
194 return;
195 if (!m4_numeric_arg (context, argc, argv, 4, &tm.tm_mday))
196 return;
197 if (!m4_numeric_arg (context, argc, argv, 5, &tm.tm_mon))
198 return;
199 if (!m4_numeric_arg (context, argc, argv, 6, &tm.tm_year))
200 return;
201 if (M4ARG (7) && !m4_numeric_arg (context, argc, argv, 7, &tm.tm_isdst))
202 return;
204 t = mktime (&tm);
206 m4_shipout_int (obs, t);
208 #endif /* HAVE_MKTIME */
210 #if HAVE_STRFTIME
212 * strftime(FORMAT, SECONDS)
214 M4BUILTIN_HANDLER (strftime)
216 struct tm *tm;
217 time_t t;
218 char *buf;
219 int l;
221 if (!m4_numeric_arg (context, argc, argv, 2, &l))
222 return;
224 t = l;
225 tm = localtime (&t);
227 buf = (char *) obstack_alloc (obs, 1024);
228 l = strftime (buf, 1024, M4ARG (1), tm);
229 obstack_grow (obs, buf, l);
231 #endif /* HAVE_STRFTIME */