12168Seric # include "../hdr/macros.h"
232351Sbostic # include <tzfile.h>
32168Seric
4*37836Sbostic static char Sccsid[] = "@(#)date_ab.c 4.6 05/10/89";
52168Seric
62168Seric /*
72168Seric Function to convert date in the form "yymmddhhmmss" to
82168Seric standard UNIX time (seconds since Jan. 1, 1970 GMT).
92168Seric Units left off of the right are replaced by their
102168Seric maximum possible values.
112168Seric
122168Seric The function corrects properly for leap year,
132168Seric daylight savings time, offset from Greenwich time, etc.
142168Seric
152168Seric Function returns -1 if bad time is given (i.e., "730229").
162168Seric */
1732351Sbostic
18*37836Sbostic #define dysize(year) (isleap(year) ? DAYSPERLYEAR : DAYSPERNYEAR)
1932351Sbostic
202168Seric char *Datep;
212168Seric
222168Seric
date_ab(adt,bdt)232168Seric date_ab(adt,bdt)
242168Seric char *adt;
252168Seric long *bdt;
262168Seric {
272168Seric int y, t, d, h, m, s, i;
282168Seric long tim;
292168Seric extern int *localtime();
302168Seric #define time_t long
312168Seric #include <sys/timeb.h>
322168Seric struct timeb timeb;
332168Seric
342168Seric ftime(&timeb);
352168Seric Datep = adt;
362168Seric
372168Seric if((y=g2()) == -2) y = 99;
382168Seric if(y<70 || y>99) return(-1);
392168Seric
402168Seric if((t=g2()) == -2) t = 12;
412168Seric if(t<1 || t>12) return(-1);
422168Seric
432168Seric if((d=g2()) == -2) d = mosize(y,t);
442168Seric if(d<1 || d>mosize(y,t)) return(-1);
452168Seric
462168Seric if((h=g2()) == -2) h = 23;
472168Seric if(h<0 || h>23) return(-1);
482168Seric
492168Seric if((m=g2()) == -2) m = 59;
502168Seric if(m<0 || m>59) return(-1);
512168Seric
522168Seric if((s=g2()) == -2) s = 59;
532168Seric if(s<0 || s>59) return(-1);
542168Seric
552168Seric tim = 0L;
5630497Slepreau y += 1900;
572168Seric for(i=1970; i<y; i++)
5830497Slepreau tim += dysize(i);
592168Seric while(--t)
6030497Slepreau tim += mosize(y,t);
6130497Slepreau tim += d - 1;
6230497Slepreau tim *= 24;
6330497Slepreau tim += h;
6430497Slepreau tim *= 60;
6530497Slepreau tim += m;
6630497Slepreau tim += timeb.timezone; /* GMT correction */
6730497Slepreau tim *= 60;
6830497Slepreau tim += s;
692168Seric
702168Seric if(localtime(&tim)[8])
7130497Slepreau tim += -1*60*60; /* daylight savings */
722168Seric *bdt = tim;
732168Seric return(0);
742168Seric }
752168Seric
762168Seric
mosize(y,t)772168Seric mosize(y,t)
782168Seric int y, t;
792168Seric {
8019936Ssam static int dmsize[12] =
8119936Ssam { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
822168Seric
832168Seric if(t==2 && dysize(y)==366) return(29);
842168Seric return(dmsize[t-1]);
852168Seric }
862168Seric
872168Seric
g2()882168Seric g2()
892168Seric {
902168Seric register int c;
912168Seric register char *p;
922168Seric
932168Seric for (p = Datep; *p; p++)
942168Seric if (numeric(*p))
952168Seric break;
962168Seric if (*p) {
972168Seric c = (*p++ - '0') * 10;
982168Seric if (*p)
9930497Slepreau c += (*p++ - '0');
1002168Seric else
1012168Seric c = -1;
1022168Seric }
1032168Seric else
1042168Seric c = -2;
1052168Seric Datep = p;
1062168Seric return(c);
1072168Seric }
108