removed old flag
[gnutls.git] / src / libopts / parse-duration.c
blob655a5e542be7e4ecbaabc256e25bc045c29a0ca3
1 /* Parse a time duration and return a seconds count
2 Copyright (C) 2008-2012 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/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "parse-duration.h"
23 #include <ctype.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #ifndef NUL
31 #define NUL '\0'
32 #endif
34 #define cch_t char const
36 typedef enum {
37 NOTHING_IS_DONE,
38 YEAR_IS_DONE,
39 MONTH_IS_DONE,
40 WEEK_IS_DONE,
41 DAY_IS_DONE,
42 HOUR_IS_DONE,
43 MINUTE_IS_DONE,
44 SECOND_IS_DONE
45 } whats_done_t;
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 inline static unsigned long
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. */
64 inline static long
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. */
73 inline static time_t
74 scale_n_add (time_t base, time_t val, int scale)
76 if (base == BAD_TIME)
78 if (errno == 0)
79 errno = EINVAL;
80 return BAD_TIME;
83 if (val > TIME_MAX / scale)
85 errno = ERANGE;
86 return BAD_TIME;
89 val *= scale;
90 if (base > TIME_MAX - val)
92 errno = ERANGE;
93 return BAD_TIME;
96 return base + val;
99 /* After a number HH has been parsed, parse subsequent :MM or :MM:SS. */
100 static time_t
101 parse_hr_min_sec (time_t start, cch_t * pz)
103 int lpct = 0;
105 errno = 0;
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);
113 if (errno != 0)
114 return BAD_TIME;
116 start = scale_n_add (v, start, 60);
118 if (errno != 0)
119 return BAD_TIME;
122 /* allow for trailing spaces */
123 while (isspace ((unsigned char)*pz))
124 pz++;
125 if (*pz != NUL)
127 errno = EINVAL;
128 return BAD_TIME;
131 return start;
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. */
137 static time_t
138 parse_scaled_value (time_t base, cch_t ** ppz, cch_t * endp, int scale)
140 cch_t * pz = *ppz;
141 time_t val;
143 if (base == BAD_TIME)
144 return base;
146 errno = 0;
147 val = str_const_to_ul (pz, &pz, 10);
148 if (errno != 0)
149 return BAD_TIME;
150 while (isspace ((unsigned char)*pz))
151 pz++;
152 if (pz != endp)
154 errno = EINVAL;
155 return BAD_TIME;
158 *ppz = 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". */
164 static time_t
165 parse_year_month_day (cch_t * pz, cch_t * ps)
167 time_t res = 0;
169 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
171 pz++; /* over the first '-' */
172 ps = strchr (pz, '-');
173 if (ps == NULL)
175 errno = EINVAL;
176 return BAD_TIME;
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. */
186 static time_t
187 parse_yearmonthday (cch_t * in_pz)
189 time_t res = 0;
190 char buf[8];
191 cch_t * pz;
193 if (strlen (in_pz) != 8)
195 errno = EINVAL;
196 return BAD_TIME;
199 memcpy (buf, in_pz, 4);
200 buf[4] = NUL;
201 pz = buf;
202 res = parse_scaled_value (0, &pz, buf + 4, SEC_PER_YEAR);
204 memcpy (buf, in_pz + 4, 2);
205 buf[2] = NUL;
206 pz = buf;
207 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MONTH);
209 memcpy (buf, in_pz + 6, 2);
210 buf[2] = NUL;
211 pz = buf;
212 return parse_scaled_value (res, &pz, buf + 2, SEC_PER_DAY);
215 /* Parses the syntax yy Y mm M ww W dd D. */
216 static time_t
217 parse_YMWD (cch_t * pz)
219 time_t res = 0;
220 cch_t * ps = strchr (pz, 'Y');
221 if (ps != NULL)
223 res = parse_scaled_value (0, &pz, ps, SEC_PER_YEAR);
224 pz++;
227 ps = strchr (pz, 'M');
228 if (ps != NULL)
230 res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH);
231 pz++;
234 ps = strchr (pz, 'W');
235 if (ps != NULL)
237 res = parse_scaled_value (res, &pz, ps, SEC_PER_WEEK);
238 pz++;
241 ps = strchr (pz, 'D');
242 if (ps != NULL)
244 res = parse_scaled_value (res, &pz, ps, SEC_PER_DAY);
245 pz++;
248 while (isspace ((unsigned char)*pz))
249 pz++;
250 if (*pz != NUL)
252 errno = EINVAL;
253 return BAD_TIME;
256 return res;
259 /* Parses the syntax HH:MM:SS.
260 PS points into the string, after "HH", before ":MM:SS". */
261 static time_t
262 parse_hour_minute_second (cch_t * pz, cch_t * ps)
264 time_t res = 0;
266 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
268 pz++;
269 ps = strchr (pz, ':');
270 if (ps == NULL)
272 errno = EINVAL;
273 return BAD_TIME;
276 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
278 pz++;
279 ps = pz + strlen (pz);
280 return parse_scaled_value (res, &pz, ps, 1);
283 /* Parses the syntax HHMMSS. */
284 static time_t
285 parse_hourminutesecond (cch_t * in_pz)
287 time_t res = 0;
288 char buf[4];
289 cch_t * pz;
291 if (strlen (in_pz) != 6)
293 errno = EINVAL;
294 return BAD_TIME;
297 memcpy (buf, in_pz, 2);
298 buf[2] = NUL;
299 pz = buf;
300 res = parse_scaled_value (0, &pz, buf + 2, SEC_PER_HR);
302 memcpy (buf, in_pz + 2, 2);
303 buf[2] = NUL;
304 pz = buf;
305 res = parse_scaled_value (res, &pz, buf + 2, SEC_PER_MIN);
307 memcpy (buf, in_pz + 4, 2);
308 buf[2] = NUL;
309 pz = buf;
310 return parse_scaled_value (res, &pz, buf + 2, 1);
313 /* Parses the syntax hh H mm M ss S. */
314 static time_t
315 parse_HMS (cch_t * pz)
317 time_t res = 0;
318 cch_t * ps = strchr (pz, 'H');
319 if (ps != NULL)
321 res = parse_scaled_value (0, &pz, ps, SEC_PER_HR);
322 pz++;
325 ps = strchr (pz, 'M');
326 if (ps != NULL)
328 res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN);
329 pz++;
332 ps = strchr (pz, 'S');
333 if (ps != NULL)
335 res = parse_scaled_value (res, &pz, ps, 1);
336 pz++;
339 while (isspace ((unsigned char)*pz))
340 pz++;
341 if (*pz != NUL)
343 errno = EINVAL;
344 return BAD_TIME;
347 return res;
350 /* Parses a time (hours, minutes, seconds) specification in either syntax. */
351 static time_t
352 parse_time (cch_t * pz)
354 cch_t * ps;
355 time_t res = 0;
358 * Scan for a hyphen
360 ps = strchr (pz, ':');
361 if (ps != NULL)
363 res = parse_hour_minute_second (pz, ps);
367 * Try for a 'H', 'M' or 'S' suffix
369 else if (ps = strpbrk (pz, "HMS"),
370 ps == NULL)
372 /* Its a YYYYMMDD format: */
373 res = parse_hourminutesecond (pz);
376 else
377 res = parse_HMS (pz);
379 return res;
382 /* Returns a substring of the given string, with spaces at the beginning and at
383 the end destructively removed, per SNOBOL. */
384 static char *
385 trim (char * pz)
387 /* trim leading white space */
388 while (isspace ((unsigned char)*pz))
389 pz++;
391 /* trim trailing white space */
393 char * pe = pz + strlen (pz);
394 while ((pe > pz) && isspace ((unsigned char)pe[-1]))
395 pe--;
396 *pe = NUL;
399 return pz;
403 * Parse the year/months/days of a time period
405 static time_t
406 parse_period (cch_t * in_pz)
408 char * pT;
409 char * ps;
410 char * pz = strdup (in_pz);
411 void * fptr = pz;
412 time_t res = 0;
414 if (pz == NULL)
416 errno = ENOMEM;
417 return BAD_TIME;
420 pT = strchr (pz, 'T');
421 if (pT != NULL)
423 *(pT++) = NUL;
424 pz = trim (pz);
425 pT = trim (pT);
429 * Scan for a hyphen
431 ps = strchr (pz, '-');
432 if (ps != NULL)
434 res = parse_year_month_day (pz, ps);
438 * Try for a 'Y', 'M' or 'D' suffix
440 else if (ps = strpbrk (pz, "YMWD"),
441 ps == NULL)
443 /* Its a YYYYMMDD format: */
444 res = parse_yearmonthday (pz);
447 else
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);
456 free (fptr);
457 return res;
460 static time_t
461 parse_non_iso8601 (cch_t * pz)
463 whats_done_t whatd_we_do = NOTHING_IS_DONE;
465 time_t res = 0;
467 do {
468 time_t val;
470 errno = 0;
471 val = str_const_to_l (pz, &pz, 10);
472 if (errno != 0)
473 goto bad_time;
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. */
479 if (*pz == ':')
481 if (whatd_we_do >= MINUTE_IS_DONE)
482 break;
484 val = parse_hr_min_sec (val, pz);
486 if ((whatd_we_do == HOUR_IS_DONE) && (val >= SEC_PER_HR))
487 break;
489 return scale_n_add (res, val, 1);
493 unsigned int mult;
495 /* Skip over white space following the number we just parsed. */
496 while (isspace ((unsigned char)*pz))
497 pz++;
499 switch (*pz)
501 default: goto bad_time;
502 case NUL:
503 return scale_n_add (res, val, 1);
505 case 'y': case 'Y':
506 if (whatd_we_do >= YEAR_IS_DONE)
507 goto bad_time;
508 mult = SEC_PER_YEAR;
509 whatd_we_do = YEAR_IS_DONE;
510 break;
512 case 'M':
513 if (whatd_we_do >= MONTH_IS_DONE)
514 goto bad_time;
515 mult = SEC_PER_MONTH;
516 whatd_we_do = MONTH_IS_DONE;
517 break;
519 case 'W':
520 if (whatd_we_do >= WEEK_IS_DONE)
521 goto bad_time;
522 mult = SEC_PER_WEEK;
523 whatd_we_do = WEEK_IS_DONE;
524 break;
526 case 'd': case 'D':
527 if (whatd_we_do >= DAY_IS_DONE)
528 goto bad_time;
529 mult = SEC_PER_DAY;
530 whatd_we_do = DAY_IS_DONE;
531 break;
533 case 'h':
534 if (whatd_we_do >= HOUR_IS_DONE)
535 goto bad_time;
536 mult = SEC_PER_HR;
537 whatd_we_do = HOUR_IS_DONE;
538 break;
540 case 'm':
541 if (whatd_we_do >= MINUTE_IS_DONE)
542 goto bad_time;
543 mult = SEC_PER_MIN;
544 whatd_we_do = MINUTE_IS_DONE;
545 break;
547 case 's':
548 mult = 1;
549 whatd_we_do = SECOND_IS_DONE;
550 break;
553 res = scale_n_add (res, val, mult);
555 pz++;
556 while (isspace ((unsigned char)*pz))
557 pz++;
558 if (*pz == NUL)
559 return res;
561 if (! isdigit ((unsigned char)*pz))
562 break;
565 } while (whatd_we_do < SECOND_IS_DONE);
567 bad_time:
568 errno = EINVAL;
569 return BAD_TIME;
572 time_t
573 parse_duration (char const * pz)
575 while (isspace ((unsigned char)*pz))
576 pz++;
578 switch (*pz)
580 case 'P':
581 return parse_period (pz + 1);
583 case 'T':
584 return parse_time (pz + 1);
586 default:
587 if (isdigit ((unsigned char)*pz))
588 return parse_non_iso8601 (pz);
590 errno = EINVAL;
591 return BAD_TIME;
596 * Local Variables:
597 * mode: C
598 * c-file-style: "gnu"
599 * indent-tabs-mode: nil
600 * End:
601 * end of parse-duration.c */