Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1983 Eric P. Allman | |
78ed81a3 | 3 | * Copyright (c) 1988, 1993 |
4 | * The Regents of the University of California. All rights reserved. | |
15637ed4 RG |
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 | |
78ed81a3 | 36 | static char sccsid[] = "@(#)convtime.c 8.1 (Berkeley) 6/7/93"; |
15637ed4 RG |
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. | |
78ed81a3 | 56 | ** units -- default units if none specified. |
15637ed4 RG |
57 | ** |
58 | ** Returns: | |
59 | ** time in seconds. | |
60 | ** | |
61 | ** Side Effects: | |
62 | ** none. | |
63 | */ | |
64 | ||
65 | time_t | |
78ed81a3 | 66 | convtime(p, units) |
15637ed4 | 67 | char *p; |
78ed81a3 | 68 | char units; |
15637ed4 RG |
69 | { |
70 | register time_t t, r; | |
71 | register char c; | |
72 | ||
73 | r = 0; | |
74 | while (*p != '\0') | |
75 | { | |
76 | t = 0; | |
78ed81a3 | 77 | while ((c = *p++) != '\0' && isascii(c) && isdigit(c)) |
15637ed4 RG |
78 | t = t * 10 + (c - '0'); |
79 | if (c == '\0') | |
78ed81a3 | 80 | { |
81 | c = units; | |
15637ed4 | 82 | p--; |
78ed81a3 | 83 | } |
15637ed4 RG |
84 | switch (c) |
85 | { | |
86 | case 'w': /* weeks */ | |
87 | t *= 7; | |
88 | ||
89 | case 'd': /* days */ | |
90 | default: | |
91 | t *= 24; | |
92 | ||
93 | case 'h': /* hours */ | |
94 | t *= 60; | |
95 | ||
96 | case 'm': /* minutes */ | |
97 | t *= 60; | |
98 | ||
99 | case 's': /* seconds */ | |
100 | break; | |
101 | } | |
102 | r += t; | |
103 | } | |
104 | ||
105 | return (r); | |
106 | } | |
107 | \f/* | |
108 | ** PINTVL -- produce printable version of a time interval | |
109 | ** | |
110 | ** Parameters: | |
111 | ** intvl -- the interval to be converted | |
112 | ** brief -- if TRUE, print this in an extremely compact form | |
113 | ** (basically used for logging). | |
114 | ** | |
115 | ** Returns: | |
116 | ** A pointer to a string version of intvl suitable for | |
117 | ** printing or framing. | |
118 | ** | |
119 | ** Side Effects: | |
120 | ** none. | |
121 | ** | |
122 | ** Warning: | |
123 | ** The string returned is in a static buffer. | |
124 | */ | |
125 | ||
126 | # define PLURAL(n) ((n) == 1 ? "" : "s") | |
127 | ||
128 | char * | |
129 | pintvl(intvl, brief) | |
130 | time_t intvl; | |
131 | bool brief; | |
132 | { | |
133 | static char buf[256]; | |
134 | register char *p; | |
135 | int wk, dy, hr, mi, se; | |
136 | ||
137 | if (intvl == 0 && !brief) | |
138 | return ("zero seconds"); | |
139 | ||
140 | /* decode the interval into weeks, days, hours, minutes, seconds */ | |
141 | se = intvl % 60; | |
142 | intvl /= 60; | |
143 | mi = intvl % 60; | |
144 | intvl /= 60; | |
145 | hr = intvl % 24; | |
146 | intvl /= 24; | |
147 | if (brief) | |
148 | dy = intvl; | |
149 | else | |
150 | { | |
151 | dy = intvl % 7; | |
152 | intvl /= 7; | |
153 | wk = intvl; | |
154 | } | |
155 | ||
156 | /* now turn it into a sexy form */ | |
157 | p = buf; | |
158 | if (brief) | |
159 | { | |
160 | if (dy > 0) | |
161 | { | |
162 | (void) sprintf(p, "%d+", dy); | |
163 | p += strlen(p); | |
164 | } | |
165 | (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se); | |
166 | return (buf); | |
167 | } | |
168 | ||
169 | /* use the verbose form */ | |
170 | if (wk > 0) | |
171 | { | |
172 | (void) sprintf(p, ", %d week%s", wk, PLURAL(wk)); | |
173 | p += strlen(p); | |
174 | } | |
175 | if (dy > 0) | |
176 | { | |
177 | (void) sprintf(p, ", %d day%s", dy, PLURAL(dy)); | |
178 | p += strlen(p); | |
179 | } | |
180 | if (hr > 0) | |
181 | { | |
182 | (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr)); | |
183 | p += strlen(p); | |
184 | } | |
185 | if (mi > 0) | |
186 | { | |
187 | (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi)); | |
188 | p += strlen(p); | |
189 | } | |
190 | if (se > 0) | |
191 | { | |
192 | (void) sprintf(p, ", %d second%s", se, PLURAL(se)); | |
193 | p += strlen(p); | |
194 | } | |
195 | ||
196 | return (buf + 2); | |
197 | } |