2 * Copyright (c) 1998, 1999 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
15 static char id[] = "@(#)$Id: convtime.c,v 8.25 1999/06/16 21:11:26 ca Exp $";
21 ** CONVTIME -- convert time
23 ** Takes a time as an ascii string with a trailing character
28 ** d -- days (default)
30 ** For example, "3d12h" is three and a half days.
33 ** p -- pointer to ascii time.
34 ** units -- default units if none specified.
52 if (strcasecmp(p, "now") == 0)
57 while ((c = *p++) != '\0' && isascii(c) && isdigit(c))
58 t = t * 10 + (c - '0');
64 else if (strchr("wdhms", c) == NULL)
66 usrerr("Invalid time unit `%c'", c);
85 case 'm': /* minutes */
89 case 's': /* seconds */
98 ** PINTVL -- produce printable version of a time interval
101 ** intvl -- the interval to be converted
102 ** brief -- if TRUE, print this in an extremely compact form
103 ** (basically used for logging).
106 ** A pointer to a string version of intvl suitable for
107 ** printing or framing.
113 ** The string returned is in a static buffer.
116 #define PLURAL(n) ((n) == 1 ? "" : "s")
123 static char buf[256];
125 int wk, dy, hr, mi, se;
127 if (intvl == 0 && !brief)
128 return "zero seconds";
132 /* decode the interval into weeks, days, hours, minutes, seconds */
151 /* now turn it into a sexy form */
157 (void) snprintf(p, SPACELEFT(buf, p), "%d+", dy);
160 (void) snprintf(p, SPACELEFT(buf, p), "%02d:%02d:%02d",
165 /* use the verbose form */
168 (void) snprintf(p, SPACELEFT(buf, p), ", %d week%s", wk, PLURAL(wk));
173 (void) snprintf(p, SPACELEFT(buf, p), ", %d day%s", dy, PLURAL(dy));
178 (void) snprintf(p, SPACELEFT(buf, p), ", %d hour%s", hr, PLURAL(hr));
183 (void) snprintf(p, SPACELEFT(buf, p), ", %d minute%s", mi, PLURAL(mi));
188 (void) snprintf(p, SPACELEFT(buf, p), ", %d second%s", se, PLURAL(se));