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

Defined functions

pintvl defined in line 105; used 4 times

Defined variables

sccsid defined in line 18; never used

Defined macros

PLURAL defined in line 103; used 5 times
Last modified: 1988-09-14
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2064
Valid CSS Valid XHTML 1.0 Strict