home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <dos.h>
-
- #include "timutil.h"
-
- enum { JAN=1,FEB,MAR,APR,MAY,JUN, JUL,AUG,SEP,OCT,NOV,DEC,YEAREND};
- int dm[]={ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int dy[]={ 0, 0, 31, 59, 90,120,151, 181,212,243,273,304,334,365 };
-
-
- int tleap(int year)
- {
- return ((year&3)==0); /*simple leapyear, good 'til 2100*/
- } /*tleap*/
-
-
- int jtomd(TIME *t) /*julian day to month.day*/
- {
- int j;
-
- j=t->jday;
- t->mon=1;
- if (tleap(t->year)) dm[FEB]=29;
- while (j>dm[t->mon]) {
- j-=dm[t->mon]; /*subtract this month from julian day*/
- (t->mon)++; /*go to next month*/
- } /*while*/
- t->day=j;
- dm[FEB]=28;
- return t->jday;
- } /*jtomd*/
-
-
- TIME *tnow(TIME *t)
- {
- struct date td;
- struct time tt;
-
- getdate(&td);
- gettime(&tt);
- t->year=td.da_year;
- t->mon =td.da_mon;
- t->day =td.da_day;
- t->jday=dy[td.da_mon]+td.da_day;
- if (tleap(t->year)&&(t->mon>FEB)) t->jday++;
- t->hour=tt.ti_hour;
- t->min =tt.ti_min;
- t->sec =tt.ti_sec;
- t->hund=tt.ti_hund;
- return t;
- } /*tnow*/
-
-
- int tcmp(TIME *t1,TIME *t2)
- {
- if (t1->year!=t2->year) return (t1->year - t2->year);
- if (t1->jday!=t2->jday) return (t1->jday - t2->jday);
- if (t1->hour!=t2->hour) return (t1->hour - t2->hour);
- if (t1->min !=t2->min ) return (t1->min - t2->min );
- if (t1->sec !=t2->sec ) return (t1->sec - t2->sec );
- if (t1->hund!=t2->hund) return (t1->hund - t2->hund);
- return 0;
- } /*tcmp*/
-
-
- TIME *tadd(TIME *a1,TIME *a2,TIME *s) /*put delta times in a2 please*/
- {
- TIME t;
-
- if (tleap(a1->year)) dy[YEAREND]++; /*check leapyears*/
- t=*a1;
- t.year += a2->year;
- t.jday += a2->jday;
- t.hour += a2->hour;
- t.min += a2->min ;
- t.sec += a2->sec ;
- t.hund += a2->hund;
- while (t.hund>99) { t.hund-=100; t.sec++; }
- while (t.sec >59) { t.sec -=60; t.min++; }
- while (t.min >59) { t.min -=60; t.hour++; }
- while (t.hour>23) { t.hour-=24; t.jday++; }
- if (t.jday>dy[YEAREND]) { t.jday-=dy[YEAREND]; t.year++; }
- dy[YEAREND]=365; /*reset days in year*/
- while (t.jday>dy[YEAREND]) { t.jday-=dy[YEAREND]; t.year++; }
- jtomd(&t);
- *s=t;
- return s;
- } /*tadd*/
-
-
- TIME *tsub(TIME *s1,TIME *s2,TIME *d)
- {
- TIME t;
-
- if (tleap(s1->year)) dy[YEAREND]++; /*check leapyears*/
- t=*s1;
- t.year -= s2->year;
- t.jday -= s2->jday;
- t.hour -= s2->hour;
- t.min -= s2->min ;
- t.sec -= s2->sec ;
- t.hund -= s2->hund;
- while (t.hund<0) { t.hund+=100; t.sec--; }
- while (t.sec <0) { t.sec +=60; t.min--; }
- while (t.min <0) { t.min +=60; t.hour--; }
- while (t.hour<0) { t.hour+=24; t.jday--; }
- if (t.jday<0) { t.jday+=dy[YEAREND]; t.year--; }
- dy[YEAREND]=365; /*reset days in year*/
- while (t.jday<0) { t.jday+=dy[YEAREND]; t.year--; }
- jtomd(&t);
- *d=t;
- return d;
- } /*tsub*/
-
-
- int tout(TIME *to)
- {
- TIME now;
-
- tnow(&now);
- return (tcmp(&now,to)>0); /*timeout when now is later than then*/
- } /*tout*/
-
-
- TIME *toutsec(TIME *to,int sec)
- {
- TIME now;
-
- to->year=0;
- to->jday=0;
- to->hour=0;
- to->min =0;
- to->sec =sec;
- to->hund=0;
- tnow(&now);
- tadd(&now,to,to);
- return to;
- } /*toutsec*/
-
-
- TIME *touthun(TIME *to,int hun)
- {
- TIME now;
-
- to->year=0;
- to->jday=0;
- to->hour=0;
- to->min =0;
- to->sec =0;
- to->hund=hun;
- tnow(&now);
- tadd(&now,to,to);
- return to;
- } /*touthun*/
-
-
- char *tstr(TIME *t,int typ)
- {
- static char st[20];
-
- switch (typ) {
- case YYMMDD:
- sprintf(st,"%02u%02u%02u",t->year%100,t->mon,t->day);
- break;
- case YYYYMMDD:
- sprintf(st,"%04u%02u%02u",t->year,t->mon,t->day);
- break;
- case mm_HH_SS_hh: /*only time of day*/
- default:
- sprintf(st,"%02u:%02u:%02u.%02u",t->hour,t->min,t->sec,t->hund);
- } /*switch*/
- return st;
- } /*tstr*/
-
-