1 # include "../hdr/macros.h" 2 # include <tzfile.h> 3 4 static char Sccsid[] = "@(#)date_ab.c 4.6 05/10/89"; 5 6 /* 7 Function to convert date in the form "yymmddhhmmss" to 8 standard UNIX time (seconds since Jan. 1, 1970 GMT). 9 Units left off of the right are replaced by their 10 maximum possible values. 11 12 The function corrects properly for leap year, 13 daylight savings time, offset from Greenwich time, etc. 14 15 Function returns -1 if bad time is given (i.e., "730229"). 16 */ 17 18 #define dysize(year) (isleap(year) ? DAYSPERLYEAR : DAYSPERNYEAR) 19 20 char *Datep; 21 22 23 date_ab(adt,bdt) 24 char *adt; 25 long *bdt; 26 { 27 int y, t, d, h, m, s, i; 28 long tim; 29 extern int *localtime(); 30 #define time_t long 31 #include <sys/timeb.h> 32 struct timeb timeb; 33 34 ftime(&timeb); 35 Datep = adt; 36 37 if((y=g2()) == -2) y = 99; 38 if(y<70 || y>99) return(-1); 39 40 if((t=g2()) == -2) t = 12; 41 if(t<1 || t>12) return(-1); 42 43 if((d=g2()) == -2) d = mosize(y,t); 44 if(d<1 || d>mosize(y,t)) return(-1); 45 46 if((h=g2()) == -2) h = 23; 47 if(h<0 || h>23) return(-1); 48 49 if((m=g2()) == -2) m = 59; 50 if(m<0 || m>59) return(-1); 51 52 if((s=g2()) == -2) s = 59; 53 if(s<0 || s>59) return(-1); 54 55 tim = 0L; 56 y += 1900; 57 for(i=1970; i<y; i++) 58 tim += dysize(i); 59 while(--t) 60 tim += mosize(y,t); 61 tim += d - 1; 62 tim *= 24; 63 tim += h; 64 tim *= 60; 65 tim += m; 66 tim += timeb.timezone; /* GMT correction */ 67 tim *= 60; 68 tim += s; 69 70 if(localtime(&tim)[8]) 71 tim += -1*60*60; /* daylight savings */ 72 *bdt = tim; 73 return(0); 74 } 75 76 77 mosize(y,t) 78 int y, t; 79 { 80 static int dmsize[12] = 81 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 82 83 if(t==2 && dysize(y)==366) return(29); 84 return(dmsize[t-1]); 85 } 86 87 88 g2() 89 { 90 register int c; 91 register char *p; 92 93 for (p = Datep; *p; p++) 94 if (numeric(*p)) 95 break; 96 if (*p) { 97 c = (*p++ - '0') * 10; 98 if (*p) 99 c += (*p++ - '0'); 100 else 101 c = -1; 102 } 103 else 104 c = -2; 105 Datep = p; 106 return(c); 107 } 108