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

Defined functions

ltod defined in line 133; used 3 times

Defined variables

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

Defined macros

dysize defined in line 38; used 2 times
twodigtoi defined in line 37; used 6 times
Last modified: 1988-02-07
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3362
Valid CSS Valid XHTML 1.0 Strict