Import sendmail 8.13.4 into a new contrib directory as the first step
[dragonfly.git] / contrib / sendmail-8.13.4 / sendmail / convtime.c
blob36edc1a0b47f2906484e59fbadcfa3ad601898aa
1 /*
2 * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
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.
14 #include <sendmail.h>
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
22 ** giving units:
23 ** s -- seconds
24 ** m -- minutes
25 ** h -- hours
26 ** d -- days (default)
27 ** w -- weeks
28 ** For example, "3d12h" is three and a half days.
30 ** Parameters:
31 ** p -- pointer to ascii time.
32 ** units -- default units if none specified.
34 ** Returns:
35 ** time in seconds.
37 ** Side Effects:
38 ** none.
41 time_t
42 convtime(p, units)
43 char *p;
44 int units;
46 register time_t t, r;
47 register char c;
48 bool pos = true;
50 r = 0;
51 if (sm_strcasecmp(p, "now") == 0)
52 return NOW;
53 if (*p == '-')
55 pos = false;
56 ++p;
58 while (*p != '\0')
60 t = 0;
61 while ((c = *p++) != '\0' && isascii(c) && isdigit(c))
62 t = t * 10 + (c - '0');
63 if (c == '\0')
65 c = units;
66 p--;
68 else if (strchr("wdhms", c) == NULL)
70 usrerr("Invalid time unit `%c'", c);
71 c = units;
73 switch (c)
75 case 'w': /* weeks */
76 t *= 7;
77 /* FALLTHROUGH */
79 case 'd': /* days */
80 /* FALLTHROUGH */
81 default:
82 t *= 24;
83 /* FALLTHROUGH */
85 case 'h': /* hours */
86 t *= 60;
87 /* FALLTHROUGH */
89 case 'm': /* minutes */
90 t *= 60;
91 /* FALLTHROUGH */
93 case 's': /* seconds */
94 break;
96 r += t;
99 return pos ? r : -r;
102 ** PINTVL -- produce printable version of a time interval
104 ** Parameters:
105 ** intvl -- the interval to be converted
106 ** brief -- if true, print this in an extremely compact form
107 ** (basically used for logging).
109 ** Returns:
110 ** A pointer to a string version of intvl suitable for
111 ** printing or framing.
113 ** Side Effects:
114 ** none.
116 ** Warning:
117 ** The string returned is in a static buffer.
120 #define PLURAL(n) ((n) == 1 ? "" : "s")
122 char *
123 pintvl(intvl, brief)
124 time_t intvl;
125 bool brief;
127 static char buf[256];
128 register char *p;
129 int wk, dy, hr, mi, se;
131 if (intvl == 0 && !brief)
132 return "zero seconds";
133 if (intvl == NOW)
134 return "too long";
136 /* decode the interval into weeks, days, hours, minutes, seconds */
137 se = intvl % 60;
138 intvl /= 60;
139 mi = intvl % 60;
140 intvl /= 60;
141 hr = intvl % 24;
142 intvl /= 24;
143 if (brief)
145 dy = intvl;
146 wk = 0;
148 else
150 dy = intvl % 7;
151 intvl /= 7;
152 wk = intvl;
155 /* now turn it into a sexy form */
156 p = buf;
157 if (brief)
159 if (dy > 0)
161 (void) sm_snprintf(p, SPACELEFT(buf, p), "%d+", dy);
162 p += strlen(p);
164 (void) sm_snprintf(p, SPACELEFT(buf, p), "%02d:%02d:%02d",
165 hr, mi, se);
166 return buf;
169 /* use the verbose form */
170 if (wk > 0)
172 (void) sm_snprintf(p, SPACELEFT(buf, p), ", %d week%s", wk,
173 PLURAL(wk));
174 p += strlen(p);
176 if (dy > 0)
178 (void) sm_snprintf(p, SPACELEFT(buf, p), ", %d day%s", dy,
179 PLURAL(dy));
180 p += strlen(p);
182 if (hr > 0)
184 (void) sm_snprintf(p, SPACELEFT(buf, p), ", %d hour%s", hr,
185 PLURAL(hr));
186 p += strlen(p);
188 if (mi > 0)
190 (void) sm_snprintf(p, SPACELEFT(buf, p), ", %d minute%s", mi,
191 PLURAL(mi));
192 p += strlen(p);
194 if (se > 0)
196 (void) sm_snprintf(p, SPACELEFT(buf, p), ", %d second%s", se,
197 PLURAL(se));
198 p += strlen(p);
201 return (buf + 2);