1 /* Parse a time duration and return a seconds count
2 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
3 Written by Bruce Korb <bkorb@gnu.org>, 2008.
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 3 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/>. */
21 #include "parse-duration.h"
35 #define cch_t char const
48 #define SEC_PER_MIN 60
49 #define SEC_PER_HR (SEC_PER_MIN * 60)
50 #define SEC_PER_DAY (SEC_PER_HR * 24)
51 #define SEC_PER_WEEK (SEC_PER_DAY * 7)
52 #define SEC_PER_MONTH (SEC_PER_DAY * 30)
53 #define SEC_PER_YEAR (SEC_PER_DAY * 365)
55 #define TIME_MAX 0x7FFFFFFF
57 /* Wrapper around strtoul that does not require a cast. */
58 static unsigned long inline
59 str_const_to_ul (cch_t * str, cch_t ** ppz, int base)
61 return strtoul (str, (char **)ppz, base);
64 /* Wrapper around strtol that does not require a cast. */
66 str_const_to_l (cch_t * str, cch_t ** ppz, int base)
68 return strtol (str, (char **)ppz, base);
71 /* Returns BASE + VAL * SCALE, interpreting BASE = BAD_TIME
72 with errno set as an error situation, and returning BAD_TIME
73 with errno set in an error situation. */
75 scale_n_add (time_t base, time_t val, int scale)
84 if (val > TIME_MAX / scale)
91 if (base > TIME_MAX - val)
100 /* After a number HH has been parsed, parse subsequent :MM or :MM:SS. */
102 parse_hr_min_sec (time_t start, cch_t * pz)
108 /* For as long as our scanner pointer points to a colon *AND*
109 we've not looped before, then keep looping. (two iterations max) */
110 while ((*pz == ':') && (lpct++ <= 1))
112 unsigned long v = str_const_to_ul (pz+1, &pz, 10);
117 start = scale_n_add (v, start, 60);
123 /* allow for trailing spaces */
124 while (isspace ((unsigned char)*pz))
135 /* Parses a value and returns BASE + value * SCALE, interpreting
136 BASE = BAD_TIME with errno set as an error situation, and returning
137 BAD_TIME with errno set in an error situation. */
139 parse_scaled_value (time_t base, cch_t ** ppz, cch_t * endp, int scale)
144 if (base == BAD_TIME)
148 val = str_const_to_ul (pz, &pz, 10);
151 while (isspace ((unsigned char)*pz))
160 return scale_n_add (base, val, scale);
163 /* Parses the syntax YEAR-MONTH-DAY.
164 PS points into the string, after "YEAR", before "-MONTH-DAY". */
166 parse_year_month_day (cch_t * pz, cch_t * ps)
170 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
172 pz++; /* over the first '-' */
173 ps = strchr (pz, '-');
179 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
181 pz++; /* over the second '-' */
182 ps = pz + strlen (pz);
183 return parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
186 /* Parses the syntax YYYYMMDD. */
188 parse_yearmonthday (cch_t * in_pz)
194 if (strlen (in_pz) != 8)
200 memcpy (buf, in_pz, 4);
203 res = parse_scaled_value (0, &pz, buf + 4, SEC_PER_YEAR);
205 memcpy (buf, in_pz + 4, 2);
208 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MONTH);
210 memcpy (buf, in_pz + 6, 2);
213 return parse_scaled_value (res, &pz, buf + 2, SEC_PER_DAY);
216 /* Parses the syntax yy Y mm M ww W dd D. */
218 parse_YMWD (cch_t * pz)
221 cch_t * ps = strchr (pz, 'Y');
224 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
228 ps = strchr (pz, 'M');
231 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
235 ps = strchr (pz, 'W');
238 res = parse_scaled_value (res, &pz, ps, SEC_PER_WEEK);
242 ps = strchr (pz, 'D');
245 res = parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
249 while (isspace ((unsigned char)*pz))
260 /* Parses the syntax HH:MM:SS.
261 PS points into the string, after "HH", before ":MM:SS". */
263 parse_hour_minute_second (cch_t * pz, cch_t * ps)
267 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
270 ps = strchr (pz, ':');
277 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
280 ps = pz + strlen (pz);
281 return parse_scaled_value (res, &pz, ps, 1);
284 /* Parses the syntax HHMMSS. */
286 parse_hourminutesecond (cch_t * in_pz)
292 if (strlen (in_pz) != 6)
298 memcpy (buf, in_pz, 2);
301 res = parse_scaled_value (0, &pz, buf + 2, SEC_PER_HR);
303 memcpy (buf, in_pz + 2, 2);
306 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MIN);
308 memcpy (buf, in_pz + 4, 2);
311 return parse_scaled_value (res, &pz, buf + 2, 1);
314 /* Parses the syntax hh H mm M ss S. */
316 parse_HMS (cch_t * pz)
319 cch_t * ps = strchr (pz, 'H');
322 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
326 ps = strchr (pz, 'M');
329 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
333 ps = strchr (pz, 'S');
336 res = parse_scaled_value (res, &pz, ps, 1);
340 while (isspace ((unsigned char)*pz))
351 /* Parses a time (hours, minutes, seconds) specification in either syntax. */
353 parse_time (cch_t * pz)
361 ps = strchr (pz, ':');
364 res = parse_hour_minute_second (pz, ps);
368 * Try for a 'H', 'M' or 'S' suffix
370 else if (ps = strpbrk (pz, "HMS"),
373 /* Its a YYYYMMDD format: */
374 res = parse_hourminutesecond (pz);
378 res = parse_HMS (pz);
383 /* Returns a substring of the given string, with spaces at the beginning and at
384 the end destructively removed. */
388 /* trim leading white space */
389 while (isspace ((unsigned char)*pz))
392 /* trim trailing white space */
394 char * pe = pz + strlen (pz);
395 while ((pe > pz) && isspace ((unsigned char)pe[-1]))
404 * Parse the year/months/days of a time period
407 parse_period (cch_t * in_pz)
409 char * pz = xstrdup (in_pz);
410 char * pT = strchr (pz, 'T');
425 ps = strchr (pz, '-');
428 res = parse_year_month_day (pz, ps);
432 * Try for a 'Y', 'M' or 'D' suffix
434 else if (ps = strpbrk (pz, "YMWD"),
437 /* Its a YYYYMMDD format: */
438 res = parse_yearmonthday (pz);
442 res = parse_YMWD (pz);
444 if ((errno == 0) && (pT != NULL))
446 time_t val = parse_time (pT);
447 res = scale_n_add (res, val, 1);
455 parse_non_iso8601 (cch_t * pz)
457 whats_done_t whatd_we_do = NOTHING_IS_DONE;
465 val = str_const_to_l (pz, &pz, 10);
469 /* IF we find a colon, then we're going to have a seconds value.
470 We will not loop here any more. We cannot already have parsed
471 a minute value and if we've parsed an hour value, then the result
472 value has to be less than an hour. */
475 if (whatd_we_do >= MINUTE_IS_DONE)
478 val = parse_hr_min_sec (val, pz);
480 if ((whatd_we_do == HOUR_IS_DONE) && (val >= SEC_PER_HR))
483 return scale_n_add (res, val, 1);
489 /* Skip over white space following the number we just parsed. */
490 while (isspace ((unsigned char)*pz))
495 default: goto bad_time;
497 return scale_n_add (res, val, 1);
500 if (whatd_we_do >= YEAR_IS_DONE)
503 whatd_we_do = YEAR_IS_DONE;
507 if (whatd_we_do >= MONTH_IS_DONE)
509 mult = SEC_PER_MONTH;
510 whatd_we_do = MONTH_IS_DONE;
514 if (whatd_we_do >= WEEK_IS_DONE)
517 whatd_we_do = WEEK_IS_DONE;
521 if (whatd_we_do >= DAY_IS_DONE)
524 whatd_we_do = DAY_IS_DONE;
528 if (whatd_we_do >= HOUR_IS_DONE)
531 whatd_we_do = HOUR_IS_DONE;
535 if (whatd_we_do >= MINUTE_IS_DONE)
538 whatd_we_do = MINUTE_IS_DONE;
543 whatd_we_do = SECOND_IS_DONE;
547 res = scale_n_add (res, val, mult);
550 while (isspace ((unsigned char)*pz))
555 if (! isdigit ((unsigned char)*pz))
559 } while (whatd_we_do < SECOND_IS_DONE);
567 parse_duration (char const * pz)
569 while (isspace ((unsigned char)*pz))
575 return parse_period (pz + 1);
578 return parse_time (pz + 1);
581 if (isdigit ((unsigned char)*pz))
582 return parse_non_iso8601 (pz);
592 * c-file-style: "gnu"
593 * indent-tabs-mode: nil
595 * end of parse-duration.c */