3 /* Parse a time duration and return a seconds count
4 Copyright (C) 2008 Free Software Foundation, Inc.
5 Written by Bruce Korb <bkorb@gnu.org>, 2008.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "parse-duration.h"
39 #define cch_t char const
52 #define SEC_PER_MIN 60
53 #define SEC_PER_HR (SEC_PER_MIN * 60)
54 #define SEC_PER_DAY (SEC_PER_HR * 24)
55 #define SEC_PER_WEEK (SEC_PER_DAY * 7)
56 #define SEC_PER_MONTH (SEC_PER_DAY * 30)
57 #define SEC_PER_YEAR (SEC_PER_DAY * 365)
59 #define TIME_MAX 0x7FFFFFFF
61 static unsigned long inline
62 str_const_to_ul (cch_t
* str
, cch_t
** ppz
, int base
)
64 return strtoul (str
, (char **)ppz
, base
);
68 str_const_to_l (cch_t
* str
, cch_t
** ppz
, int base
)
70 return strtol (str
, (char **)ppz
, base
);
74 scale_n_add (time_t base
, time_t val
, int scale
)
83 if (val
> TIME_MAX
/ scale
)
90 if (base
> TIME_MAX
- val
)
100 parse_hr_min_sec (time_t start
, cch_t
* pz
)
106 /* For as long as our scanner pointer points to a colon *AND*
107 we've not looped before, then keep looping. (two iterations max) */
108 while ((*pz
== ':') && (lpct
++ <= 1))
110 unsigned long v
= str_const_to_ul (pz
+1, &pz
, 10);
115 start
= scale_n_add (v
, start
, 60);
121 /* allow for trailing spaces */
122 while (isspace ((unsigned char)*pz
)) pz
++;
133 parse_scaled_value (time_t base
, cch_t
** ppz
, cch_t
* endp
, int scale
)
138 if (base
== BAD_TIME
)
142 val
= str_const_to_ul (pz
, &pz
, 10);
145 while (isspace ((unsigned char)*pz
)) pz
++;
153 return scale_n_add (base
, val
, scale
);
157 parse_year_month_day (cch_t
* pz
, cch_t
* ps
)
161 res
= parse_scaled_value (0, &pz
, ps
, SEC_PER_YEAR
);
163 ps
= strchr (++pz
, '-');
169 res
= parse_scaled_value (res
, &pz
, ps
, SEC_PER_MONTH
);
172 ps
= pz
+ strlen (pz
);
173 return parse_scaled_value (res
, &pz
, ps
, SEC_PER_DAY
);
177 parse_yearmonthday (cch_t
* in_pz
)
183 if (strlen (in_pz
) != 8)
189 memcpy (buf
, in_pz
, 4);
192 res
= parse_scaled_value (0, &pz
, buf
+ 4, SEC_PER_YEAR
);
194 memcpy (buf
, in_pz
+ 4, 2);
197 res
= parse_scaled_value (res
, &pz
, buf
+ 2, SEC_PER_MONTH
);
199 memcpy (buf
, in_pz
+ 6, 2);
202 return parse_scaled_value (res
, &pz
, buf
+ 2, SEC_PER_DAY
);
206 parse_YMWD (cch_t
* pz
)
209 cch_t
* ps
= strchr (pz
, 'Y');
212 res
= parse_scaled_value (0, &pz
, ps
, SEC_PER_YEAR
);
216 ps
= strchr (pz
, 'M');
219 res
= parse_scaled_value (res
, &pz
, ps
, SEC_PER_MONTH
);
223 ps
= strchr (pz
, 'W');
226 res
= parse_scaled_value (res
, &pz
, ps
, SEC_PER_WEEK
);
230 ps
= strchr (pz
, 'D');
233 res
= parse_scaled_value (res
, &pz
, ps
, SEC_PER_DAY
);
237 while (isspace ((unsigned char)*pz
)) pz
++;
248 parse_hour_minute_second (cch_t
* pz
, cch_t
* ps
)
252 res
= parse_scaled_value (0, &pz
, ps
, SEC_PER_HR
);
254 ps
= strchr (++pz
, ':');
261 res
= parse_scaled_value (res
, &pz
, ps
, SEC_PER_MIN
);
264 ps
= pz
+ strlen (pz
);
265 return parse_scaled_value (res
, &pz
, ps
, 1);
269 parse_hourminutesecond (cch_t
* in_pz
)
275 if (strlen (in_pz
) != 6)
281 memcpy (buf
, in_pz
, 2);
284 res
= parse_scaled_value (0, &pz
, buf
+ 2, SEC_PER_HR
);
286 memcpy (buf
, in_pz
+ 2, 2);
289 res
= parse_scaled_value (res
, &pz
, buf
+ 2, SEC_PER_MIN
);
291 memcpy (buf
, in_pz
+ 4, 2);
294 return parse_scaled_value (res
, &pz
, buf
+ 2, 1);
298 parse_HMS (cch_t
* pz
)
301 cch_t
* ps
= strchr (pz
, 'H');
304 res
= parse_scaled_value (0, &pz
, ps
, SEC_PER_HR
);
308 ps
= strchr (pz
, 'M');
311 res
= parse_scaled_value (res
, &pz
, ps
, SEC_PER_MIN
);
315 ps
= strchr (pz
, 'S');
318 res
= parse_scaled_value (res
, &pz
, ps
, 1);
322 while (isspace ((unsigned char)*pz
)) pz
++;
333 parse_time (cch_t
* pz
)
341 ps
= strchr (pz
, ':');
344 res
= parse_hour_minute_second (pz
, ps
);
348 * Try for a 'H', 'M' or 'S' suffix
350 else if (ps
= strpbrk (pz
, "HMS"),
353 /* Its a YYYYMMDD format: */
354 res
= parse_hourminutesecond (pz
);
358 res
= parse_HMS (pz
);
366 /* trim leading white space */
367 while (isspace ((unsigned char)*pz
)) pz
++;
369 /* trim trailing white space */
371 char * pe
= pz
+ strlen (pz
);
372 while ((pe
> pz
) && isspace ((unsigned char)pe
[-1])) pe
--;
380 * Parse the year/months/days of a time period
383 parse_period (cch_t
* in_pz
)
385 char * pz
= xstrdup (in_pz
);
386 char * pT
= strchr (pz
, 'T');
401 ps
= strchr (pz
, '-');
404 res
= parse_year_month_day (pz
, ps
);
408 * Try for a 'Y', 'M' or 'D' suffix
410 else if (ps
= strpbrk (pz
, "YMWD"),
413 /* Its a YYYYMMDD format: */
414 res
= parse_yearmonthday (pz
);
418 res
= parse_YMWD (pz
);
420 if ((errno
== 0) && (pT
!= NULL
))
422 time_t val
= parse_time (pT
);
423 res
= scale_n_add (res
, val
, 1);
431 parse_non_iso8601(cch_t
* pz
)
433 whats_done_t whatd_we_do
= NOTHING_IS_DONE
;
441 val
= str_const_to_l (pz
, &pz
, 10);
445 /* IF we find a colon, then we're going to have a seconds value.
446 We will not loop here any more. We cannot already have parsed
447 a minute value and if we've parsed an hour value, then the result
448 value has to be less than an hour. */
451 if (whatd_we_do
>= MINUTE_IS_DONE
)
454 val
= parse_hr_min_sec (val
, pz
);
456 if ((whatd_we_do
== HOUR_IS_DONE
) && (val
>= SEC_PER_HR
))
459 return scale_n_add (res
, val
, 1);
465 /* Skip over white space following the number we just parsed. */
466 while (isspace ((unsigned char)*pz
)) pz
++;
470 default: goto bad_time
;
472 return scale_n_add (res
, val
, 1);
475 if (whatd_we_do
>= YEAR_IS_DONE
)
478 whatd_we_do
= YEAR_IS_DONE
;
482 if (whatd_we_do
>= MONTH_IS_DONE
)
484 mult
= SEC_PER_MONTH
;
485 whatd_we_do
= MONTH_IS_DONE
;
489 if (whatd_we_do
>= WEEK_IS_DONE
)
492 whatd_we_do
= WEEK_IS_DONE
;
496 if (whatd_we_do
>= DAY_IS_DONE
)
499 whatd_we_do
= DAY_IS_DONE
;
503 if (whatd_we_do
>= HOUR_IS_DONE
)
506 whatd_we_do
= HOUR_IS_DONE
;
510 if (whatd_we_do
>= MINUTE_IS_DONE
)
513 whatd_we_do
= MINUTE_IS_DONE
;
518 whatd_we_do
= SECOND_IS_DONE
;
522 res
= scale_n_add (res
, val
, mult
);
524 while (isspace ((unsigned char)*++pz
)) ;
528 if (! isdigit ((unsigned char)*pz
))
532 } while (whatd_we_do
< SECOND_IS_DONE
);
540 parse_duration (char const * pz
)
544 while (isspace ((unsigned char)*pz
)) pz
++;
549 res
= parse_period (pz
+ 1);
550 if ((errno
!= 0) || (res
== BAD_TIME
))
557 res
= parse_time (pz
+ 1);
558 if ((errno
!= 0) || (res
== BAD_TIME
))
563 if (! isdigit ((unsigned char)*pz
))
566 res
= parse_non_iso8601 (pz
);
567 if ((errno
== 0) && (res
!= BAD_TIME
))
572 fprintf (stderr
, _("Invalid time duration: %s\n"), pz
);
581 * c-file-style: "gnu"
582 * indent-tabs-mode: nil
584 * end of parse-duration.c */