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