/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#pragma prototyped22/*23* Glenn Fowler24* AT&T Bell Laboratories25*26* parse elapsed time in 1/n secs from s27* compatible with fmtelapsed()28* also handles ps [day-][hour:]min:sec29* also handles coshell % for 'infinity'30* if e!=0 then it is set to first unrecognized char31*/3233#include <ast.h>34#include <ctype.h>3536unsigned long37strelapsed(register const char* s, char** e, int n)38{39register int c;40register unsigned long v;41unsigned long t = 0;42int f = 0;43int p = 0;44int z = 1;45int m;46const char* last;4748for (;;)49{50while (isspace(*s) || *s == '_')51s++;52if (!*(last = s))53break;54if (z)55{56z = 0;57if (*s == '0' && (!(c = *(s + 1)) || isspace(c) || c == '_'))58{59last = s + 1;60break;61}62}63v = 0;64while ((c = *s++) >= '0' && c <= '9')65v = v * 10 + c - '0';66v *= n;67if (c == '.')68for (m = n; (c = *s++) >= '0' && c <= '9';)69f += (m /= 10) * (c - '0');70if (c == '%')71{72t = ~t;73last = s;74break;75}76if (s == last + 1)77break;78if (!p)79while (isspace(c) || c == '_')80c = *s++;81switch (c)82{83case 'S':84if (*s == 'E' || *s == 'e')85{86v += f;87f = 0;88}89else90v *= 20 * 12 * 4 * 7 * 24 * 60 * 60;91break;92case 'y':93case 'Y':94v *= 12 * 4 * 7 * 24 * 60 * 60;95break;96case 'M':97if (*s == 'I' || *s == 'i')98v *= 60;99else100v *= 4 * 7 * 24 * 60 * 60;101break;102case 'w':103v *= 7 * 24 * 60 * 60;104break;105case '-':106p = 1;107/*FALLTHROUGH*/108case 'd':109v *= 24 * 60 * 60;110break;111case 'h':112v *= 60 * 60;113break;114case ':':115p = 1;116v *= strchr(s, ':') ? (60 * 60) : 60;117break;118case 'm':119if (*s == 'o')120v *= 4 * 7 * 24 * 60 * 60;121else122v *= 60;123break;124case 's':125if (*s == 'c')126{127v *= 20 * 12 * 4 * 7 * 24 * 60 * 60;128break;129}130v += f;131f = 0;132break;133case 0:134s--;135v += f;136break;137default:138if (p)139{140last = s - 1;141t += v + f;142}143goto done;144}145t += v;146while (isalpha(*s))147s++;148}149done:150if (e)151*e = (char*)last;152return t;153}154155156