home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CSAPE32.ARJ / SOURCE / OWLSCR / TMELAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-28  |  3.8 KB  |  182 lines

  1. /*
  2.     tmelap.c    2/7/87
  3.  
  4.     % tm_ElapSecs, tm_ElapDays, tm_Cmp
  5.  
  6.     Written by John Cooke.
  7.  
  8.     OWL 1.2
  9.     Copyright (c) 1986-1989, by Oakland Group, Inc.
  10.     ALL RIGHTS RESERVED.
  11.  
  12.     Revision History:
  13.     -----------------
  14.      8/07/89 jmd    Split from cstime.c
  15.      3/10/90 pmcm    Changed tm_Cmp to use tm_mon & tm_mday rather than tm_yday
  16.      3/28/90 jmd    ansi-fied
  17.      8/01/90 ted    Moved to OWL; include oaktime.h not cstime.h.
  18. */
  19.  
  20. #include "oakhead.h"
  21.  
  22. #include <time.h>
  23. #include "oaktime.h"
  24.  
  25. long tm_ElapSecs(struct tm *t1, struct tm *t2)
  26. /*
  27. DESCRIPTION:
  28.     The tm_ElapSecs function returns the elapsed time in 
  29.     seconds between t1 and t2.
  30. RETURNS:
  31.     Returns the elapsed time in seconds.
  32. */
  33. {
  34.     struct tm *temp;
  35.     register int i;
  36.     int  leap, sign, years, leapyears, mod;
  37.     long seconds;
  38.  
  39.     sign = 1;
  40.     if ((sign = tm_Cmp(t2,t1)) <= 0) {
  41.         temp = t2;
  42.         t2 = t1;
  43.         t1 = temp;
  44.     }        
  45.     seconds = 0;
  46.  
  47.     if (t2->tm_year - t1->tm_year > 68)    {
  48.         return(TM_OVERFLOW);
  49.     }
  50.  
  51.     if (t2->tm_year > t1->tm_year) {
  52.          years = t2->tm_year-t1->tm_year;
  53.         leapyears = years/4;
  54.         if ((mod = (years%4)-1) >= 0) {
  55.             for (i = 0; i <= mod; i++) {
  56.                 if (tm_LeapYear(t1->tm_year+i)) leapyears++;
  57.             }
  58.         }
  59.         years -= leapyears;
  60.         seconds = (long)(years*(60L*60L*24L*365L)+leapyears*(60L*60L*24L*366L));
  61.     }
  62.      leap = tm_LeapYear(t2->tm_year);
  63.      for (i = 0; i < t2->tm_mon; i++) {
  64.         seconds += (long) tm_daytab[leap][i]*(60L*60L*24L);
  65.     }
  66.  
  67.     seconds += (long) t2->tm_mday*(60L*60L*24L);
  68.     seconds += (long) t2->tm_hour*(60L*60L);
  69.     seconds += (long) t2->tm_min*60;
  70.     seconds += (long) t2->tm_sec;
  71.              
  72.      leap = tm_LeapYear(t1->tm_year);
  73.      for (i = 0; i < t1->tm_mon; i++) {
  74.         seconds -= (long) tm_daytab[leap][i]*(60L*60L*24L);
  75.     }
  76.  
  77.     seconds -= (long) t1->tm_mday*(60L*60L*24L);
  78.     seconds -= (long) t1->tm_hour*(60L*60L);
  79.     seconds -= (long) t1->tm_min*60;
  80.     seconds -= (long) t1->tm_sec;
  81.  
  82.     return(sign*seconds);    
  83. }
  84.  
  85. long tm_ElapDays(struct tm *t1, struct tm *t2)
  86. /*
  87. DESCRIPTION:
  88.     The tm_ElapDays function returns the elapsed time in 
  89.     days between t1 and t2.
  90. RETURNS:
  91.     Returns the elapsed time in days.
  92. */
  93. {
  94.     struct tm *temp;
  95.     register int i;
  96.     int leap, sign, years, leapyears, mod;
  97.     long days;
  98.  
  99.     sign = 1;
  100.     if ((sign = tm_Cmp(t2,t1)) <= 0) {
  101.         temp = t2;
  102.         t2 = t1;
  103.         t1 = temp;
  104.     }
  105.  
  106.     days = 0;
  107.     if (t2->tm_year > t1->tm_year) {
  108.          years = t2->tm_year-t1->tm_year;
  109.         leapyears = years/4;
  110.         if ((mod = (years%4)-1) >= 0) {
  111.             for (i = 0; i <= mod; i++) {
  112.                 if (tm_LeapYear(t1->tm_year+i)) leapyears++;
  113.             }
  114.         }
  115.         years -= leapyears;
  116.         days = years*365L + leapyears*366L;
  117.     }
  118.      leap = tm_LeapYear(t2->tm_year);
  119.      for (i = 0; i < t2->tm_mon; i++) {
  120.         days += tm_daytab[leap][i];
  121.     }
  122.     days += t2->tm_mday;
  123.              
  124.      leap = tm_LeapYear(t1->tm_year);
  125.      for (i = 0; i < t1->tm_mon; i++) {
  126.         days -= tm_daytab[leap][i];
  127.     }
  128.     days -= t1->tm_mday;
  129.  
  130.     return(sign*days);    
  131. }
  132.  
  133. int tm_Cmp(struct tm *t1, struct tm *t2)
  134. /*
  135.     The tm_Cmp function compares t1 with t2.
  136. RETURNS:
  137.     Returns a value indicating the relationship between the two times:
  138.     Less than 0            t1 earlier than t2
  139.     0                    t1 equivalent to t2
  140.     Greater than 0        t1 later than t2
  141. */
  142. {
  143.     if (t1->tm_year < t2->tm_year) {
  144.         return(-1);
  145.     }
  146.     else if (t1->tm_year > t2->tm_year) {
  147.         return(1);
  148.     }
  149.     else if (t1->tm_mon < t2->tm_mon) {
  150.         return(-1);
  151.     }
  152.     else if (t1->tm_mon > t2->tm_mon) {
  153.         return(1);
  154.     }
  155.     else if (t1->tm_mday < t2->tm_mday) {
  156.         return(-1);
  157.     }
  158.     else if (t1->tm_mday > t2->tm_mday) {
  159.         return(1);
  160.     }
  161.     else if (t1->tm_hour < t2->tm_hour) {
  162.         return(-1);
  163.     }
  164.     else if (t1->tm_hour > t2->tm_hour) {
  165.         return(1);
  166.     }
  167.     else if (t1->tm_min < t2->tm_min) { 
  168.         return(-1);
  169.     }
  170.     else if (t1->tm_min > t2->tm_min) { 
  171.         return(1);
  172.     }
  173.     else if (t1->tm_sec < t2->tm_sec) { 
  174.         return(-1);
  175.     }
  176.     else if (t1->tm_sec > t2->tm_sec) { 
  177.         return(1);
  178.     }
  179.  
  180.     return(0);
  181. }
  182.