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