1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin *                                                                      *
3da2e3ebdSchin *               This software is part of the ast package               *
4*b30d1939SAndy Fiddaman *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5da2e3ebdSchin *                      and is licensed under the                       *
6*b30d1939SAndy Fiddaman *                 Eclipse Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
8da2e3ebdSchin *                                                                      *
9da2e3ebdSchin *                A copy of the License is available at                 *
10*b30d1939SAndy Fiddaman *          http://www.eclipse.org/org/documents/epl-v10.html           *
11*b30d1939SAndy Fiddaman *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12da2e3ebdSchin *                                                                      *
13da2e3ebdSchin *              Information and Software Systems Research               *
14da2e3ebdSchin *                            AT&T Research                             *
15da2e3ebdSchin *                           Florham Park NJ                            *
16da2e3ebdSchin *                                                                      *
17da2e3ebdSchin *                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebdSchin *                  David Korn <dgk@research.att.com>                   *
19da2e3ebdSchin *                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebdSchin *                                                                      *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #pragma prototyped
23da2e3ebdSchin 
2434f9b3eeSRoland Mainz #include <tmx.h>
2534f9b3eeSRoland Mainz #include <ctype.h>
2634f9b3eeSRoland Mainz 
27da2e3ebdSchin /*
2834f9b3eeSRoland Mainz  * parse duration expression in s and return Time_t value
2934f9b3eeSRoland Mainz  * if non-null, e points to the first unused char in s
3034f9b3eeSRoland Mainz  * returns 0 with *e==s on error
31da2e3ebdSchin  */
32da2e3ebdSchin 
3334f9b3eeSRoland Mainz Time_t
tmxduration(const char * s,char ** e)3434f9b3eeSRoland Mainz tmxduration(const char* s, char** e)
35da2e3ebdSchin {
3634f9b3eeSRoland Mainz 	Time_t		ns;
3734f9b3eeSRoland Mainz 	Time_t		ts;
3834f9b3eeSRoland Mainz 	Time_t		now;
3934f9b3eeSRoland Mainz 	char*		last;
4034f9b3eeSRoland Mainz 	char*		t;
4134f9b3eeSRoland Mainz 	char*		x;
4234f9b3eeSRoland Mainz 	Sfio_t*		f;
4334f9b3eeSRoland Mainz 	int		i;
44da2e3ebdSchin 
4534f9b3eeSRoland Mainz 	now = TMX_NOW;
4634f9b3eeSRoland Mainz 	while (isspace(*s))
4734f9b3eeSRoland Mainz 		s++;
4834f9b3eeSRoland Mainz 	if (*s == 'P' || *s == 'p')
4934f9b3eeSRoland Mainz 		ns = tmxdate(s, &last, now) - now;
5034f9b3eeSRoland Mainz 	else
51da2e3ebdSchin 	{
5234f9b3eeSRoland Mainz 		ns = strtod(s, &last) * TMX_RESOLUTION;
5334f9b3eeSRoland Mainz 		if (*last && (f = sfstropen()))
5434f9b3eeSRoland Mainz 		{
5534f9b3eeSRoland Mainz 			sfprintf(f, "exact %s", s);
5634f9b3eeSRoland Mainz 			t = sfstruse(f);
5734f9b3eeSRoland Mainz 			ts = tmxdate(t, &x, now);
5834f9b3eeSRoland Mainz 			if ((i = x - t - 6) > (last - s))
5934f9b3eeSRoland Mainz 			{
6034f9b3eeSRoland Mainz 				last = (char*)s + i;
6134f9b3eeSRoland Mainz 				ns = ts - now;
6234f9b3eeSRoland Mainz 			}
6334f9b3eeSRoland Mainz 			else
6434f9b3eeSRoland Mainz 			{
6534f9b3eeSRoland Mainz 				sfprintf(f, "p%s", s);
6634f9b3eeSRoland Mainz 				t = sfstruse(f);
6734f9b3eeSRoland Mainz 				ts = tmxdate(t, &x, now);
6834f9b3eeSRoland Mainz 				if ((i = x - t - 1) > (last - s))
6934f9b3eeSRoland Mainz 				{
7034f9b3eeSRoland Mainz 					last = (char*)s + i;
7134f9b3eeSRoland Mainz 					ns = ts - now;
7234f9b3eeSRoland Mainz 				}
7334f9b3eeSRoland Mainz 			}
7434f9b3eeSRoland Mainz 			sfstrclose(f);
7534f9b3eeSRoland Mainz 		}
76da2e3ebdSchin 	}
7734f9b3eeSRoland Mainz 	if (e)
7834f9b3eeSRoland Mainz 		*e = last;
7934f9b3eeSRoland Mainz 	return ns;
80da2e3ebdSchin }
81