1 /* Parse a time duration and return a seconds count
2 Copyright (C) 2008 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/>. */
27 #include "parse-duration.h"
37 #define cch_t char const
50 #define SEC_PER_MIN 60
51 #define SEC_PER_HR (SEC_PER_MIN * 60)
52 #define SEC_PER_DAY (SEC_PER_HR * 24)
53 #define SEC_PER_WEEK (SEC_PER_DAY * 7)
54 #define SEC_PER_MONTH (SEC_PER_DAY * 30)
55 #define SEC_PER_YEAR (SEC_PER_DAY * 365)
57 #define TIME_MAX 0x7FFFFFFF
59 static unsigned long inline
60 str_const_to_ul (cch_t * str, cch_t ** ppz, int base)
62 return strtoul (str, (char **)ppz, base);
66 str_const_to_l (cch_t * str, cch_t ** ppz, int base)
68 return strtol (str, (char **)ppz, base);
72 scale_n_add (time_t base, time_t val, int scale)
81 if (val > TIME_MAX / scale)
88 if (base > TIME_MAX - val)
98 parse_hr_min_sec (time_t start, cch_t * pz)
104 /* For as long as our scanner pointer points to a colon *AND*
105 we've not looped before, then keep looping. (two iterations max) */
106 while ((*pz == ':') && (lpct++ <= 1))
108 unsigned long v = str_const_to_ul (pz+1, &pz, 10);
113 start = scale_n_add (v, start, 60);
119 /* allow for trailing spaces */
120 while (isspace ((unsigned char)*pz)) pz++;
131 parse_scaled_value (time_t base, cch_t ** ppz, cch_t * endp, int scale)
136 if (base == BAD_TIME)
140 val = str_const_to_ul (pz, &pz, 10);
143 while (isspace ((unsigned char)*pz)) pz++;
151 return scale_n_add (base, val, scale);
155 parse_year_month_day (cch_t * pz, cch_t * ps)
159 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
161 ps = strchr (++pz, '-');
167 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
170 ps = pz + strlen (pz);
171 return parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
175 parse_yearmonthday (cch_t * in_pz)
181 if (strlen (in_pz) != 8)
187 memcpy (buf, in_pz, 4);
190 res = parse_scaled_value (0, &pz, buf + 4, SEC_PER_YEAR);
192 memcpy (buf, in_pz + 4, 2);
195 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MONTH);
197 memcpy (buf, in_pz + 6, 2);
200 return parse_scaled_value (res, &pz, buf + 2, SEC_PER_DAY);
204 parse_YMWD (cch_t * pz)
207 cch_t * ps = strchr (pz, 'Y');
210 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
214 ps = strchr (pz, 'M');
217 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
221 ps = strchr (pz, 'W');
224 res = parse_scaled_value (res, &pz, ps, SEC_PER_WEEK);
228 ps = strchr (pz, 'D');
231 res = parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
235 while (isspace ((unsigned char)*pz)) pz++;
246 parse_hour_minute_second (cch_t * pz, cch_t * ps)
250 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
252 ps = strchr (++pz, ':');
259 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
262 ps = pz + strlen (pz);
263 return parse_scaled_value (res, &pz, ps, 1);
267 parse_hourminutesecond (cch_t * in_pz)
273 if (strlen (in_pz) != 6)
279 memcpy (buf, in_pz, 2);
282 res = parse_scaled_value (0, &pz, buf + 2, SEC_PER_HR);
284 memcpy (buf, in_pz + 2, 2);
287 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MIN);
289 memcpy (buf, in_pz + 4, 2);
292 return parse_scaled_value (res, &pz, buf + 2, 1);
296 parse_HMS (cch_t * pz)
299 cch_t * ps = strchr (pz, 'H');
302 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
306 ps = strchr (pz, 'M');
309 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
313 ps = strchr (pz, 'S');
316 res = parse_scaled_value (res, &pz, ps, 1);
320 while (isspace ((unsigned char)*pz)) pz++;
331 parse_time (cch_t * pz)
339 ps = strchr (pz, ':');
342 res = parse_hour_minute_second (pz, ps);
346 * Try for a 'H', 'M' or 'S' suffix
348 else if (ps = strpbrk (pz, "HMS"),
351 /* Its a YYYYMMDD format: */
352 res = parse_hourminutesecond (pz);
356 res = parse_HMS (pz);
364 /* trim leading white space */
365 while (isspace ((unsigned char)*pz)) pz++;
367 /* trim trailing white space */
369 char * pe = pz + strlen (pz);
370 while ((pe > pz) && isspace ((unsigned char)pe[-1])) pe--;
378 * Parse the year/months/days of a time period
381 parse_period (cch_t * in_pz)
383 char * pz = xstrdup (in_pz);
384 char * pT = strchr (pz, 'T');
399 ps = strchr (pz, '-');
402 res = parse_year_month_day (pz, ps);
406 * Try for a 'Y', 'M' or 'D' suffix
408 else if (ps = strpbrk (pz, "YMWD"),
411 /* Its a YYYYMMDD format: */
412 res = parse_yearmonthday (pz);
416 res = parse_YMWD (pz);
418 if ((errno == 0) && (pT != NULL))
420 time_t val = parse_time (pT);
421 res = scale_n_add (res, val, 1);
429 parse_non_iso8601(cch_t * pz)
431 whats_done_t whatd_we_do = NOTHING_IS_DONE;
439 val = str_const_to_l (pz, &pz, 10);
443 /* IF we find a colon, then we're going to have a seconds value.
444 We will not loop here any more. We cannot already have parsed
445 a minute value and if we've parsed an hour value, then the result
446 value has to be less than an hour. */
449 if (whatd_we_do >= MINUTE_IS_DONE)
452 val = parse_hr_min_sec (val, pz);
454 if ((whatd_we_do == HOUR_IS_DONE) && (val >= SEC_PER_HR))
457 return scale_n_add (res, val, 1);
463 /* Skip over white space following the number we just parsed. */
464 while (isspace ((unsigned char)*pz)) pz++;
468 default: goto bad_time;
470 return scale_n_add (res, val, 1);
473 if (whatd_we_do >= YEAR_IS_DONE)
476 whatd_we_do = YEAR_IS_DONE;
480 if (whatd_we_do >= MONTH_IS_DONE)
482 mult = SEC_PER_MONTH;
483 whatd_we_do = MONTH_IS_DONE;
487 if (whatd_we_do >= WEEK_IS_DONE)
490 whatd_we_do = WEEK_IS_DONE;
494 if (whatd_we_do >= DAY_IS_DONE)
497 whatd_we_do = DAY_IS_DONE;
501 if (whatd_we_do >= HOUR_IS_DONE)
504 whatd_we_do = HOUR_IS_DONE;
508 if (whatd_we_do >= MINUTE_IS_DONE)
511 whatd_we_do = MINUTE_IS_DONE;
516 whatd_we_do = SECOND_IS_DONE;
520 res = scale_n_add (res, val, mult);
522 while (isspace ((unsigned char)*++pz)) ;
526 if (! isdigit ((unsigned char)*pz))
530 } while (whatd_we_do < SECOND_IS_DONE);
538 parse_duration (char const * pz)
542 while (isspace ((unsigned char)*pz)) pz++;
547 res = parse_period (pz + 1);
548 if ((errno != 0) || (res == BAD_TIME))
555 res = parse_time (pz + 1);
556 if ((errno != 0) || (res == BAD_TIME))
561 if (! isdigit ((unsigned char)*pz))
564 res = parse_non_iso8601 (pz);
565 if ((errno == 0) && (res != BAD_TIME))
570 fprintf (stderr, _("Invalid time duration: %s\n"), pz);
579 * c-file-style: "gnu"
580 * indent-tabs-mode: nil
582 * end of parse-duration.c */