1: #ifndef lint
   2: static char *sccsid = "@(#)time.c	1.4	(Berkeley) 3/20/86";
   3: #endif
   4: 
   5: /*
   6:  * Collection of routines for dealing with ASCII time strings.
   7:  * These may actually be useful in their own right.
   8:  */
   9: 
  10: #include <stdio.h>
  11: #include <sys/types.h>
  12: #include <sys/time.h>
  13: #include <ctype.h>
  14: #include <strings.h>
  15: 
  16: /*
  17:  * dtol -- convert date to long integer.  This is not implicitly
  18:  * local time, or any other kind of time, for that matter.  If you
  19:  * pass it a date you think is GMT, you wind up with that number of
  20:  * seconds...
  21:  *
  22:  *	Parameters:		"date_ascii" is in the form "yymmddhhmmss".
  23:  *
  24:  *	Returns:		Long integer containing number
  25:  *				of seconds since 000000 Jan 1, 1970,
  26:  *				and "date".  -1 on error.
  27:  *
  28:  *	Side effects:		None.
  29:  */
  30: 
  31: #define twodigtoi(x)    (((*x++) - '0') + (*x++ - '0')*10)
  32: #define dysize(y)   ((y % 4 ? 365 : 366))
  33: 
  34: static  int dmsize[12] =
  35:     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  36: 
  37: long
  38: dtol(date_ascii)
  39: char    *date_ascii;
  40: {
  41:     char    date[32], *date_str;
  42:     char    *lhs, *rhs;
  43:     char    temp;
  44:     long    seconds;
  45:     int year, month, day, hour, mins, secs;
  46:     int len, i;
  47: 
  48:     len = strlen(date_ascii);
  49:     if (len != sizeof("yymmddhhmmss")-1)
  50:         return (-1);
  51: 
  52:     (void) strcpy(date, date_ascii);
  53:     date_str = date;
  54: 
  55: #ifdef debug
  56:     printf("date_str = %s\n", date_str);
  57: #endif
  58:     rhs = date_str + len - 1;
  59:     lhs = date_str;
  60: 
  61:     for (; lhs < rhs; ++lhs, --rhs) {
  62:         temp = *lhs;
  63:         *lhs = *rhs;
  64:         *rhs = temp;
  65:         if (!isdigit(temp) || !isdigit(*lhs))
  66:             return (-1);
  67:     }
  68: 
  69:     lhs = date_str;
  70: #ifdef debug
  71:     printf("date_str = %s\n", date_str);
  72: #endif
  73: 
  74:     secs = twodigtoi(lhs);
  75:     mins = twodigtoi(lhs);
  76:     hour = twodigtoi(lhs);
  77:     day = twodigtoi(lhs);
  78:     month = twodigtoi(lhs);
  79:     year = twodigtoi(lhs);
  80: 
  81:     if (month < 1 || month > 12 ||
  82:         day < 1 || day > 31 ||
  83:         mins < 0 || mins > 59 ||
  84:         secs < 0 || secs > 59)
  85:         return (-1);
  86:     if (hour == 24) {
  87:         hour = 0;
  88:         day++;
  89:     }
  90:     if (hour < 0 || hour > 23)
  91:         return (-1);
  92:     seconds = 0;
  93:     year += 1900;
  94:     for (i = 1970; i < year; i++)
  95:         seconds += dysize(i);
  96:     /* Leap year */
  97:     if (dysize(year) == 366 && month >= 3)
  98:         seconds++;
  99:     while (--month)
 100:         seconds += dmsize[month-1];
 101:     seconds += day-1;
 102:     seconds = 24 * seconds + hour;
 103:     seconds = 60 * seconds + mins;
 104:     seconds = 60 * seconds + secs;
 105: 
 106:     return (seconds);
 107: }
 108: 
 109: 
 110: /*
 111:  * ltod -- convert long integer to date string.
 112:  *
 113:  *	Parameters:		"date" is in the number of seconds
 114:  *				since the epoch.
 115:  *
 116:  *	Returns:		Pointer to static data in the form
 117:  *				yymmddhhmmss\0.
 118:  *
 119:  *	Side effects:		None.
 120:  */
 121: 
 122: char *
 123: ltod(date)
 124: long    date;
 125: {
 126:     static char timebuf[32];
 127:     struct tm   *tp;
 128: 
 129:     tp = gmtime(&date);
 130: 
 131:     sprintf(timebuf, "%02d%02d%02d%02d%02d%02d",
 132:         tp->tm_year,
 133:         tp->tm_mon + 1,     /* 0 to 11??? How silly. */
 134:         tp->tm_mday,
 135:         tp->tm_hour,
 136:         tp->tm_min,
 137:         tp->tm_sec);
 138: 
 139:     return (timebuf);
 140: }
 141: 
 142: 
 143: /*
 144:  * local_to_gmt -- convert a local time (in form of number of
 145:  * seconds since you-know-when) to GMT.
 146:  *
 147:  *	Parameters:	"date" is date we want converted, in
 148:  *			seconds since 000000 1 Jan 1970.
 149:  *
 150:  *	Returns:	Number of seconds corrected for local
 151:  *			and dst.
 152:  */
 153: 
 154: long
 155: local_to_gmt(date)
 156: long    date;
 157: {
 158:     struct  timeval tv;
 159:     struct  timezone tz;
 160: 
 161:     (void) gettimeofday(&tv, &tz);
 162:     date += (long) tz.tz_minuteswest * 60;
 163: 
 164:     /* now fix up local daylight time */
 165:     if (localtime((time_t *)&date)->tm_isdst)
 166:         date -= 60*60;
 167: 
 168:     return (date);
 169: }
 170: 
 171: /*
 172:  * gmt_to_local -- take a GMT time expressed in seconds since
 173:  * the epoch, and convert it to local time.
 174:  *
 175:  *	Parameters:	"date" is the number of seconds...
 176:  *
 177:  *	Returns:	Number of seconds corrected to reflect
 178:  *			local time and dst.
 179:  */
 180: 
 181: long
 182: gmt_to_local(date)
 183: long    date;
 184: {
 185:     struct  timeval tv;
 186:     struct  timezone tz;
 187: 
 188:     (void) gettimeofday(&tv, &tz);
 189:     date -= (long) tz.tz_minuteswest * 60;
 190: 
 191:     /* now fix up local daylight time */
 192:     if (localtime((time_t *)&date)->tm_isdst)
 193:         date += 60*60;
 194: 
 195:     return (date);
 196: }

Defined functions

dtol defined in line 37; used 4 times
gmt_to_local defined in line 181; used 2 times
local_to_gmt defined in line 154; used 2 times
ltod defined in line 122; used 2 times

Defined variables

dmsize defined in line 34; used 1 times
sccsid defined in line 2; never used

Defined macros

dysize defined in line 32; used 2 times
twodigtoi defined in line 31; used 6 times
Last modified: 1986-03-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1047
Valid CSS Valid XHTML 1.0 Strict