1 /*
2 * Stolen from Jef Poskanzer's tws time library, which was stolen from
3 * Marshall Rose's MH Message Handling system...
4 *
5 * tmclock() will convert time from a tm struct back to a clock value.
6 * tmjuliandate() converts a tm struct to its julian day number.
7 * tmsubdayclock() takes hours, minutes, and seconds from a tm struct
8 * and returns the number of seconds since midnight of that day. (?)
9 * -- Howard Chu, August 1 1988 hyc@umix.cc.umich.edu, umix!hyc
10 */
11
12 /* $Header: /cvsroot/arc/arc/tmclock.c,v 1.3 2003/10/31 02:31:24 highlandsun Exp $ */
13
14 /* Julian day number of the Unix* clock's origin, 01 Jan 1970. */
15 #define JD1970 2440587L
16 #define CENTURY 19
17 #if BSD
18 #include <sys/time.h>
19 #else
20 #include <time.h>
21 extern long timezone; /* should be in <time.h>, but isn't on Sun */
22 #endif
23
24 long tzone;
25
26 long
tmjuliandate(tm)27 tmjuliandate( tm )
28 struct tm *tm;
29 {
30 register int mday, mon, year;
31 register long a, b;
32 double jd;
33
34 if ( (mday = tm -> tm_mday) < 1 || mday > 31 ||
35 (mon = tm -> tm_mon + 1) < 1 || mon > 12 ||
36 (year = tm -> tm_year) < 1 || year > 10000 )
37 return ( -1L );
38 if ( year < 1583 )
39 year += CENTURY * 100;
40
41 if ( mon == 1 || mon == 2 )
42 {
43 --year;
44 mon += 12;
45 }
46 if ( year < 1583 )
47 return ( -1L );
48 a = year / 100;
49 b = 2 - a + a / 4;
50 b += (long) ( (double) year * 365.25 );
51 b += (long) ( 30.6001 * ( (double) mon + 1.0 ) );
52 jd = mday + b + 1720994.5;
53 return ( (long) jd );
54 }
55
56
57 long
tmsubdayclock(tm)58 tmsubdayclock( tm )
59 struct tm *tm;
60 {
61 register int sec, min, hour;
62 register long result;
63 #if BSD
64 {
65 struct timezone tzp;
66
67 gettimeofday(0L, &tzp);
68 tzone = tzp.tz_minuteswest*(-60);
69 }
70 #else
71 tzone=timezone;
72 #endif
73 if ( (sec = tm -> tm_sec) < 0 || sec > 59 ||
74 (min = tm -> tm_min) < 0 || min > 59 ||
75 (hour = tm -> tm_hour) < 0 || hour > 23 )
76 return ( -1L );
77
78 result = ( hour * 60 + min ) * 60 + sec;
79 result -= tzone;
80
81 return ( result );
82 }
83
84
85 long
tmclock(tm)86 tmclock( tm )
87 struct tm *tm;
88 {
89 register long jd, sdc;
90 long result;
91
92 if ( ( jd = tmjuliandate( tm ) ) == -1L )
93 return ( -1L );
94 if ( ( sdc = tmsubdayclock( tm ) ) == -1L )
95 return ( -1L );
96
97 result = ( jd - JD1970 ) * 24 * 60 * 60 + sdc;
98
99 if ( localtime( &result )->tm_isdst )
100 result -= 60L * 60L;
101
102 return ( result );
103 }
104