Make gnulib a git submodule.
[m4.git] / modules / time.c
blobee193a7bb297b2593e6c8551e4ef377e561771d7
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1999, 2000, 2001, 2006, 2007, 2008, 2009 Free
3 Software Foundation, Inc.
5 This file is part of GNU M4.
7 GNU M4 is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU M4 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 #if TM_IN_SYS_TIME
24 # include <sys/time.h>
25 #else
26 # include <time.h>
27 #endif /* TM_IN_SYS_TIME */
29 /* Build using only the exported interfaces, unless NDEBUG is set, in
30 which case use private symbols to speed things up as much as possible. */
31 #ifndef NDEBUG
32 # include <m4/m4module.h>
33 #else
34 # include "m4private.h"
35 #endif
37 /* Rename exported symbols for dlpreload()ing. */
38 #define m4_builtin_table time_LTX_m4_builtin_table
40 /* function macros blind side minargs maxargs */
41 #define builtin_functions \
42 BUILTIN (currenttime, false, false, false, 0, 0 ) \
43 BUILTIN (ctime, false, false, false, 0, 1 ) \
44 BUILTIN (gmtime, false, true, false, 1, 1 ) \
45 BUILTIN (localtime, false, true, false, 1, 1 ) \
47 #define mktime_functions \
48 BUILTIN (mktime, false, true, false, 6, 7 ) \
50 #define strftime_functions \
51 BUILTIN (strftime, false, true, false, 2, 2 ) \
54 #define BUILTIN(handler, macros, blind, side, min, max) M4BUILTIN (handler)
55 builtin_functions
56 # if HAVE_MKTIME
57 mktime_functions
58 # endif
59 # if HAVE_STRFTIME
60 strftime_functions
61 # endif
62 #undef BUILTIN
64 const m4_builtin m4_builtin_table[] =
66 #define BUILTIN(handler, macros, blind, side, min, max) \
67 M4BUILTIN_ENTRY (handler, #handler, macros, blind, side, min, max)
69 builtin_functions
70 # if HAVE_MKTIME
71 mktime_functions
72 # endif
73 # if HAVE_STRFTIME
74 strftime_functions
75 # endif
76 #undef BUILTIN
78 { NULL, NULL, 0, 0, 0 },
81 /**
82 * currenttime()
83 **/
84 M4BUILTIN_HANDLER (currenttime)
86 char buf[64];
87 time_t now;
88 int l;
90 now = time (0L);
91 l = sprintf (buf, "%ld", now);
93 obstack_grow (obs, buf, l);
96 /**
97 * ctime([SECONDS])
98 **/
99 M4BUILTIN_HANDLER (ctime)
101 time_t t;
102 int i;
103 const char *s;
105 if (argc == 2)
107 m4_numeric_arg (context, m4_arg_info (argv), M4ARG (1), M4ARGLEN (1),
108 &i);
109 t = i;
111 else
112 t = time (0L);
114 s = ctime (&t);
115 obstack_grow (obs, s, 24);
118 static void
119 format_tm (m4_obstack *obs, struct tm *tm)
121 m4_shipout_int (obs, tm->tm_sec);
122 obstack_1grow (obs, ',');
124 m4_shipout_int (obs, tm->tm_min);
125 obstack_1grow (obs, ',');
127 m4_shipout_int (obs, tm->tm_hour);
128 obstack_1grow (obs, ',');
130 m4_shipout_int (obs, tm->tm_mday);
131 obstack_1grow (obs, ',');
133 m4_shipout_int (obs, tm->tm_mon);
134 obstack_1grow (obs, ',');
136 m4_shipout_int (obs, tm->tm_year);
137 obstack_1grow (obs, ',');
139 m4_shipout_int (obs, tm->tm_wday);
140 obstack_1grow (obs, ',');
142 m4_shipout_int (obs, tm->tm_yday);
143 obstack_1grow (obs, ',');
145 m4_shipout_int (obs, tm->tm_isdst);
149 * gmtime(SECONDS)
151 M4BUILTIN_HANDLER (gmtime)
153 time_t t;
154 int i;
156 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (1), M4ARGLEN (1),
157 &i))
158 return;
160 t = i;
161 format_tm (obs, gmtime (&t));
165 * localtime(SECONDS)
167 M4BUILTIN_HANDLER (localtime)
169 time_t t;
170 int i;
172 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (1), M4ARGLEN (1),
173 &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 const m4_call_info *me = m4_arg_info (argv);
187 struct tm tm;
188 time_t t;
190 if (!m4_numeric_arg (context, me, M4ARG (1), M4ARGLEN (1), &tm.tm_sec))
191 return;
192 if (!m4_numeric_arg (context, me, M4ARG (2), M4ARGLEN (2), &tm.tm_min))
193 return;
194 if (!m4_numeric_arg (context, me, M4ARG (3), M4ARGLEN (3), &tm.tm_hour))
195 return;
196 if (!m4_numeric_arg (context, me, M4ARG (4), M4ARGLEN (4), &tm.tm_mday))
197 return;
198 if (!m4_numeric_arg (context, me, M4ARG (5), M4ARGLEN (5), &tm.tm_mon))
199 return;
200 if (!m4_numeric_arg (context, me, M4ARG (6), M4ARGLEN (6), &tm.tm_year))
201 return;
202 if (M4ARG (7) && !m4_numeric_arg (context, me, M4ARG (7), M4ARGLEN (7),
203 &tm.tm_isdst))
204 return;
206 t = mktime (&tm);
208 m4_shipout_int (obs, t);
210 #endif /* HAVE_MKTIME */
212 #if HAVE_STRFTIME
214 * strftime(FORMAT, SECONDS)
216 M4BUILTIN_HANDLER (strftime)
218 struct tm *tm;
219 time_t t;
220 char *buf;
221 int l;
223 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (2), M4ARGLEN (2),
224 &l))
225 return;
227 t = l;
228 tm = localtime (&t);
230 buf = (char *) obstack_alloc (obs, 1024);
231 l = strftime (buf, 1024, M4ARG (1), tm);
232 obstack_grow (obs, buf, l);
234 #endif /* HAVE_STRFTIME */