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