xref: /titanic_44/usr/src/lib/libast/common/tm/tmxduration.c (revision 96d9f183facd90dbbc2268c9a51689be0b6a0b46)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2009 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
34 tmxduration(const char* s, char** e)
35 {
36 	double		d;
37 	Time_t		ns;
38 	Time_t		ts;
39 	Time_t		now;
40 	char*		last;
41 	char*		t;
42 	char*		x;
43 	Sfio_t*		f;
44 	int		i;
45 
46 	now = TMX_NOW;
47 	while (isspace(*s))
48 		s++;
49 	if (*s == 'P' || *s == 'p')
50 		ns = tmxdate(s, &last, now) - now;
51 	else
52 	{
53 		ns = strtod(s, &last) * TMX_RESOLUTION;
54 		if (*last && (f = sfstropen()))
55 		{
56 			sfprintf(f, "exact %s", s);
57 			t = sfstruse(f);
58 			ts = tmxdate(t, &x, now);
59 			if ((i = x - t - 6) > (last - s))
60 			{
61 				last = (char*)s + i;
62 				ns = ts - now;
63 			}
64 			else
65 			{
66 				sfprintf(f, "p%s", s);
67 				t = sfstruse(f);
68 				ts = tmxdate(t, &x, now);
69 				if ((i = x - t - 1) > (last - s))
70 				{
71 					last = (char*)s + i;
72 					ns = ts - now;
73 				}
74 			}
75 			sfstrclose(f);
76 		}
77 	}
78 	if (e)
79 		*e = last;
80 	return ns;
81 }
82