home *** CD-ROM | disk | FTP | other *** search
- /*
- tmelap.c 2/7/87
-
- % tm_ElapSecs, tm_ElapDays, tm_Cmp
-
- Written by John Cooke.
-
- OWL 1.2
- Copyright (c) 1986-1989, by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 8/07/89 jmd Split from cstime.c
- 3/10/90 pmcm Changed tm_Cmp to use tm_mon & tm_mday rather than tm_yday
- 3/28/90 jmd ansi-fied
- 8/01/90 ted Moved to OWL; include oaktime.h not cstime.h.
- */
-
- #include "oakhead.h"
-
- #include <time.h>
- #include "oaktime.h"
-
- long tm_ElapSecs(struct tm *t1, struct tm *t2)
- /*
- DESCRIPTION:
- The tm_ElapSecs function returns the elapsed time in
- seconds between t1 and t2.
- RETURNS:
- Returns the elapsed time in seconds.
- */
- {
- struct tm *temp;
- register int i;
- int leap, sign, years, leapyears, mod;
- long seconds;
-
- sign = 1;
- if ((sign = tm_Cmp(t2,t1)) <= 0) {
- temp = t2;
- t2 = t1;
- t1 = temp;
- }
- seconds = 0;
-
- if (t2->tm_year - t1->tm_year > 68) {
- return(TM_OVERFLOW);
- }
-
- if (t2->tm_year > t1->tm_year) {
- years = t2->tm_year-t1->tm_year;
- leapyears = years/4;
- if ((mod = (years%4)-1) >= 0) {
- for (i = 0; i <= mod; i++) {
- if (tm_LeapYear(t1->tm_year+i)) leapyears++;
- }
- }
- years -= leapyears;
- seconds = (long)(years*(60L*60L*24L*365L)+leapyears*(60L*60L*24L*366L));
- }
- leap = tm_LeapYear(t2->tm_year);
- for (i = 0; i < t2->tm_mon; i++) {
- seconds += (long) tm_daytab[leap][i]*(60L*60L*24L);
- }
-
- seconds += (long) t2->tm_mday*(60L*60L*24L);
- seconds += (long) t2->tm_hour*(60L*60L);
- seconds += (long) t2->tm_min*60;
- seconds += (long) t2->tm_sec;
-
- leap = tm_LeapYear(t1->tm_year);
- for (i = 0; i < t1->tm_mon; i++) {
- seconds -= (long) tm_daytab[leap][i]*(60L*60L*24L);
- }
-
- seconds -= (long) t1->tm_mday*(60L*60L*24L);
- seconds -= (long) t1->tm_hour*(60L*60L);
- seconds -= (long) t1->tm_min*60;
- seconds -= (long) t1->tm_sec;
-
- return(sign*seconds);
- }
-
- long tm_ElapDays(struct tm *t1, struct tm *t2)
- /*
- DESCRIPTION:
- The tm_ElapDays function returns the elapsed time in
- days between t1 and t2.
- RETURNS:
- Returns the elapsed time in days.
- */
- {
- struct tm *temp;
- register int i;
- int leap, sign, years, leapyears, mod;
- long days;
-
- sign = 1;
- if ((sign = tm_Cmp(t2,t1)) <= 0) {
- temp = t2;
- t2 = t1;
- t1 = temp;
- }
-
- days = 0;
- if (t2->tm_year > t1->tm_year) {
- years = t2->tm_year-t1->tm_year;
- leapyears = years/4;
- if ((mod = (years%4)-1) >= 0) {
- for (i = 0; i <= mod; i++) {
- if (tm_LeapYear(t1->tm_year+i)) leapyears++;
- }
- }
- years -= leapyears;
- days = years*365L + leapyears*366L;
- }
- leap = tm_LeapYear(t2->tm_year);
- for (i = 0; i < t2->tm_mon; i++) {
- days += tm_daytab[leap][i];
- }
- days += t2->tm_mday;
-
- leap = tm_LeapYear(t1->tm_year);
- for (i = 0; i < t1->tm_mon; i++) {
- days -= tm_daytab[leap][i];
- }
- days -= t1->tm_mday;
-
- return(sign*days);
- }
-
- int tm_Cmp(struct tm *t1, struct tm *t2)
- /*
- The tm_Cmp function compares t1 with t2.
- RETURNS:
- Returns a value indicating the relationship between the two times:
- Less than 0 t1 earlier than t2
- 0 t1 equivalent to t2
- Greater than 0 t1 later than t2
- */
- {
- if (t1->tm_year < t2->tm_year) {
- return(-1);
- }
- else if (t1->tm_year > t2->tm_year) {
- return(1);
- }
- else if (t1->tm_mon < t2->tm_mon) {
- return(-1);
- }
- else if (t1->tm_mon > t2->tm_mon) {
- return(1);
- }
- else if (t1->tm_mday < t2->tm_mday) {
- return(-1);
- }
- else if (t1->tm_mday > t2->tm_mday) {
- return(1);
- }
- else if (t1->tm_hour < t2->tm_hour) {
- return(-1);
- }
- else if (t1->tm_hour > t2->tm_hour) {
- return(1);
- }
- else if (t1->tm_min < t2->tm_min) {
- return(-1);
- }
- else if (t1->tm_min > t2->tm_min) {
- return(1);
- }
- else if (t1->tm_sec < t2->tm_sec) {
- return(-1);
- }
- else if (t1->tm_sec > t2->tm_sec) {
- return(1);
- }
-
- return(0);
- }
-