Commit | Line | Data |
---|---|---|
15637ed4 RG |
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, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * 3. All advertising materials mentioning features or use of this software | |
15 | * must display the following acknowledgement: | |
16 | * This product includes software developed by the University of | |
17 | * California, Berkeley and its contributors. | |
18 | * 4. Neither the name of the University nor the names of its contributors | |
19 | * may be used to endorse or promote products derived from this software | |
20 | * without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | |
33 | */ | |
34 | ||
35 | #ifndef lint | |
36 | static char sccsid[] = "@(#)convtime.c 5.4 (Berkeley) 6/1/90"; | |
37 | #endif /* not lint */ | |
38 | ||
39 | # include <ctype.h> | |
40 | # include "useful.h" | |
41 | ||
42 | /* | |
43 | ** CONVTIME -- convert time | |
44 | ** | |
45 | ** Takes a time as an ascii string with a trailing character | |
46 | ** giving units: | |
47 | ** s -- seconds | |
48 | ** m -- minutes | |
49 | ** h -- hours | |
50 | ** d -- days (default) | |
51 | ** w -- weeks | |
52 | ** For example, "3d12h" is three and a half days. | |
53 | ** | |
54 | ** Parameters: | |
55 | ** p -- pointer to ascii time. | |
56 | ** | |
57 | ** Returns: | |
58 | ** time in seconds. | |
59 | ** | |
60 | ** Side Effects: | |
61 | ** none. | |
62 | */ | |
63 | ||
64 | time_t | |
65 | convtime(p) | |
66 | char *p; | |
67 | { | |
68 | register time_t t, r; | |
69 | register char c; | |
70 | ||
71 | r = 0; | |
72 | while (*p != '\0') | |
73 | { | |
74 | t = 0; | |
75 | while (isdigit(c = *p++)) | |
76 | t = t * 10 + (c - '0'); | |
77 | if (c == '\0') | |
78 | p--; | |
79 | switch (c) | |
80 | { | |
81 | case 'w': /* weeks */ | |
82 | t *= 7; | |
83 | ||
84 | case 'd': /* days */ | |
85 | default: | |
86 | t *= 24; | |
87 | ||
88 | case 'h': /* hours */ | |
89 | t *= 60; | |
90 | ||
91 | case 'm': /* minutes */ | |
92 | t *= 60; | |
93 | ||
94 | case 's': /* seconds */ | |
95 | break; | |
96 | } | |
97 | r += t; | |
98 | } | |
99 | ||
100 | return (r); | |
101 | } | |
102 | \f/* | |
103 | ** PINTVL -- produce printable version of a time interval | |
104 | ** | |
105 | ** Parameters: | |
106 | ** intvl -- the interval to be converted | |
107 | ** brief -- if TRUE, print this in an extremely compact form | |
108 | ** (basically used for logging). | |
109 | ** | |
110 | ** Returns: | |
111 | ** A pointer to a string version of intvl suitable for | |
112 | ** printing or framing. | |
113 | ** | |
114 | ** Side Effects: | |
115 | ** none. | |
116 | ** | |
117 | ** Warning: | |
118 | ** The string returned is in a static buffer. | |
119 | */ | |
120 | ||
121 | # define PLURAL(n) ((n) == 1 ? "" : "s") | |
122 | ||
123 | char * | |
124 | pintvl(intvl, brief) | |
125 | time_t intvl; | |
126 | bool brief; | |
127 | { | |
128 | static char buf[256]; | |
129 | register char *p; | |
130 | int wk, dy, hr, mi, se; | |
131 | ||
132 | if (intvl == 0 && !brief) | |
133 | return ("zero seconds"); | |
134 | ||
135 | /* decode the interval into weeks, days, hours, minutes, seconds */ | |
136 | se = intvl % 60; | |
137 | intvl /= 60; | |
138 | mi = intvl % 60; | |
139 | intvl /= 60; | |
140 | hr = intvl % 24; | |
141 | intvl /= 24; | |
142 | if (brief) | |
143 | dy = intvl; | |
144 | else | |
145 | { | |
146 | dy = intvl % 7; | |
147 | intvl /= 7; | |
148 | wk = intvl; | |
149 | } | |
150 | ||
151 | /* now turn it into a sexy form */ | |
152 | p = buf; | |
153 | if (brief) | |
154 | { | |
155 | if (dy > 0) | |
156 | { | |
157 | (void) sprintf(p, "%d+", dy); | |
158 | p += strlen(p); | |
159 | } | |
160 | (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se); | |
161 | return (buf); | |
162 | } | |
163 | ||
164 | /* use the verbose form */ | |
165 | if (wk > 0) | |
166 | { | |
167 | (void) sprintf(p, ", %d week%s", wk, PLURAL(wk)); | |
168 | p += strlen(p); | |
169 | } | |
170 | if (dy > 0) | |
171 | { | |
172 | (void) sprintf(p, ", %d day%s", dy, PLURAL(dy)); | |
173 | p += strlen(p); | |
174 | } | |
175 | if (hr > 0) | |
176 | { | |
177 | (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr)); | |
178 | p += strlen(p); | |
179 | } | |
180 | if (mi > 0) | |
181 | { | |
182 | (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi)); | |
183 | p += strlen(p); | |
184 | } | |
185 | if (se > 0) | |
186 | { | |
187 | (void) sprintf(p, ", %d second%s", se, PLURAL(se)); | |
188 | p += strlen(p); | |
189 | } | |
190 | ||
191 | return (buf + 2); | |
192 | } |