Allow user to choose which sed to use in testsuite.
[m4.git] / modules / time.c
bloba922a6775df922dada033fde0f31dfbc9f91ce75
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1999, 2000, 2001, 2006, 2007, 2008 Free Software
3 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), &i);
108 t = i;
110 else
111 t = time (0L);
113 s = ctime (&t);
114 obstack_grow (obs, s, 24);
117 static void
118 format_tm (m4_obstack *obs, struct tm *tm)
120 m4_shipout_int (obs, tm->tm_sec);
121 obstack_1grow (obs, ',');
123 m4_shipout_int (obs, tm->tm_min);
124 obstack_1grow (obs, ',');
126 m4_shipout_int (obs, tm->tm_hour);
127 obstack_1grow (obs, ',');
129 m4_shipout_int (obs, tm->tm_mday);
130 obstack_1grow (obs, ',');
132 m4_shipout_int (obs, tm->tm_mon);
133 obstack_1grow (obs, ',');
135 m4_shipout_int (obs, tm->tm_year);
136 obstack_1grow (obs, ',');
138 m4_shipout_int (obs, tm->tm_wday);
139 obstack_1grow (obs, ',');
141 m4_shipout_int (obs, tm->tm_yday);
142 obstack_1grow (obs, ',');
144 m4_shipout_int (obs, tm->tm_isdst);
148 * gmtime(SECONDS)
150 M4BUILTIN_HANDLER (gmtime)
152 time_t t;
153 int i;
155 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (1), &i))
156 return;
158 t = i;
159 format_tm (obs, gmtime (&t));
163 * localtime(SECONDS)
165 M4BUILTIN_HANDLER (localtime)
167 time_t t;
168 int i;
170 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (1), &i))
171 return;
173 t = i;
174 format_tm (obs, localtime (&t));
177 #if HAVE_MKTIME
179 * mktime(SEC, MIN, HOUR, MDAY, MONTH, YEAR, [ISDST])
181 M4BUILTIN_HANDLER (mktime)
183 const m4_call_info *me = m4_arg_info (argv);
184 struct tm tm;
185 time_t t;
187 if (!m4_numeric_arg (context, me, M4ARG (1), &tm.tm_sec))
188 return;
189 if (!m4_numeric_arg (context, me, M4ARG (2), &tm.tm_min))
190 return;
191 if (!m4_numeric_arg (context, me, M4ARG (3), &tm.tm_hour))
192 return;
193 if (!m4_numeric_arg (context, me, M4ARG (4), &tm.tm_mday))
194 return;
195 if (!m4_numeric_arg (context, me, M4ARG (5), &tm.tm_mon))
196 return;
197 if (!m4_numeric_arg (context, me, M4ARG (6), &tm.tm_year))
198 return;
199 if (M4ARG (7) && !m4_numeric_arg (context, me, M4ARG (7), &tm.tm_isdst))
200 return;
202 t = mktime (&tm);
204 m4_shipout_int (obs, t);
206 #endif /* HAVE_MKTIME */
208 #if HAVE_STRFTIME
210 * strftime(FORMAT, SECONDS)
212 M4BUILTIN_HANDLER (strftime)
214 struct tm *tm;
215 time_t t;
216 char *buf;
217 int l;
219 if (!m4_numeric_arg (context, m4_arg_info (argv), M4ARG (2), &l))
220 return;
222 t = l;
223 tm = localtime (&t);
225 buf = (char *) obstack_alloc (obs, 1024);
226 l = strftime (buf, 1024, M4ARG (1), tm);
227 obstack_grow (obs, buf, l);
229 #endif /* HAVE_STRFTIME */