1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1999-2001, 2006-2010, 2013-2014 Free Software
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/>.
24 # include <sys/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. */
32 # include <m4/m4module.h>
34 # include "m4private.h"
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)
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)
75 { NULL
, NULL
, 0, 0, 0 },
80 include_time (m4
*context
, m4_module
*module
, m4_obstack
*obs
)
82 m4_install_builtins (context
, module
, m4_builtin_table
);
90 M4BUILTIN_HANDLER (currenttime
)
97 l
= sprintf (buf
, "%ld", now
);
99 obstack_grow (obs
, buf
, l
);
105 M4BUILTIN_HANDLER (ctime
)
113 m4_numeric_arg (context
, m4_arg_info (argv
), M4ARG (1), M4ARGLEN (1),
121 obstack_grow (obs
, s
, 24);
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
);
157 M4BUILTIN_HANDLER (gmtime
)
162 if (!m4_numeric_arg (context
, m4_arg_info (argv
), M4ARG (1), M4ARGLEN (1),
167 format_tm (obs
, gmtime (&t
));
173 M4BUILTIN_HANDLER (localtime
)
178 if (!m4_numeric_arg (context
, m4_arg_info (argv
), M4ARG (1), M4ARGLEN (1),
183 format_tm (obs
, localtime (&t
));
188 * mktime(SEC, MIN, HOUR, MDAY, MONTH, YEAR, [ISDST])
190 M4BUILTIN_HANDLER (mktime
)
192 const m4_call_info
*me
= m4_arg_info (argv
);
196 if (!m4_numeric_arg (context
, me
, M4ARG (1), M4ARGLEN (1), &tm
.tm_sec
))
198 if (!m4_numeric_arg (context
, me
, M4ARG (2), M4ARGLEN (2), &tm
.tm_min
))
200 if (!m4_numeric_arg (context
, me
, M4ARG (3), M4ARGLEN (3), &tm
.tm_hour
))
202 if (!m4_numeric_arg (context
, me
, M4ARG (4), M4ARGLEN (4), &tm
.tm_mday
))
204 if (!m4_numeric_arg (context
, me
, M4ARG (5), M4ARGLEN (5), &tm
.tm_mon
))
206 if (!m4_numeric_arg (context
, me
, M4ARG (6), M4ARGLEN (6), &tm
.tm_year
))
208 if (M4ARG (7) && !m4_numeric_arg (context
, me
, M4ARG (7), M4ARGLEN (7),
214 m4_shipout_int (obs
, t
);
216 #endif /* HAVE_MKTIME */
220 * strftime(FORMAT, SECONDS)
222 M4BUILTIN_HANDLER (strftime
)
229 if (!m4_numeric_arg (context
, m4_arg_info (argv
), M4ARG (2), M4ARGLEN (2),
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 */