1: /*
2: ** Sendmail
3: ** Copyright (c) 1983 Eric P. Allman
4: ** Berkeley, California
5: **
6: ** Copyright (c) 1983 Regents of the University of California.
7: ** All rights reserved. The Berkeley software License Agreement
8: ** specifies the terms and conditions for redistribution.
9: */
10:
11: #if !defined(lint) && !defined(NOSCCS)
12: static char SccsId[] = "@(#)convtime.c 5.1 (Berkeley) 6/7/85";
13: #endif
14:
15: # include <ctype.h>
16: # include "useful.h"
17:
18: /*
19: ** CONVTIME -- convert time
20: **
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.
29: **
30: ** Parameters:
31: ** p -- pointer to ascii time.
32: **
33: ** Returns:
34: ** time in seconds.
35: **
36: ** Side Effects:
37: ** none.
38: */
39:
40: time_t
41: convtime(p)
42: char *p;
43: {
44: register time_t t, r;
45: register char c;
46:
47: r = 0;
48: while (*p != '\0')
49: {
50: t = 0;
51: while (isdigit(c = *p++))
52: t = t * 10 + (c - '0');
53: if (c == '\0')
54: p--;
55: switch (c)
56: {
57: case 'w': /* weeks */
58: t *= 7;
59:
60: case 'd': /* days */
61: default:
62: t *= 24;
63:
64: case 'h': /* hours */
65: t *= 60;
66:
67: case 'm': /* minutes */
68: t *= 60;
69:
70: case 's': /* seconds */
71: break;
72: }
73: r += t;
74: }
75:
76: return (r);
77: }
78: /*
79: ** PINTVL -- produce printable version of a time interval
80: **
81: ** Parameters:
82: ** intvl -- the interval to be converted
83: ** brief -- if TRUE, print this in an extremely compact form
84: ** (basically used for logging).
85: **
86: ** Returns:
87: ** A pointer to a string version of intvl suitable for
88: ** printing or framing.
89: **
90: ** Side Effects:
91: ** none.
92: **
93: ** Warning:
94: ** The string returned is in a static buffer.
95: */
96:
97: # define PLURAL(n) ((n) == 1 ? "" : "s")
98:
99: char *
100: pintvl(intvl, brief)
101: time_t intvl;
102: bool brief;
103: {
104: static char buf[256];
105: register char *p;
106: int wk, dy, hr, mi, se;
107:
108: if (intvl == 0 && !brief)
109: return ("zero seconds");
110:
111: /* decode the interval into weeks, days, hours, minutes, seconds */
112: se = intvl % 60;
113: intvl /= 60;
114: mi = intvl % 60;
115: intvl /= 60;
116: hr = intvl % 24;
117: intvl /= 24;
118: if (brief)
119: dy = intvl;
120: else
121: {
122: dy = intvl % 7;
123: intvl /= 7;
124: wk = intvl;
125: }
126:
127: /* now turn it into a sexy form */
128: p = buf;
129: if (brief)
130: {
131: if (dy > 0)
132: {
133: (void) sprintf(p, "%d+", dy);
134: p += strlen(p);
135: }
136: (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se);
137: return (buf);
138: }
139:
140: /* use the verbose form */
141: if (wk > 0)
142: {
143: (void) sprintf(p, ", %d week%s", wk, PLURAL(wk));
144: p += strlen(p);
145: }
146: if (dy > 0)
147: {
148: (void) sprintf(p, ", %d day%s", dy, PLURAL(dy));
149: p += strlen(p);
150: }
151: if (hr > 0)
152: {
153: (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr));
154: p += strlen(p);
155: }
156: if (mi > 0)
157: {
158: (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi));
159: p += strlen(p);
160: }
161: if (se > 0)
162: {
163: (void) sprintf(p, ", %d second%s", se, PLURAL(se));
164: p += strlen(p);
165: }
166:
167: return (buf + 2);
168: }
Defined functions
Defined variables
Defined macros