home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c222 / 1.ddi / SOURCE / CLIB / UT_DTADD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-18  |  6.0 KB  |  296 lines

  1. /*********************
  2.  *
  3.  *  ut_dtadd.c - date math functions.
  4.  *
  5.  *  Purpose: This file contains funcitons to operate on the date and time.
  6.  *
  7.  *  Blackstar C Function Library
  8.  *  (c) Copyright 1985,1989 Sterling Castle Software
  9.  *
  10.  *******/
  11.  
  12. #include "blackstr.h"
  13. #include "ut_head.h"
  14.  
  15.  
  16. /********
  17.  *
  18.  *   ut_datecmp(sdate1,sdate2) - compare  late structures sdate1 to sdate2
  19.  *
  20.  **/
  21.  
  22.  
  23. int ut_datecmp(struct DATE *sdate1, struct DATE *sdate2)
  24. {
  25.     int i;
  26.  
  27.     if((i = sdate1->yr - sdate2->yr) != 0)
  28.     ;
  29.     else
  30.     if ((i = sdate1->mo - sdate2->mo) != 0)
  31.         ;
  32.     else
  33.         i = sdate1->dy - sdate2->dy;
  34.  
  35.     if (i == 0)
  36.     return (0);
  37.     else
  38.     return(i/abs(i));
  39. }
  40.  
  41.  
  42. /********
  43.  *
  44.  *   ut_datesub(sdate1,sdate2) - subtract date structure sdate2 from sdate1
  45.  *
  46.  **/
  47.  
  48. int ut_datesub(struct DATE *sdate1, struct DATE *sdate2)
  49. {
  50.     sdate1->yr -= sdate2->yr;
  51.     sdate1->mo -= sdate2->mo;
  52.     sdate1->dy -= sdate2->dy;
  53.     while(sdate1->mo <= 0) {
  54.     sdate1->yr--;
  55.     sdate1->mo += 12;
  56.     }
  57.     while(sdate1->dy <= 0) {
  58.     if(!(--(sdate1->mo))) {
  59.         sdate1->mo = 12;                /* previous year */
  60.         sdate1->yr--;
  61.     }
  62.     sdate1->dy += mo_days_[sdate1->mo];     /* add last mo */
  63.     if((sdate1->yr % 4)&& sdate1->mo==2)    /* leap feb day */
  64.         sdate1->dy++;
  65.     }
  66.     return(sdate1->dy);             /* return days difference */
  67. }
  68.  
  69.  
  70. /********
  71.  *
  72.  *   ut_dateadd(sdate1,sdate2) - add sdate2 to sdate1
  73.  *
  74.  **/
  75.  
  76. void ut_dateadd(struct DATE *sdate1, struct DATE *sdate2)
  77. {
  78.     sdate1->yr += sdate2->yr;
  79.     sdate1->mo += sdate2->mo;
  80.     sdate1->dy += sdate2->dy;
  81.     while(sdate1->mo >12) {
  82.     sdate1->yr++;
  83.     sdate1->mo -= 12;
  84.     }
  85.     while(sdate1->dy>mo_days_[sdate1->mo]) {
  86.     sdate1->dy -= mo_days_[sdate1->mo];
  87.     sdate1->mo++;
  88.     }
  89.     if(sdate1->mo>12) {
  90.     sdate1->yr++;
  91.     sdate1->mo -= 12;
  92.     }
  93.  
  94.     /*  Now adjust for leap year feb */
  95.     if(!(sdate1->yr%4)) {
  96.     if(sdate1->mo==3)
  97.         --(sdate1->dy); /* adj for feb 29 */
  98.     if(!sdate1->dy) {
  99.         sdate1->dy= 29; /* adjust to feb 29 */
  100.         sdate1->mo = 2;
  101.     }
  102.     }
  103. }
  104.  
  105.  
  106. /********
  107.  *
  108.  *   ut_datedif(sdate1,sdate2) - get difference sdate2 from sdate1
  109.  *
  110.  **/
  111.  
  112. int ut_datedif(struct DATE *sdate1, struct DATE *sdate2)
  113. {
  114.     sdate1->yr -= sdate2->yr;
  115.     sdate1->mo -= sdate2->mo;
  116.     sdate1->dy -= sdate2->dy;
  117.     while(sdate1->mo < 0) {
  118.     sdate1->yr--;                           /* if sdate1<sdate2 then year is negative */
  119.     sdate1->mo += 12;
  120.     }
  121.     while(sdate1->dy < 0) {          /* If days are negative */
  122.     if(!sdate1->mo) {
  123.         sdate1->mo = 12;                                /* previous year */
  124.         sdate1->yr--;
  125.     }else
  126.         sdate1->mo--;                            /* decrement month and add its days */
  127.     sdate1->dy += mo_days_[sdate1->mo+sdate2->mo];/* add last mo */
  128.     }
  129.     return(sdate1->dy);             /* return days difference */
  130. }
  131.  
  132.  
  133. /********
  134.  *
  135.  *   ut_ldatedif(ldate1,ldate2) - get difference of long dates
  136.  *
  137.  **/
  138.  
  139. long ut_ldatedif(long ldate1, long ldate2)
  140. {
  141.     struct DATE tdate1,tdate2;
  142.  
  143.     ut_ldates(&tdate1,ldate1);      /* convert to structure */
  144.     ut_ldates(&tdate2,ldate2);
  145.     ut_datedif(&tdate1,&tdate2);
  146.     return((tdate1.yr%1900)*10000L + tdate1.mo*100 + tdate1.dy);
  147. }
  148.  
  149.  
  150. /********
  151.  *
  152.  *   ut_timecmp(time1,time2) - compare two times
  153.  *
  154.  **/
  155.  
  156. int ut_timecmp(struct TIME *time1, struct TIME *time2) /* return -1,0,1 from time1-time2 */
  157. {
  158.     int i;
  159.  
  160.     if ((i = time1->hr - time2->hr) != 0)
  161.     ;
  162.     else if ((i = time1->mn - time2->mn) != 0)
  163.     ;
  164.     else if ((i = time1->sec - time2->sec) != 0)
  165.     ;
  166.     else
  167.     i = time1->hsec - time2->hsec;
  168.    
  169.     if (i == 0)
  170.     return (0);
  171.     else
  172.     return(i/abs(i));
  173. }
  174.  
  175.  
  176. /********
  177.  *
  178.  *   ut_timeadd(time1,time2) - add time2 to time1
  179.  *
  180.  **/
  181.  
  182. void ut_timeadd(struct TIME *time1, struct TIME *time2)
  183. {
  184.     time1->hr += time2->hr;
  185.     time1->mn += time2->mn;
  186.     time1->sec += time2->sec;
  187.     time1->hsec += time2->hsec;
  188.     while(time1->hsec>99) {
  189.     time1->sec++;
  190.     time1->hsec -= 100;
  191.     }
  192.     while(time1->sec > 59) {
  193.     time1->mn++;
  194.     time1->sec -= 60;
  195.     }
  196.     while(time1->mn > 59) {
  197.     time1->hr++;
  198.     time1->mn -= 60;
  199.     }
  200. }
  201.  
  202.  
  203. /********
  204.  *
  205.  *   ut_timesub(time1,time2) - subtract time2 from time1
  206.  *
  207.  **/
  208.  
  209. void ut_timesub(struct TIME *time1, struct TIME *time2)
  210. {
  211.     time1->hr -= time2->hr;
  212.     time1->mn -= time2->mn;
  213.     time1->sec -= time2->sec;
  214.     time1->hsec -= time2->hsec;
  215.     while(time1->hsec < 0) {
  216.     time1->sec--;
  217.     time1->hsec += 100;
  218.     }
  219.     while(time1->sec < 0) {
  220.     time1->mn--;
  221.     time1->sec += 60;
  222.     }
  223.     while(time1->mn < 0) {
  224.     time1->hr--;
  225.     time1->mn += 60;
  226.     }
  227. }
  228.  
  229.  
  230. /********
  231.  *
  232.  *   ut_ldateadd(ldate1,ldate2) - add long dates
  233.  *
  234.  **/
  235.  
  236. long ut_ldateadd(long ldate1, long ldate2)
  237. {
  238.     struct DATE tdate1,tdate2;
  239.  
  240.     ut_ldates(&tdate1,ldate1);      /* convert to structure */
  241.     ut_ldates(&tdate2,ldate2);
  242.     ut_dateadd(&tdate1,&tdate2);
  243.     return((tdate1.yr%1900)*10000L + tdate1.mo*100 + tdate1.dy);
  244. }
  245.  
  246.  
  247. /********
  248.  *
  249.  *   ut_ldatesub(ldate1,ldate2) - subtract long dates
  250.  *
  251.  **/
  252.  
  253. long ut_ldatesub(long ldate1, long ldate2)
  254. {
  255.     struct DATE tdate1,tdate2;
  256.  
  257.     ut_ldates(&tdate1,ldate1);      /* convert to structure */
  258.     ut_ldates(&tdate2,ldate2);
  259.     ut_datesub(&tdate1,&tdate2);
  260.     return((tdate1.yr%1900)*10000L + tdate1.mo*100 + tdate1.dy);
  261. }
  262.  
  263.  
  264. /********
  265.  *
  266.  *   ut_ltimeadd(ltime1,ltime2) - add long times
  267.  *
  268.  **/
  269.  
  270. long ut_ltimeadd(long ltime1, long ltime2)
  271. {
  272.     struct TIME ttime1,ttime2;
  273.  
  274.     ut_ltimes(&ttime1,ltime1);      /* convert to structure */
  275.     ut_ltimes(&ttime2,ltime2);
  276.     ut_timeadd(&ttime1,&ttime2);
  277.     return(ttime1.hr*10000L + ttime1.mn*100 + ttime1.sec);
  278. }
  279.  
  280.  
  281. /********
  282.  *
  283.  *   ut_ltimesub(ltime1,ltime2) - subtract long times
  284.  *
  285.  **/
  286.  
  287. long ut_ltimesub(long ltime1, long ltime2)
  288. {
  289.     struct TIME ttime1,ttime2;
  290.  
  291.     ut_ltimes(&ttime1,ltime1);      /* convert to structure */
  292.     ut_ltimes(&ttime2,ltime2);
  293.     ut_timesub(&ttime1,&ttime2);
  294.     return(ttime1.hr*10000L + ttime1.mn*100 + ttime1.sec);
  295. }
  296.