Debian Codegraph @

contrib/time64.c

raw
1 /* Copyright (c) Robert Walker, [email protected]
2 * Free source. Do what you wish with it - treat it as you would
3 * example code in a book on c programming.
4 */
5 #include "time64.h"
6 #undef time
7 #undef localtime
8 #undef mktime
9 #undef difftime
10 #undef gmtime
11 #undef time_t
12
13 #include <time.h>
14 #include <windows.h>
15 #include <assert.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #define SECS_TO_FT_MULT 10000000
20
21 #ifdef _DEBUG
22 # define DEBUG_TIME_T
23 #endif
24
25 /* From MSVC help:
26 * The gmtime, mktime, and localtime functions use the same single,
27 * statically allocated structure to hold their results. Each call to
28 * one of these functions destroys the result of any previous call.
29 * If timer represents a date before midnight, January 1, 1970,
30 * gmtime returns NULL. There is no error return.
31 *
32 * So here is the struct to use for our 64 bit implementation
33 *
34 * However, it may be useful to be able to make this thread safe
35 */
36
37 #ifdef USE_THREAD_LOCAL_VARIABLES
38 # define TIME64_THREAD_LOCAL _declspec(thread)
39 #else
40 # define TIME64_THREAD_LOCAL
41 #endif
42
43 TIME64_THREAD_LOCAL static struct tm today_ret;
44
45 static void T64ToFileTime(t64 *pt,FILETIME *pft)
46 {
47 LARGE_INTEGER li;
48 li.QuadPart=*pt*SECS_TO_FT_MULT;
49 pft->dwLowDateTime=li.LowPart;
50 pft->dwHighDateTime=li.HighPart;
51 }
52
53 static void FileTimeToT64(FILETIME *pft,t64 *pt)
54 {
55 LARGE_INTEGER li;
56 li.LowPart = pft->dwLowDateTime;
57 li.HighPart = pft->dwHighDateTime;
58 *pt=li.QuadPart;
59 *pt/=SECS_TO_FT_MULT;
60 }
61
62 #define FindTimeTBase() (t64) 11644473600
63 // calculated using
64 /**
65 static t64 FindTimeTBase(void)
66 {
67 // Find 1st Jan 1970 as a FILETIME
68 SYSTEMTIME st;
69 FILETIME ft;
70 memset(&st,0,sizeof(st));
71 st.wYear=1970;
72 st.wMonth=1;
73 st.wDay=1;
74 SystemTimeToFileTime(&st, &ft);
75 FileTimeToT64(&ft,&tbase);
76 return tbase;
77 }
78 **/
79
80 static void SystemTimeToT64(SYSTEMTIME *pst,t64 *pt)
81 {
82 FILETIME ft;
83 SystemTimeToFileTime( pst, &ft );
84 FileTimeToT64( &ft, pt );
85 *pt-=FindTimeTBase();
86 }
87
88 static void T64ToSystemTime(t64 *pt,SYSTEMTIME *pst)
89 {
90 FILETIME ft;
91 t64 t=*pt;
92 t+=FindTimeTBase();
93 T64ToFileTime(&t,&ft);
94 FileTimeToSystemTime(&ft,pst);
95 }
96
97
98 t64 time_64(t64 *pt)
99 {
100 t64 t;
101 SYSTEMTIME st;
102 GetSystemTime(&st);
103 SystemTimeToT64(&st,&t);
104
105 #ifdef DEBUG_TIME_T
106 {
107 time_t t2=time(NULL);
108 if ( t2 >= 0 )
109 assert(abs(t2-(int)t)<=1);
110 // the <=1 here is in case the seconds get incremented
111 // betweeen the GetSystemTime(..) call and the time(..) call
112 }
113 #endif
114 if ( pt )
115 *pt=t;
116 return t;
117 }
118
119 double difftime_64(t64 time1, t64 time0)
120 {
121 return (double)(time1-time0);
122 }
123
124 // Converts from a broken down time into calendar time represented as a time_t
125 // Assumes the incoming time is local and adjusts result to be UTC
126 t64 mktime_64( struct tm *today )
127 {
128 t64 t;
129 SYSTEMTIME st;
130 TIME_ZONE_INFORMATION TimeZoneInformation;
131
132 st.wDay=(WORD)today->tm_mday;
133 st.wDayOfWeek=(WORD)today->tm_wday;
134 st.wHour=(WORD)today->tm_hour;
135 st.wMinute=(WORD)today->tm_min;
136 st.wMonth=(WORD)(today->tm_mon+1);
137 st.wSecond=(WORD)today->tm_sec;
138 st.wYear=(WORD)(today->tm_year+1900);
139 st.wMilliseconds=0;
140 SystemTimeToT64(&st,&t);
141 // adjust by timezone
142 GetTimeZoneInformation( &TimeZoneInformation );
143 t += (60*TimeZoneInformation.Bias);
144 return t;
145 }
146
147 #define DAY_IN_SECS (60*60*24)
148
149 struct tm *gmtime_64(t64 *t)
150 {
151 SYSTEMTIME st, styear;
152 t64 t64Year;
153
154 T64ToSystemTime(t,&st);
155 today_ret.tm_wday=st.wDayOfWeek;
156 today_ret.tm_min=st.wMinute;
157 today_ret.tm_sec=st.wSecond;
158 today_ret.tm_mon=st.wMonth-1;
159 today_ret.tm_mday=st.wDay;
160 today_ret.tm_hour=st.wHour;
161 today_ret.tm_year=st.wYear-1900;
162 memset(&styear,0,sizeof(styear));
163 styear.wYear=st.wYear;
164 styear.wMonth=1;
165 styear.wDay=1;
166 SystemTimeToT64(&styear,&t64Year);
167 today_ret.tm_yday=(int)((*t-t64Year)/DAY_IN_SECS);
168 today_ret.tm_isdst=0;
169
170 #ifdef DEBUG_TIME_T
171 {
172 struct tm today2;
173 time_t t32=(time_t)*t;
174 if ( t32 >= 0 )
175 {
176 today2=*gmtime(&t32);
177 fprintf(stderr,"yday %d %d wday %d %d min %d %d sec %d %d mon %d %d mday %d %d hour %d %d year %d %d\n",
178 today_ret.tm_yday,today2.tm_yday,
179 today_ret.tm_wday,today2.tm_wday,
180 today_ret.tm_min,today2.tm_min,
181 today_ret.tm_sec,today2.tm_sec,
182 today_ret.tm_mon,today2.tm_mon,
183 today_ret.tm_mday,today2.tm_mday,
184 today_ret.tm_hour,today2.tm_hour,
185 today_ret.tm_year,today2.tm_year);
186
187 assert(today_ret.tm_yday==today2.tm_yday);
188 assert(today_ret.tm_wday==today2.tm_wday);
189 assert(today_ret.tm_min==today2.tm_min);
190 assert(today_ret.tm_sec==today2.tm_sec);
191 assert(today_ret.tm_mon==today2.tm_mon);
192 assert(today_ret.tm_mday==today2.tm_mday);
193 assert(today_ret.tm_hour==today2.tm_hour);
194 assert(today_ret.tm_year==today2.tm_year);
195 }
196 }
197 #endif
198 return &today_ret;
199 }
200
201 struct tm *localtime_64(t64 *pt)
202 {
203 t64 t=*pt;
204 FILETIME ft,ftlocal;
205 TIME_ZONE_INFORMATION TimeZoneInformation;
206 T64ToFileTime(&t,&ft);
207 FileTimeToLocalFileTime(&ft,&ftlocal);
208 FileTimeToT64(&ftlocal,&t);
209 today_ret=*gmtime_64(&t);
210 if ( GetTimeZoneInformation( &TimeZoneInformation ) == TIME_ZONE_ID_DAYLIGHT )
211 today_ret.tm_isdst = 1;
212 else
213 today_ret.tm_isdst = 0;
214 return &today_ret;
215 }
216