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)
63 return strtoul (str, (char **)ppz, base);
66 /* Wrapper around strtol that does not require a cast. */
68 str_const_to_l (cch_t * str, cch_t ** ppz, int base)
70 return strtol (str, (char **)ppz, base);
73 /* Returns BASE + VAL * SCALE, interpreting BASE = BAD_TIME
74 with errno set as an error situation, and returning BAD_TIME
75 with errno set in an error situation. */
77 scale_n_add (time_t base, time_t val, int scale)
86 if (val > MAX_DURATION / scale)
93 if (base > MAX_DURATION - val)
102 /* After a number HH has been parsed, parse subsequent :MM or :MM:SS. */
104 parse_hr_min_sec (time_t start, cch_t * pz)
110 /* For as long as our scanner pointer points to a colon *AND*
111 we've not looped before, then keep looping. (two iterations max) */
112 while ((*pz == ':') && (lpct++ <= 1))
114 unsigned long v = str_const_to_ul (pz+1, &pz, 10);
119 start = scale_n_add (v, start, 60);
125 /* allow for trailing spaces */
126 while (isspace ((unsigned char)*pz))
137 /* Parses a value and returns BASE + value * SCALE, interpreting
138 BASE = BAD_TIME with errno set as an error situation, and returning
139 BAD_TIME with errno set in an error situation. */
141 parse_scaled_value (time_t base, cch_t ** ppz, cch_t * endp, int scale)
146 if (base == BAD_TIME)
150 val = str_const_to_ul (pz, &pz, 10);
153 while (isspace ((unsigned char)*pz))
162 return scale_n_add (base, val, scale);
165 /* Parses the syntax YEAR-MONTH-DAY.
166 PS points into the string, after "YEAR", before "-MONTH-DAY". */
168 parse_year_month_day (cch_t * pz, cch_t * ps)
172 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
174 pz++; /* over the first '-' */
175 ps = strchr (pz, '-');
181 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
183 pz++; /* over the second '-' */
184 ps = pz + strlen (pz);
185 return parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
188 /* Parses the syntax YYYYMMDD. */
190 parse_yearmonthday (cch_t * in_pz)
196 if (strlen (in_pz) != 8)
202 memcpy (buf, in_pz, 4);
205 res = parse_scaled_value (0, &pz, buf + 4, SEC_PER_YEAR);
207 memcpy (buf, in_pz + 4, 2);
210 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MONTH);
212 memcpy (buf, in_pz + 6, 2);
215 return parse_scaled_value (res, &pz, buf + 2, SEC_PER_DAY);
218 /* Parses the syntax yy Y mm M ww W dd D. */
220 parse_YMWD (cch_t * pz)
223 cch_t * ps = strchr (pz, 'Y');
226 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
230 ps = strchr (pz, 'M');
233 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
237 ps = strchr (pz, 'W');
240 res = parse_scaled_value (res, &pz, ps, SEC_PER_WEEK);
244 ps = strchr (pz, 'D');
247 res = parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
251 while (isspace ((unsigned char)*pz))
262 /* Parses the syntax HH:MM:SS.
263 PS points into the string, after "HH", before ":MM:SS". */
265 parse_hour_minute_second (cch_t * pz, cch_t * ps)
269 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
272 ps = strchr (pz, ':');
279 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
282 ps = pz + strlen (pz);
283 return parse_scaled_value (res, &pz, ps, 1);
286 /* Parses the syntax HHMMSS. */
288 parse_hourminutesecond (cch_t * in_pz)
294 if (strlen (in_pz) != 6)
300 memcpy (buf, in_pz, 2);
303 res = parse_scaled_value (0, &pz, buf + 2, SEC_PER_HR);
305 memcpy (buf, in_pz + 2, 2);
308 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MIN);
310 memcpy (buf, in_pz + 4, 2);
313 return parse_scaled_value (res, &pz, buf + 2, 1);
316 /* Parses the syntax hh H mm M ss S. */
318 parse_HMS (cch_t * pz)
321 cch_t * ps = strchr (pz, 'H');
324 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
328 ps = strchr (pz, 'M');
331 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
335 ps = strchr (pz, 'S');
338 res = parse_scaled_value (res, &pz, ps, 1);
342 while (isspace ((unsigned char)*pz))
353 /* Parses a time (hours, minutes, seconds) specification in either syntax. */
355 parse_time (cch_t * pz)
363 ps = strchr (pz, ':');
366 res = parse_hour_minute_second (pz, ps);
370 * Try for a 'H', 'M' or 'S' suffix
372 else if (ps = strpbrk (pz, "HMS"),
375 /* Its a YYYYMMDD format: */
376 res = parse_hourminutesecond (pz);
380 res = parse_HMS (pz);
385 /* Returns a substring of the given string, with spaces at the beginning and at
386 the end destructively removed, per SNOBOL. */
390 /* trim leading white space */
391 while (isspace ((unsigned char)*pz))
394 /* trim trailing white space */
396 char * pe = pz + strlen (pz);
397 while ((pe > pz) && isspace ((unsigned char)pe[-1]))
406 * Parse the year/months/days of a time period
409 parse_period (cch_t * in_pz)
413 char * pz = strdup (in_pz);
423 pT = strchr (pz, 'T');
434 ps = strchr (pz, '-');
437 res = parse_year_month_day (pz, ps);
441 * Try for a 'Y', 'M' or 'D' suffix
443 else if (ps = strpbrk (pz, "YMWD"),
446 /* Its a YYYYMMDD format: */
447 res = parse_yearmonthday (pz);
451 res = parse_YMWD (pz);
453 if ((errno == 0) && (pT != NULL))
455 time_t val = parse_time (pT);
456 res = scale_n_add (res, val, 1);
464 parse_non_iso8601 (cch_t * pz)
466 whats_done_t whatd_we_do = NOTHING_IS_DONE;
474 val = str_const_to_l (pz, &pz, 10);
478 /* IF we find a colon, then we're going to have a seconds value.
479 We will not loop here any more. We cannot already have parsed
480 a minute value and if we've parsed an hour value, then the result
481 value has to be less than an hour. */
484 if (whatd_we_do >= MINUTE_IS_DONE)
487 val = parse_hr_min_sec (val, pz);
489 if ((whatd_we_do == HOUR_IS_DONE) && (val >= SEC_PER_HR))
492 return scale_n_add (res, val, 1);
498 /* Skip over white space following the number we just parsed. */
499 while (isspace ((unsigned char)*pz))
504 default: goto bad_time;
506 return scale_n_add (res, val, 1);
509 if (whatd_we_do >= YEAR_IS_DONE)
512 whatd_we_do = YEAR_IS_DONE;
516 if (whatd_we_do >= MONTH_IS_DONE)
518 mult = SEC_PER_MONTH;
519 whatd_we_do = MONTH_IS_DONE;
523 if (whatd_we_do >= WEEK_IS_DONE)
526 whatd_we_do = WEEK_IS_DONE;
530 if (whatd_we_do >= DAY_IS_DONE)
533 whatd_we_do = DAY_IS_DONE;
537 if (whatd_we_do >= HOUR_IS_DONE)
540 whatd_we_do = HOUR_IS_DONE;
544 if (whatd_we_do >= MINUTE_IS_DONE)
547 whatd_we_do = MINUTE_IS_DONE;
552 whatd_we_do = SECOND_IS_DONE;
556 res = scale_n_add (res, val, mult);
559 while (isspace ((unsigned char)*pz))
564 if (! isdigit ((unsigned char)*pz))
568 } while (whatd_we_do < SECOND_IS_DONE);
576 parse_duration (char const * pz)
578 while (isspace ((unsigned char)*pz))
584 return parse_period (pz + 1);
587 return parse_time (pz + 1);
590 if (isdigit ((unsigned char)*pz))
591 return parse_non_iso8601 (pz);
601 * c-file-style: "gnu"
602 * indent-tabs-mode: nil
604 * end of parse-duration.c */