|
|
1.1 root 1: /*
2: * Copyright (c) 1983 Eric P. Allman
3: * Copyright (c) 1988 Regents of the University of California.
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms are permitted provided
7: * that: (1) source distributions retain this entire copyright notice and
8: * comment, and (2) distributions including binaries display the following
9: * acknowledgement: ``This product includes software developed by the
10: * University of California, Berkeley and its contributors'' in the
11: * documentation or other materials provided with the distribution and in
12: * all advertising materials mentioning features or use of this software.
13: * Neither the name of the University nor the names of its contributors may
14: * be used to endorse or promote products derived from this software without
15: * specific prior written permission.
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
17: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
18: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19: */
20:
21: #ifndef lint
22: static char sccsid[] = "@(#)convtime.c 5.4 (Berkeley) 6/1/90";
23: #endif /* not lint */
24:
25: # include <ctype.h>
26: # include "useful.h"
27:
28: /*
29: ** CONVTIME -- convert time
30: **
31: ** Takes a time as an ascii string with a trailing character
32: ** giving units:
33: ** s -- seconds
34: ** m -- minutes
35: ** h -- hours
36: ** d -- days (default)
37: ** w -- weeks
38: ** For example, "3d12h" is three and a half days.
39: **
40: ** Parameters:
41: ** p -- pointer to ascii time.
42: **
43: ** Returns:
44: ** time in seconds.
45: **
46: ** Side Effects:
47: ** none.
48: */
49:
50: time_t
51: convtime(p)
52: char *p;
53: {
54: register time_t t, r;
55: register char c;
56:
57: r = 0;
58: while (*p != '\0')
59: {
60: t = 0;
61: while (isdigit(c = *p++))
62: t = t * 10 + (c - '0');
63: if (c == '\0')
64: p--;
65: switch (c)
66: {
67: case 'w': /* weeks */
68: t *= 7;
69:
70: case 'd': /* days */
71: default:
72: t *= 24;
73:
74: case 'h': /* hours */
75: t *= 60;
76:
77: case 'm': /* minutes */
78: t *= 60;
79:
80: case 's': /* seconds */
81: break;
82: }
83: r += t;
84: }
85:
86: return (r);
87: }
88: /*
89: ** PINTVL -- produce printable version of a time interval
90: **
91: ** Parameters:
92: ** intvl -- the interval to be converted
93: ** brief -- if TRUE, print this in an extremely compact form
94: ** (basically used for logging).
95: **
96: ** Returns:
97: ** A pointer to a string version of intvl suitable for
98: ** printing or framing.
99: **
100: ** Side Effects:
101: ** none.
102: **
103: ** Warning:
104: ** The string returned is in a static buffer.
105: */
106:
107: # define PLURAL(n) ((n) == 1 ? "" : "s")
108:
109: char *
110: pintvl(intvl, brief)
111: time_t intvl;
112: bool brief;
113: {
114: static char buf[256];
115: register char *p;
116: int wk, dy, hr, mi, se;
117:
118: if (intvl == 0 && !brief)
119: return ("zero seconds");
120:
121: /* decode the interval into weeks, days, hours, minutes, seconds */
122: se = intvl % 60;
123: intvl /= 60;
124: mi = intvl % 60;
125: intvl /= 60;
126: hr = intvl % 24;
127: intvl /= 24;
128: if (brief)
129: dy = intvl;
130: else
131: {
132: dy = intvl % 7;
133: intvl /= 7;
134: wk = intvl;
135: }
136:
137: /* now turn it into a sexy form */
138: p = buf;
139: if (brief)
140: {
141: if (dy > 0)
142: {
143: (void) sprintf(p, "%d+", dy);
144: p += strlen(p);
145: }
146: (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se);
147: return (buf);
148: }
149:
150: /* use the verbose form */
151: if (wk > 0)
152: {
153: (void) sprintf(p, ", %d week%s", wk, PLURAL(wk));
154: p += strlen(p);
155: }
156: if (dy > 0)
157: {
158: (void) sprintf(p, ", %d day%s", dy, PLURAL(dy));
159: p += strlen(p);
160: }
161: if (hr > 0)
162: {
163: (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr));
164: p += strlen(p);
165: }
166: if (mi > 0)
167: {
168: (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi));
169: p += strlen(p);
170: }
171: if (se > 0)
172: {
173: (void) sprintf(p, ", %d second%s", se, PLURAL(se));
174: p += strlen(p);
175: }
176:
177: return (buf + 2);
178: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.