1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #pragma prototyped
23
24 #include <tmx.h>
25 #include <ctype.h>
26
27 /*
28 * parse duration expression in s and return Time_t value
29 * if non-null, e points to the first unused char in s
30 * returns 0 with *e==s on error
31 */
32
33 Time_t
tmxduration(const char * s,char ** e)34 tmxduration(const char* s, char** e)
35 {
36 Time_t ns;
37 Time_t ts;
38 Time_t now;
39 char* last;
40 char* t;
41 char* x;
42 Sfio_t* f;
43 int i;
44
45 now = TMX_NOW;
46 while (isspace(*s))
47 s++;
48 if (*s == 'P' || *s == 'p')
49 ns = tmxdate(s, &last, now) - now;
50 else
51 {
52 ns = strtod(s, &last) * TMX_RESOLUTION;
53 if (*last && (f = sfstropen()))
54 {
55 sfprintf(f, "exact %s", s);
56 t = sfstruse(f);
57 ts = tmxdate(t, &x, now);
58 if ((i = x - t - 6) > (last - s))
59 {
60 last = (char*)s + i;
61 ns = ts - now;
62 }
63 else
64 {
65 sfprintf(f, "p%s", s);
66 t = sfstruse(f);
67 ts = tmxdate(t, &x, now);
68 if ((i = x - t - 1) > (last - s))
69 {
70 last = (char*)s + i;
71 ns = ts - now;
72 }
73 }
74 sfstrclose(f);
75 }
76 }
77 if (e)
78 *e = last;
79 return ns;
80 }
81