2 * Copyright (c) 1998-2001 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.
16 SM_RCSID("@(#)$Id: convtime.c,v 8.39 2001/09/11 04:05:13 gshapiro Exp $")
19 ** CONVTIME -- convert time
21 ** Takes a time as an ascii string with a trailing character
26 ** d -- days (default)
28 ** For example, "3d12h" is three and a half days.
31 ** p -- pointer to ascii time.
32 ** units -- default units if none specified.
51 if (sm_strcasecmp(p
, "now") == 0)
61 while ((c
= *p
++) != '\0' && isascii(c
) && isdigit(c
))
62 t
= t
* 10 + (c
- '0');
68 else if (strchr("wdhms", c
) == NULL
)
70 usrerr("Invalid time unit `%c'", c
);
89 case 'm': /* minutes */
93 case 's': /* seconds */
102 ** PINTVL -- produce printable version of a time interval
105 ** intvl -- the interval to be converted
106 ** brief -- if true, print this in an extremely compact form
107 ** (basically used for logging).
110 ** A pointer to a string version of intvl suitable for
111 ** printing or framing.
117 ** The string returned is in a static buffer.
120 #define PLURAL(n) ((n) == 1 ? "" : "s")
127 static char buf
[256];
129 int wk
, dy
, hr
, mi
, se
;
131 if (intvl
== 0 && !brief
)
132 return "zero seconds";
136 /* decode the interval into weeks, days, hours, minutes, seconds */
155 /* now turn it into a sexy form */
161 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "%d+", dy
);
164 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "%02d:%02d:%02d",
169 /* use the verbose form */
172 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), ", %d week%s", wk
,
178 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), ", %d day%s", dy
,
184 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), ", %d hour%s", hr
,
190 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), ", %d minute%s", mi
,
196 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), ", %d second%s", se
,