2 * parse time string - user friendly date and time parser
3 * Copyright © 2012 Jani Nikula
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Author: Jani Nikula <jani@nikula.org>
28 #include "parse-time-string.h"
30 #define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
32 static const char *parse_time_error_strings[] = {
33 [PARSE_TIME_OK] = "OK",
34 [PARSE_TIME_ERR] = "ERR",
35 [PARSE_TIME_ERR_LIB] = "LIB",
36 [PARSE_TIME_ERR_ALREADYSET] = "ALREADYSET",
37 [PARSE_TIME_ERR_FORMAT] = "FORMAT",
38 [PARSE_TIME_ERR_DATEFORMAT] = "DATEFORMAT",
39 [PARSE_TIME_ERR_TIMEFORMAT] = "TIMEFORMAT",
40 [PARSE_TIME_ERR_INVALIDDATE] = "INVALIDDATE",
41 [PARSE_TIME_ERR_INVALIDTIME] = "INVALIDTIME",
42 [PARSE_TIME_ERR_KEYWORD] = "KEYWORD",
46 parse_time_strerror (unsigned int errnum)
48 if (errnum < ARRAY_SIZE (parse_time_error_strings))
49 return parse_time_error_strings[errnum];
55 * concat argv[start]...argv[end - 1], separating them by a single
56 * space, to a malloced string
59 concat_args (int start, int end, char *argv[])
65 for (i = start; i < end; i++)
66 len += strlen (argv[i]) + 1;
74 for (i = start; i < end; i++) {
83 #define DEFAULT_FORMAT "%a %b %d %T %z %Y"
86 usage (const char *name)
88 printf ("Usage: %s [options ...] [<date/time>]\n\n", name);
90 "Parse <date/time> and display it in given format. If <date/time> is\n"
91 "not given, parse each line in stdin according to:\n\n"
92 " <date/time> [(==>|==_>|==^>|==^^>)<ignored>] [#<comment>]\n\n"
93 "and produce output:\n\n"
94 " <date/time> (==>|==_>|==^>|==^^>) <time in --format=FMT> [#<comment>]\n\n"
95 "preserving whitespace and comment in input. The operators ==>, ==_>,\n"
96 "==^>, and ==^^> define rounding as no rounding, round down, round up\n"
97 "inclusive, and round up, respectively.\n\n"
99 " -f, --format=FMT output format, FMT according to strftime(3)\n"
100 " (default: \"%s\")\n"
101 " -r, --ref=N use N seconds since epoch as reference time\n"
103 " -u, --^ round result up inclusive (default: no rounding)\n"
104 " -U, --^^ round result up (default: no rounding)\n"
105 " -d, --_ round result down (default: no rounding)\n"
106 " -h, --help print this help\n",
111 const char *operator;
114 { "==>", PARSE_TIME_NO_ROUND },
115 { "==_>", PARSE_TIME_ROUND_DOWN },
116 { "==^>", PARSE_TIME_ROUND_UP_INCLUSIVE },
117 { "==^^>", PARSE_TIME_ROUND_UP },
121 find_operator_in_string (char *str, char **ptr, int *round)
123 const char *oper = NULL;
126 for (i = 0; i < ARRAY_SIZE (operators); i++) {
127 char *p = strstr (str, operators[i].operator);
130 *round = operators[i].round;
134 oper = operators[i].operator;
143 get_operator (int round)
145 const char *oper = NULL;
148 for (i = 0; i < ARRAY_SIZE(operators); i++) {
149 if (round == operators[i].round) {
150 oper = operators[i].operator;
159 parse_stdin (FILE *infile, time_t *ref, int round, const char *format)
169 while ((len = getline (&input, &inputsize, infile)) != -1) {
173 /* trail is trailing whitespace and (optional) comment */
174 trail = strchr (input, '#');
178 while (trail > input && isspace ((unsigned char) *(trail-1)))
181 if (trail == input) {
182 printf ("%s", input);
186 tmp = strdup (trail);
188 fprintf (stderr, "strdup() failed\n");
195 oper = find_operator_in_string (input, &tmp, &round);
199 oper = get_operator (round);
203 r = parse_time_string (input, &t, ref, round);
205 if (!localtime_r (&t, &tm)) {
206 fprintf (stderr, "localtime_r() failed\n");
211 strftime (result, sizeof (result), format, &tm);
213 const char *errstr = parse_time_strerror (r);
215 snprintf (result, sizeof (result), "ERROR: %s", errstr);
217 snprintf (result, sizeof (result), "ERROR: %d", r);
220 printf ("%s%s %s%s", input, oper, result, trail);
230 main (int argc, char *argv[])
238 int round = PARSE_TIME_NO_ROUND;
240 const char *format = DEFAULT_FORMAT;
241 struct option options[] = {
242 { "help", no_argument, NULL, 'h' },
243 { "^", no_argument, NULL, 'u' },
244 { "^^", no_argument, NULL, 'U' },
245 { "_", no_argument, NULL, 'd' },
246 { "format", required_argument, NULL, 'f' },
247 { "ref", required_argument, NULL, 'r' },
248 { NULL, 0, NULL, 0 },
254 c = getopt_long (argc, argv, "huUdf:r:", options, NULL);
264 round = PARSE_TIME_ROUND_UP_INCLUSIVE;
267 round = PARSE_TIME_ROUND_UP;
270 round = PARSE_TIME_ROUND_DOWN;
273 /* specify now in seconds since epoch */
274 now = (time_t) strtol (optarg, NULL, 10);
275 if (now >= (time_t) 0)
287 return parse_stdin (stdin, nowp, round, format);
289 argstr = concat_args (optind, argc, argv);
293 r = parse_time_string (argstr, &result, nowp, round);
298 const char *errstr = parse_time_strerror (r);
300 fprintf (stderr, "ERROR: %s\n", errstr);
302 fprintf (stderr, "ERROR: %d\n", r);
307 if (!localtime_r (&result, &tm))
310 strftime (buf, sizeof (buf), format, &tm);
311 printf ("%s\n", buf);