1 // Windows/TimeUtils.cpp
2
3 #include "StdAfx.h"
4
5 #include "Defs.h"
6 #include "TimeUtils.h"
7
8 namespace NWindows {
9 namespace NTime {
10
11 static const UInt32 kNumTimeQuantumsInSecond = 10000000;
12 static const UInt32 kFileTimeStartYear = 1601;
13 static const UInt32 kDosTimeStartYear = 1980;
14 static const UInt32 kUnixTimeStartYear = 1970;
15 static const UInt64 kUnixTimeOffset =
16 (UInt64)60 * 60 * 24 * (89 + 365 * (kUnixTimeStartYear - kFileTimeStartYear));
17 static const UInt64 kNumSecondsInFileTime = (UInt64)(Int64)-1 / kNumTimeQuantumsInSecond;
18
DosTimeToFileTime(UInt32 dosTime,FILETIME & ft)19 bool DosTimeToFileTime(UInt32 dosTime, FILETIME &ft) throw()
20 {
21 #if defined(_WIN32) && !defined(UNDER_CE)
22 return BOOLToBool(::DosDateTimeToFileTime((UInt16)(dosTime >> 16), (UInt16)(dosTime & 0xFFFF), &ft));
23 #else
24 ft.dwLowDateTime = 0;
25 ft.dwHighDateTime = 0;
26 UInt64 res;
27 if (!GetSecondsSince1601(kDosTimeStartYear + (dosTime >> 25), (dosTime >> 21) & 0xF, (dosTime >> 16) & 0x1F,
28 (dosTime >> 11) & 0x1F, (dosTime >> 5) & 0x3F, (dosTime & 0x1F) * 2, res))
29 return false;
30 res *= kNumTimeQuantumsInSecond;
31 ft.dwLowDateTime = (UInt32)res;
32 ft.dwHighDateTime = (UInt32)(res >> 32);
33 return true;
34 #endif
35 }
36
37 static const UInt32 kHighDosTime = 0xFF9FBF7D;
38 static const UInt32 kLowDosTime = 0x210000;
39
40 #define PERIOD_4 (4 * 365 + 1)
41 #define PERIOD_100 (PERIOD_4 * 25 - 1)
42 #define PERIOD_400 (PERIOD_100 * 4 + 1)
43
FileTimeToDosTime(const FILETIME & ft,UInt32 & dosTime)44 bool FileTimeToDosTime(const FILETIME &ft, UInt32 &dosTime) throw()
45 {
46 #if defined(_WIN32) && !defined(UNDER_CE)
47
48 WORD datePart, timePart;
49 if (!::FileTimeToDosDateTime(&ft, &datePart, &timePart))
50 {
51 dosTime = (ft.dwHighDateTime >= 0x01C00000) ? kHighDosTime : kLowDosTime;
52 return false;
53 }
54 dosTime = (((UInt32)datePart) << 16) + timePart;
55
56 #else
57
58 unsigned year, mon, day, hour, min, sec;
59 UInt64 v64 = ft.dwLowDateTime | ((UInt64)ft.dwHighDateTime << 32);
60 Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
61 unsigned temp;
62 UInt32 v;
63 v64 += (kNumTimeQuantumsInSecond * 2 - 1);
64 v64 /= kNumTimeQuantumsInSecond;
65 sec = (unsigned)(v64 % 60);
66 v64 /= 60;
67 min = (unsigned)(v64 % 60);
68 v64 /= 60;
69 hour = (unsigned)(v64 % 24);
70 v64 /= 24;
71
72 v = (UInt32)v64;
73
74 year = (unsigned)(kFileTimeStartYear + v / PERIOD_400 * 400);
75 v %= PERIOD_400;
76
77 temp = (unsigned)(v / PERIOD_100);
78 if (temp == 4)
79 temp = 3;
80 year += temp * 100;
81 v -= temp * PERIOD_100;
82
83 temp = v / PERIOD_4;
84 if (temp == 25)
85 temp = 24;
86 year += temp * 4;
87 v -= temp * PERIOD_4;
88
89 temp = v / 365;
90 if (temp == 4)
91 temp = 3;
92 year += temp;
93 v -= temp * 365;
94
95 if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
96 ms[1] = 29;
97 for (mon = 1; mon <= 12; mon++)
98 {
99 unsigned s = ms[mon - 1];
100 if (v < s)
101 break;
102 v -= s;
103 }
104 day = (unsigned)v + 1;
105
106 dosTime = kLowDosTime;
107 if (year < kDosTimeStartYear)
108 return false;
109 year -= kDosTimeStartYear;
110 dosTime = kHighDosTime;
111 if (year >= 128)
112 return false;
113 dosTime = (year << 25) | (mon << 21) | (day << 16) | (hour << 11) | (min << 5) | (sec >> 1);
114 #endif
115 return true;
116 }
117
UnixTimeToFileTime(UInt32 unixTime,FILETIME & ft)118 void UnixTimeToFileTime(UInt32 unixTime, FILETIME &ft) throw()
119 {
120 UInt64 v = (kUnixTimeOffset + (UInt64)unixTime) * kNumTimeQuantumsInSecond;
121 ft.dwLowDateTime = (DWORD)v;
122 ft.dwHighDateTime = (DWORD)(v >> 32);
123 }
124
UnixTime64ToFileTime(Int64 unixTime,FILETIME & ft)125 bool UnixTime64ToFileTime(Int64 unixTime, FILETIME &ft) throw()
126 {
127 if (unixTime > kNumSecondsInFileTime - kUnixTimeOffset)
128 {
129 ft.dwLowDateTime = ft.dwHighDateTime = (UInt32)(Int32)-1;
130 return false;
131 }
132 Int64 v = (Int64)kUnixTimeOffset + unixTime;
133 if (v < 0)
134 {
135 ft.dwLowDateTime = ft.dwHighDateTime = 0;
136 return false;
137 }
138 UInt64 v2 = (UInt64)v * kNumTimeQuantumsInSecond;
139 ft.dwLowDateTime = (DWORD)v2;
140 ft.dwHighDateTime = (DWORD)(v2 >> 32);
141 return true;
142 }
143
FileTimeToUnixTime64(const FILETIME & ft)144 Int64 FileTimeToUnixTime64(const FILETIME &ft) throw()
145 {
146 UInt64 winTime = (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
147 return (Int64)(winTime / kNumTimeQuantumsInSecond) - kUnixTimeOffset;
148 }
149
FileTimeToUnixTime(const FILETIME & ft,UInt32 & unixTime)150 bool FileTimeToUnixTime(const FILETIME &ft, UInt32 &unixTime) throw()
151 {
152 UInt64 winTime = (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
153 winTime /= kNumTimeQuantumsInSecond;
154 if (winTime < kUnixTimeOffset)
155 {
156 unixTime = 0;
157 return false;
158 }
159 winTime -= kUnixTimeOffset;
160 if (winTime > 0xFFFFFFFF)
161 {
162 unixTime = 0xFFFFFFFF;
163 return false;
164 }
165 unixTime = (UInt32)winTime;
166 return true;
167 }
168
GetSecondsSince1601(unsigned year,unsigned month,unsigned day,unsigned hour,unsigned min,unsigned sec,UInt64 & resSeconds)169 bool GetSecondsSince1601(unsigned year, unsigned month, unsigned day,
170 unsigned hour, unsigned min, unsigned sec, UInt64 &resSeconds) throw()
171 {
172 resSeconds = 0;
173 if (year < kFileTimeStartYear || year >= 10000 || month < 1 || month > 12 ||
174 day < 1 || day > 31 || hour > 23 || min > 59 || sec > 59)
175 return false;
176 UInt32 numYears = year - kFileTimeStartYear;
177 UInt32 numDays = numYears * 365 + numYears / 4 - numYears / 100 + numYears / 400;
178 Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
179 if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
180 ms[1] = 29;
181 month--;
182 for (unsigned i = 0; i < month; i++)
183 numDays += ms[i];
184 numDays += day - 1;
185 resSeconds = ((UInt64)(numDays * 24 + hour) * 60 + min) * 60 + sec;
186 return true;
187 }
188
GetCurUtcFileTime(FILETIME & ft)189 void GetCurUtcFileTime(FILETIME &ft) throw()
190 {
191 // Both variants provide same low resolution on WinXP: about 15 ms.
192 // But GetSystemTimeAsFileTime is much faster.
193
194 #ifdef UNDER_CE
195 SYSTEMTIME st;
196 GetSystemTime(&st);
197 SystemTimeToFileTime(&st, &ft);
198 #else
199 GetSystemTimeAsFileTime(&ft);
200 #endif
201 }
202
203 }}
204