1 /* Parse a time duration and return a seconds count
2 Copyright (C) 2008-2015 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "parse-duration.h"
36 #define cch_t char const
49 #define SEC_PER_MIN 60
50 #define SEC_PER_HR (SEC_PER_MIN * 60)
51 #define SEC_PER_DAY (SEC_PER_HR * 24)
52 #define SEC_PER_WEEK (SEC_PER_DAY * 7)
53 #define SEC_PER_MONTH (SEC_PER_DAY * 30)
54 #define SEC_PER_YEAR (SEC_PER_DAY * 365)
57 #define MAX_DURATION TYPE_MAXIMUM(time_t)
59 /* Wrapper around strtoul that does not require a cast. */
61 str_const_to_ul (cch_t * str, cch_t ** ppz, int base)
64 int rv = strtoul (str, &pz, base);
69 /* Wrapper around strtol that does not require a cast. */
71 str_const_to_l (cch_t * str, cch_t ** ppz, int base)
74 int rv = strtol (str, &pz, base);
79 /* Returns BASE + VAL * SCALE, interpreting BASE = BAD_TIME
80 with errno set as an error situation, and returning BAD_TIME
81 with errno set in an error situation. */
83 scale_n_add (time_t base, time_t val, int scale)
92 if (val > MAX_DURATION / scale)
99 if (base > MAX_DURATION - val)
108 /* After a number HH has been parsed, parse subsequent :MM or :MM:SS. */
110 parse_hr_min_sec (time_t start, cch_t * pz)
116 /* For as long as our scanner pointer points to a colon *AND*
117 we've not looped before, then keep looping. (two iterations max) */
118 while ((*pz == ':') && (lpct++ <= 1))
120 unsigned long v = str_const_to_ul (pz+1, &pz, 10);
125 start = scale_n_add (v, start, 60);
131 /* allow for trailing spaces */
132 while (isspace ((unsigned char)*pz))
143 /* Parses a value and returns BASE + value * SCALE, interpreting
144 BASE = BAD_TIME with errno set as an error situation, and returning
145 BAD_TIME with errno set in an error situation. */
147 parse_scaled_value (time_t base, cch_t ** ppz, cch_t * endp, int scale)
152 if (base == BAD_TIME)
156 val = str_const_to_ul (pz, &pz, 10);
159 while (isspace ((unsigned char)*pz))
168 return scale_n_add (base, val, scale);
171 /* Parses the syntax YEAR-MONTH-DAY.
172 PS points into the string, after "YEAR", before "-MONTH-DAY". */
174 parse_year_month_day (cch_t * pz, cch_t * ps)
178 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
180 pz++; /* over the first '-' */
181 ps = strchr (pz, '-');
187 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
189 pz++; /* over the second '-' */
190 ps = pz + strlen (pz);
191 return parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
194 /* Parses the syntax YYYYMMDD. */
196 parse_yearmonthday (cch_t * in_pz)
202 if (strlen (in_pz) != 8)
208 memcpy (buf, in_pz, 4);
211 res = parse_scaled_value (0, &pz, buf + 4, SEC_PER_YEAR);
213 memcpy (buf, in_pz + 4, 2);
216 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MONTH);
218 memcpy (buf, in_pz + 6, 2);
221 return parse_scaled_value (res, &pz, buf + 2, SEC_PER_DAY);
224 /* Parses the syntax yy Y mm M ww W dd D. */
226 parse_YMWD (cch_t * pz)
229 cch_t * ps = strchr (pz, 'Y');
232 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
236 ps = strchr (pz, 'M');
239 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
243 ps = strchr (pz, 'W');
246 res = parse_scaled_value (res, &pz, ps, SEC_PER_WEEK);
250 ps = strchr (pz, 'D');
253 res = parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
257 while (isspace ((unsigned char)*pz))
268 /* Parses the syntax HH:MM:SS.
269 PS points into the string, after "HH", before ":MM:SS". */
271 parse_hour_minute_second (cch_t * pz, cch_t * ps)
275 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
278 ps = strchr (pz, ':');
285 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
288 ps = pz + strlen (pz);
289 return parse_scaled_value (res, &pz, ps, 1);
292 /* Parses the syntax HHMMSS. */
294 parse_hourminutesecond (cch_t * in_pz)
300 if (strlen (in_pz) != 6)
306 memcpy (buf, in_pz, 2);
309 res = parse_scaled_value (0, &pz, buf + 2, SEC_PER_HR);
311 memcpy (buf, in_pz + 2, 2);
314 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MIN);
316 memcpy (buf, in_pz + 4, 2);
319 return parse_scaled_value (res, &pz, buf + 2, 1);
322 /* Parses the syntax hh H mm M ss S. */
324 parse_HMS (cch_t * pz)
327 cch_t * ps = strchr (pz, 'H');
330 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
334 ps = strchr (pz, 'M');
337 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
341 ps = strchr (pz, 'S');
344 res = parse_scaled_value (res, &pz, ps, 1);
348 while (isspace ((unsigned char)*pz))
359 /* Parses a time (hours, minutes, seconds) specification in either syntax. */
361 parse_time (cch_t * pz)
369 ps = strchr (pz, ':');
372 res = parse_hour_minute_second (pz, ps);
376 * Try for a 'H', 'M' or 'S' suffix
378 else if (ps = strpbrk (pz, "HMS"),
381 /* Its a YYYYMMDD format: */
382 res = parse_hourminutesecond (pz);
386 res = parse_HMS (pz);
391 /* Returns a substring of the given string, with spaces at the beginning and at
392 the end destructively removed, per SNOBOL. */
396 /* trim leading white space */
397 while (isspace ((unsigned char)*pz))
400 /* trim trailing white space */
402 char * pe = pz + strlen (pz);
403 while ((pe > pz) && isspace ((unsigned char)pe[-1]))
412 * Parse the year/months/days of a time period
415 parse_period (cch_t * in_pz)
419 char * pz = strdup (in_pz);
429 pT = strchr (pz, 'T');
440 ps = strchr (pz, '-');
443 res = parse_year_month_day (pz, ps);
447 * Try for a 'Y', 'M' or 'D' suffix
449 else if (ps = strpbrk (pz, "YMWD"),
452 /* Its a YYYYMMDD format: */
453 res = parse_yearmonthday (pz);
457 res = parse_YMWD (pz);
459 if ((errno == 0) && (pT != NULL))
461 time_t val = parse_time (pT);
462 res = scale_n_add (res, val, 1);
470 parse_non_iso8601 (cch_t * pz)
472 whats_done_t whatd_we_do = NOTHING_IS_DONE;
480 val = str_const_to_l (pz, &pz, 10);
484 /* IF we find a colon, then we're going to have a seconds value.
485 We will not loop here any more. We cannot already have parsed
486 a minute value and if we've parsed an hour value, then the result
487 value has to be less than an hour. */
490 if (whatd_we_do >= MINUTE_IS_DONE)
493 val = parse_hr_min_sec (val, pz);
495 if ((whatd_we_do == HOUR_IS_DONE) && (val >= SEC_PER_HR))
498 return scale_n_add (res, val, 1);
504 /* Skip over white space following the number we just parsed. */
505 while (isspace ((unsigned char)*pz))
510 default: goto bad_time;
512 return scale_n_add (res, val, 1);
515 if (whatd_we_do >= YEAR_IS_DONE)
518 whatd_we_do = YEAR_IS_DONE;
522 if (whatd_we_do >= MONTH_IS_DONE)
524 mult = SEC_PER_MONTH;
525 whatd_we_do = MONTH_IS_DONE;
529 if (whatd_we_do >= WEEK_IS_DONE)
532 whatd_we_do = WEEK_IS_DONE;
536 if (whatd_we_do >= DAY_IS_DONE)
539 whatd_we_do = DAY_IS_DONE;
543 if (whatd_we_do >= HOUR_IS_DONE)
546 whatd_we_do = HOUR_IS_DONE;
550 if (whatd_we_do >= MINUTE_IS_DONE)
553 whatd_we_do = MINUTE_IS_DONE;
558 whatd_we_do = SECOND_IS_DONE;
562 res = scale_n_add (res, val, mult);
565 while (isspace ((unsigned char)*pz))
570 if (! isdigit ((unsigned char)*pz))
574 } while (whatd_we_do < SECOND_IS_DONE);
582 parse_duration (char const * pz)
584 while (isspace ((unsigned char)*pz))
590 return parse_period (pz + 1);
593 return parse_time (pz + 1);
596 if (isdigit ((unsigned char)*pz))
597 return parse_non_iso8601 (pz);
607 * c-file-style: "gnu"
608 * indent-tabs-mode: nil
610 * end of parse-duration.c */