home *** CD-ROM | disk | FTP | other *** search
- /*********************
- *
- * ut_dtadd.c - date math functions.
- *
- * Purpose: This file contains funcitons to operate on the date and time.
- *
- * Blackstar C Function Library
- * (c) Copyright 1985,1989 Sterling Castle Software
- *
- *******/
-
- #include "blackstr.h"
- #include "ut_head.h"
-
-
- /********
- *
- * ut_datecmp(sdate1,sdate2) - compare late structures sdate1 to sdate2
- *
- **/
-
-
- int ut_datecmp(struct DATE *sdate1, struct DATE *sdate2)
- {
- int i;
-
- if((i = sdate1->yr - sdate2->yr) != 0)
- ;
- else
- if ((i = sdate1->mo - sdate2->mo) != 0)
- ;
- else
- i = sdate1->dy - sdate2->dy;
-
- if (i == 0)
- return (0);
- else
- return(i/abs(i));
- }
-
-
- /********
- *
- * ut_datesub(sdate1,sdate2) - subtract date structure sdate2 from sdate1
- *
- **/
-
- int ut_datesub(struct DATE *sdate1, struct DATE *sdate2)
- {
- sdate1->yr -= sdate2->yr;
- sdate1->mo -= sdate2->mo;
- sdate1->dy -= sdate2->dy;
- while(sdate1->mo <= 0) {
- sdate1->yr--;
- sdate1->mo += 12;
- }
- while(sdate1->dy <= 0) {
- if(!(--(sdate1->mo))) {
- sdate1->mo = 12; /* previous year */
- sdate1->yr--;
- }
- sdate1->dy += mo_days_[sdate1->mo]; /* add last mo */
- if((sdate1->yr % 4)&& sdate1->mo==2) /* leap feb day */
- sdate1->dy++;
- }
- return(sdate1->dy); /* return days difference */
- }
-
-
- /********
- *
- * ut_dateadd(sdate1,sdate2) - add sdate2 to sdate1
- *
- **/
-
- void ut_dateadd(struct DATE *sdate1, struct DATE *sdate2)
- {
- sdate1->yr += sdate2->yr;
- sdate1->mo += sdate2->mo;
- sdate1->dy += sdate2->dy;
- while(sdate1->mo >12) {
- sdate1->yr++;
- sdate1->mo -= 12;
- }
- while(sdate1->dy>mo_days_[sdate1->mo]) {
- sdate1->dy -= mo_days_[sdate1->mo];
- sdate1->mo++;
- }
- if(sdate1->mo>12) {
- sdate1->yr++;
- sdate1->mo -= 12;
- }
-
- /* Now adjust for leap year feb */
- if(!(sdate1->yr%4)) {
- if(sdate1->mo==3)
- --(sdate1->dy); /* adj for feb 29 */
- if(!sdate1->dy) {
- sdate1->dy= 29; /* adjust to feb 29 */
- sdate1->mo = 2;
- }
- }
- }
-
-
- /********
- *
- * ut_datedif(sdate1,sdate2) - get difference sdate2 from sdate1
- *
- **/
-
- int ut_datedif(struct DATE *sdate1, struct DATE *sdate2)
- {
- sdate1->yr -= sdate2->yr;
- sdate1->mo -= sdate2->mo;
- sdate1->dy -= sdate2->dy;
- while(sdate1->mo < 0) {
- sdate1->yr--; /* if sdate1<sdate2 then year is negative */
- sdate1->mo += 12;
- }
- while(sdate1->dy < 0) { /* If days are negative */
- if(!sdate1->mo) {
- sdate1->mo = 12; /* previous year */
- sdate1->yr--;
- }else
- sdate1->mo--; /* decrement month and add its days */
- sdate1->dy += mo_days_[sdate1->mo+sdate2->mo];/* add last mo */
- }
- return(sdate1->dy); /* return days difference */
- }
-
-
- /********
- *
- * ut_ldatedif(ldate1,ldate2) - get difference of long dates
- *
- **/
-
- long ut_ldatedif(long ldate1, long ldate2)
- {
- struct DATE tdate1,tdate2;
-
- ut_ldates(&tdate1,ldate1); /* convert to structure */
- ut_ldates(&tdate2,ldate2);
- ut_datedif(&tdate1,&tdate2);
- return((tdate1.yr%1900)*10000L + tdate1.mo*100 + tdate1.dy);
- }
-
-
- /********
- *
- * ut_timecmp(time1,time2) - compare two times
- *
- **/
-
- int ut_timecmp(struct TIME *time1, struct TIME *time2) /* return -1,0,1 from time1-time2 */
- {
- int i;
-
- if ((i = time1->hr - time2->hr) != 0)
- ;
- else if ((i = time1->mn - time2->mn) != 0)
- ;
- else if ((i = time1->sec - time2->sec) != 0)
- ;
- else
- i = time1->hsec - time2->hsec;
-
- if (i == 0)
- return (0);
- else
- return(i/abs(i));
- }
-
-
- /********
- *
- * ut_timeadd(time1,time2) - add time2 to time1
- *
- **/
-
- void ut_timeadd(struct TIME *time1, struct TIME *time2)
- {
- time1->hr += time2->hr;
- time1->mn += time2->mn;
- time1->sec += time2->sec;
- time1->hsec += time2->hsec;
- while(time1->hsec>99) {
- time1->sec++;
- time1->hsec -= 100;
- }
- while(time1->sec > 59) {
- time1->mn++;
- time1->sec -= 60;
- }
- while(time1->mn > 59) {
- time1->hr++;
- time1->mn -= 60;
- }
- }
-
-
- /********
- *
- * ut_timesub(time1,time2) - subtract time2 from time1
- *
- **/
-
- void ut_timesub(struct TIME *time1, struct TIME *time2)
- {
- time1->hr -= time2->hr;
- time1->mn -= time2->mn;
- time1->sec -= time2->sec;
- time1->hsec -= time2->hsec;
- while(time1->hsec < 0) {
- time1->sec--;
- time1->hsec += 100;
- }
- while(time1->sec < 0) {
- time1->mn--;
- time1->sec += 60;
- }
- while(time1->mn < 0) {
- time1->hr--;
- time1->mn += 60;
- }
- }
-
-
- /********
- *
- * ut_ldateadd(ldate1,ldate2) - add long dates
- *
- **/
-
- long ut_ldateadd(long ldate1, long ldate2)
- {
- struct DATE tdate1,tdate2;
-
- ut_ldates(&tdate1,ldate1); /* convert to structure */
- ut_ldates(&tdate2,ldate2);
- ut_dateadd(&tdate1,&tdate2);
- return((tdate1.yr%1900)*10000L + tdate1.mo*100 + tdate1.dy);
- }
-
-
- /********
- *
- * ut_ldatesub(ldate1,ldate2) - subtract long dates
- *
- **/
-
- long ut_ldatesub(long ldate1, long ldate2)
- {
- struct DATE tdate1,tdate2;
-
- ut_ldates(&tdate1,ldate1); /* convert to structure */
- ut_ldates(&tdate2,ldate2);
- ut_datesub(&tdate1,&tdate2);
- return((tdate1.yr%1900)*10000L + tdate1.mo*100 + tdate1.dy);
- }
-
-
- /********
- *
- * ut_ltimeadd(ltime1,ltime2) - add long times
- *
- **/
-
- long ut_ltimeadd(long ltime1, long ltime2)
- {
- struct TIME ttime1,ttime2;
-
- ut_ltimes(&ttime1,ltime1); /* convert to structure */
- ut_ltimes(&ttime2,ltime2);
- ut_timeadd(&ttime1,&ttime2);
- return(ttime1.hr*10000L + ttime1.mn*100 + ttime1.sec);
- }
-
-
- /********
- *
- * ut_ltimesub(ltime1,ltime2) - subtract long times
- *
- **/
-
- long ut_ltimesub(long ltime1, long ltime2)
- {
- struct TIME ttime1,ttime2;
-
- ut_ltimes(&ttime1,ltime1); /* convert to structure */
- ut_ltimes(&ttime2,ltime2);
- ut_timesub(&ttime1,&ttime2);
- return(ttime1.hr*10000L + ttime1.mn*100 + ttime1.sec);
- }
-