1 /* Parse a time duration and return a seconds count
2 Copyright (C) 2008-2011 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"
34 #define cch_t char const
47 #define SEC_PER_MIN 60
48 #define SEC_PER_HR (SEC_PER_MIN * 60)
49 #define SEC_PER_DAY (SEC_PER_HR * 24)
50 #define SEC_PER_WEEK (SEC_PER_DAY * 7)
51 #define SEC_PER_MONTH (SEC_PER_DAY * 30)
52 #define SEC_PER_YEAR (SEC_PER_DAY * 365)
54 #define TIME_MAX 0x7FFFFFFF
56 /* Wrapper around strtoul that does not require a cast. */
57 static unsigned long inline
58 str_const_to_ul (cch_t * str, cch_t ** ppz, int base)
60 return strtoul (str, (char **)ppz, base);
63 /* Wrapper around strtol that does not require a cast. */
65 str_const_to_l (cch_t * str, cch_t ** ppz, int base)
67 return strtol (str, (char **)ppz, base);
70 /* Returns BASE + VAL * SCALE, interpreting BASE = BAD_TIME
71 with errno set as an error situation, and returning BAD_TIME
72 with errno set in an error situation. */
74 scale_n_add (time_t base, time_t val, int scale)
83 if (val > TIME_MAX / scale)
90 if (base > TIME_MAX - val)
99 /* After a number HH has been parsed, parse subsequent :MM or :MM:SS. */
101 parse_hr_min_sec (time_t start, cch_t * pz)
107 /* For as long as our scanner pointer points to a colon *AND*
108 we've not looped before, then keep looping. (two iterations max) */
109 while ((*pz == ':') && (lpct++ <= 1))
111 unsigned long v = str_const_to_ul (pz+1, &pz, 10);
116 start = scale_n_add (v, start, 60);
122 /* allow for trailing spaces */
123 while (isspace ((unsigned char)*pz))
134 /* Parses a value and returns BASE + value * SCALE, interpreting
135 BASE = BAD_TIME with errno set as an error situation, and returning
136 BAD_TIME with errno set in an error situation. */
138 parse_scaled_value (time_t base, cch_t ** ppz, cch_t * endp, int scale)
143 if (base == BAD_TIME)
147 val = str_const_to_ul (pz, &pz, 10);
150 while (isspace ((unsigned char)*pz))
159 return scale_n_add (base, val, scale);
162 /* Parses the syntax YEAR-MONTH-DAY.
163 PS points into the string, after "YEAR", before "-MONTH-DAY". */
165 parse_year_month_day (cch_t * pz, cch_t * ps)
169 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
171 pz++; /* over the first '-' */
172 ps = strchr (pz, '-');
178 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
180 pz++; /* over the second '-' */
181 ps = pz + strlen (pz);
182 return parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
185 /* Parses the syntax YYYYMMDD. */
187 parse_yearmonthday (cch_t * in_pz)
193 if (strlen (in_pz) != 8)
199 memcpy (buf, in_pz, 4);
202 res = parse_scaled_value (0, &pz, buf + 4, SEC_PER_YEAR);
204 memcpy (buf, in_pz + 4, 2);
207 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MONTH);
209 memcpy (buf, in_pz + 6, 2);
212 return parse_scaled_value (res, &pz, buf + 2, SEC_PER_DAY);
215 /* Parses the syntax yy Y mm M ww W dd D. */
217 parse_YMWD (cch_t * pz)
220 cch_t * ps = strchr (pz, 'Y');
223 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
227 ps = strchr (pz, 'M');
230 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
234 ps = strchr (pz, 'W');
237 res = parse_scaled_value (res, &pz, ps, SEC_PER_WEEK);
241 ps = strchr (pz, 'D');
244 res = parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
248 while (isspace ((unsigned char)*pz))
259 /* Parses the syntax HH:MM:SS.
260 PS points into the string, after "HH", before ":MM:SS". */
262 parse_hour_minute_second (cch_t * pz, cch_t * ps)
266 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
269 ps = strchr (pz, ':');
276 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
279 ps = pz + strlen (pz);
280 return parse_scaled_value (res, &pz, ps, 1);
283 /* Parses the syntax HHMMSS. */
285 parse_hourminutesecond (cch_t * in_pz)
291 if (strlen (in_pz) != 6)
297 memcpy (buf, in_pz, 2);
300 res = parse_scaled_value (0, &pz, buf + 2, SEC_PER_HR);
302 memcpy (buf, in_pz + 2, 2);
305 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MIN);
307 memcpy (buf, in_pz + 4, 2);
310 return parse_scaled_value (res, &pz, buf + 2, 1);
313 /* Parses the syntax hh H mm M ss S. */
315 parse_HMS (cch_t * pz)
318 cch_t * ps = strchr (pz, 'H');
321 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
325 ps = strchr (pz, 'M');
328 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
332 ps = strchr (pz, 'S');
335 res = parse_scaled_value (res, &pz, ps, 1);
339 while (isspace ((unsigned char)*pz))
350 /* Parses a time (hours, minutes, seconds) specification in either syntax. */
352 parse_time (cch_t * pz)
360 ps = strchr (pz, ':');
363 res = parse_hour_minute_second (pz, ps);
367 * Try for a 'H', 'M' or 'S' suffix
369 else if (ps = strpbrk (pz, "HMS"),
372 /* Its a YYYYMMDD format: */
373 res = parse_hourminutesecond (pz);
377 res = parse_HMS (pz);
382 /* Returns a substring of the given string, with spaces at the beginning and at
383 the end destructively removed, per SNOBOL. */
387 /* trim leading white space */
388 while (isspace ((unsigned char)*pz))
391 /* trim trailing white space */
393 char * pe = pz + strlen (pz);
394 while ((pe > pz) && isspace ((unsigned char)pe[-1]))
403 * Parse the year/months/days of a time period
406 parse_period (cch_t * in_pz)
410 char * pz = strdup (in_pz);
420 pT = strchr (pz, 'T');
431 ps = strchr (pz, '-');
434 res = parse_year_month_day (pz, ps);
438 * Try for a 'Y', 'M' or 'D' suffix
440 else if (ps = strpbrk (pz, "YMWD"),
443 /* Its a YYYYMMDD format: */
444 res = parse_yearmonthday (pz);
448 res = parse_YMWD (pz);
450 if ((errno == 0) && (pT != NULL))
452 time_t val = parse_time (pT);
453 res = scale_n_add (res, val, 1);
461 parse_non_iso8601 (cch_t * pz)
463 whats_done_t whatd_we_do = NOTHING_IS_DONE;
471 val = str_const_to_l (pz, &pz, 10);
475 /* IF we find a colon, then we're going to have a seconds value.
476 We will not loop here any more. We cannot already have parsed
477 a minute value and if we've parsed an hour value, then the result
478 value has to be less than an hour. */
481 if (whatd_we_do >= MINUTE_IS_DONE)
484 val = parse_hr_min_sec (val, pz);
486 if ((whatd_we_do == HOUR_IS_DONE) && (val >= SEC_PER_HR))
489 return scale_n_add (res, val, 1);
495 /* Skip over white space following the number we just parsed. */
496 while (isspace ((unsigned char)*pz))
501 default: goto bad_time;
503 return scale_n_add (res, val, 1);
506 if (whatd_we_do >= YEAR_IS_DONE)
509 whatd_we_do = YEAR_IS_DONE;
513 if (whatd_we_do >= MONTH_IS_DONE)
515 mult = SEC_PER_MONTH;
516 whatd_we_do = MONTH_IS_DONE;
520 if (whatd_we_do >= WEEK_IS_DONE)
523 whatd_we_do = WEEK_IS_DONE;
527 if (whatd_we_do >= DAY_IS_DONE)
530 whatd_we_do = DAY_IS_DONE;
534 if (whatd_we_do >= HOUR_IS_DONE)
537 whatd_we_do = HOUR_IS_DONE;
541 if (whatd_we_do >= MINUTE_IS_DONE)
544 whatd_we_do = MINUTE_IS_DONE;
549 whatd_we_do = SECOND_IS_DONE;
553 res = scale_n_add (res, val, mult);
556 while (isspace ((unsigned char)*pz))
561 if (! isdigit ((unsigned char)*pz))
565 } while (whatd_we_do < SECOND_IS_DONE);
573 parse_duration (char const * pz)
575 while (isspace ((unsigned char)*pz))
581 return parse_period (pz + 1);
584 return parse_time (pz + 1);
587 if (isdigit ((unsigned char)*pz))
588 return parse_non_iso8601 (pz);
598 * c-file-style: "gnu"
599 * indent-tabs-mode: nil
601 * end of parse-duration.c */