modules: huge simplification of module loading without libltdl.
[m4/ericb.git] / modules / time.c
blob5c9c2ce65aa0b0862c0cf881641c9f1f2e7e0a9a
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1999-2001, 2006-2010, 2013 Free Software Foundation,
3 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 /* function macros blind side minargs maxargs */
38 #define builtin_functions \
39 BUILTIN (currenttime, false, false, false, 0, 0 ) \
40 BUILTIN (ctime, false, false, false, 0, 1 ) \
41 BUILTIN (gmtime, false, true, false, 1, 1 ) \
42 BUILTIN (localtime, false, true, false, 1, 1 ) \
44 #define mktime_functions \
45 BUILTIN (mktime, false, true, false, 6, 7 ) \
47 #define strftime_functions \
48 BUILTIN (strftime, false, true, false, 2, 2 ) \
51 #define BUILTIN(handler, macros, blind, side, min, max) M4BUILTIN (handler)
52 builtin_functions
53 # if HAVE_MKTIME
54 mktime_functions
55 # endif
56 # if HAVE_STRFTIME
57 strftime_functions
58 # endif
59 #undef BUILTIN
61 static const m4_builtin m4_builtin_table[] =
63 #define BUILTIN(handler, macros, blind, side, min, max) \
64 M4BUILTIN_ENTRY (handler, #handler, macros, blind, side, min, max)
66 builtin_functions
67 # if HAVE_MKTIME
68 mktime_functions
69 # endif
70 # if HAVE_STRFTIME
71 strftime_functions
72 # endif
73 #undef BUILTIN
75 { NULL, NULL, 0, 0, 0 },
79 void
80 include_time (m4 *context, m4_module *module, m4_obstack *obs)
82 m4_install_builtins (context, module, m4_builtin_table);
87 /**
88 * currenttime()
89 **/
90 M4BUILTIN_HANDLER (currenttime)
92 char buf[64];
93 time_t now;
94 int l;
96 now = time (0L);
97 l = sprintf (buf, "%ld", now);
99 obstack_grow (obs, buf, l);
103 * ctime([SECONDS])
105 M4BUILTIN_HANDLER (ctime)
107 time_t t;
108 int i;
109 const char *s;
111 if (argc == 2)
113 m4_numeric_arg (context, m4_arg_info (argv), M4ARG (1), M4ARGLEN (1),
114 &i);
115 t = i;
117 else
118 t = time (0L);
120 s = ctime (&t);
121 obstack_grow (obs, s, 24);
124 static void
125 format_tm (m4_obstack *obs, struct tm *tm)
127 m4_shipout_int (obs, tm->tm_sec);
128 obstack_1grow (obs, ',');
130 m4_shipout_int (obs, tm->tm_min);
131 obstack_1grow (obs, ',');
133 m4_shipout_int (obs, tm->tm_hour);
134 obstack_1grow (obs, ',');
136 m4_shipout_int (obs, tm->tm_mday);
137 obstack_1grow (obs, ',');
139 m4_shipout_int (obs, tm->tm_mon);
140 obstack_1grow (obs, ',');
142 m4_shipout_int (obs, tm->tm_year);
143 obstack_1grow (obs, ',');
145 m4_shipout_int (obs, tm->tm_wday);
146 obstack_1grow (obs, ',');
148 m4_shipout_int (obs, tm->tm_yday);
149 obstack_1grow (obs, ',');
151 m4_shipout_int (obs, tm->tm_isdst);
155 * gmtime(SECONDS)
157 M4BUILTIN_HANDLER (gmtime)
159 time_t t;
160 int i;
162 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (1), M4ARGLEN (1),
163 &i))
164 return;
166 t = i;
167 format_tm (obs, gmtime (&t));
171 * localtime(SECONDS)
173 M4BUILTIN_HANDLER (localtime)
175 time_t t;
176 int i;
178 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (1), M4ARGLEN (1),
179 &i))
180 return;
182 t = i;
183 format_tm (obs, localtime (&t));
186 #if HAVE_MKTIME
188 * mktime(SEC, MIN, HOUR, MDAY, MONTH, YEAR, [ISDST])
190 M4BUILTIN_HANDLER (mktime)
192 const m4_call_info *me = m4_arg_info (argv);
193 struct tm tm;
194 time_t t;
196 if (!m4_numeric_arg (context, me, M4ARG (1), M4ARGLEN (1), &tm.tm_sec))
197 return;
198 if (!m4_numeric_arg (context, me, M4ARG (2), M4ARGLEN (2), &tm.tm_min))
199 return;
200 if (!m4_numeric_arg (context, me, M4ARG (3), M4ARGLEN (3), &tm.tm_hour))
201 return;
202 if (!m4_numeric_arg (context, me, M4ARG (4), M4ARGLEN (4), &tm.tm_mday))
203 return;
204 if (!m4_numeric_arg (context, me, M4ARG (5), M4ARGLEN (5), &tm.tm_mon))
205 return;
206 if (!m4_numeric_arg (context, me, M4ARG (6), M4ARGLEN (6), &tm.tm_year))
207 return;
208 if (M4ARG (7) && !m4_numeric_arg (context, me, M4ARG (7), M4ARGLEN (7),
209 &tm.tm_isdst))
210 return;
212 t = mktime (&tm);
214 m4_shipout_int (obs, t);
216 #endif /* HAVE_MKTIME */
218 #if HAVE_STRFTIME
220 * strftime(FORMAT, SECONDS)
222 M4BUILTIN_HANDLER (strftime)
224 struct tm *tm;
225 time_t t;
226 char *buf;
227 int l;
229 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (2), M4ARGLEN (2),
230 &l))
231 return;
233 t = l;
234 tm = localtime (&t);
236 buf = (char *) obstack_alloc (obs, 1024);
237 l = strftime (buf, 1024, M4ARG (1), tm);
238 obstack_grow (obs, buf, l);
240 #endif /* HAVE_STRFTIME */