• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
UnixTimeToFileTime64(UInt32 unixTime)118 UInt64 UnixTimeToFileTime64(UInt32 unixTime) throw()
119 {
120   return (kUnixTimeOffset + (UInt64)unixTime) * kNumTimeQuantumsInSecond;
121 }
122 
UnixTimeToFileTime(UInt32 unixTime,FILETIME & ft)123 void UnixTimeToFileTime(UInt32 unixTime, FILETIME &ft) throw()
124 {
125   UInt64 v = UnixTimeToFileTime64(unixTime);
126   ft.dwLowDateTime = (DWORD)v;
127   ft.dwHighDateTime = (DWORD)(v >> 32);
128 }
129 
UnixTime64ToFileTime64(Int64 unixTime)130 UInt64 UnixTime64ToFileTime64(Int64 unixTime) throw()
131 {
132   return (UInt64)(kUnixTimeOffset + unixTime) * kNumTimeQuantumsInSecond;
133 }
134 
UnixTime64ToFileTime(Int64 unixTime,FILETIME & ft)135 bool UnixTime64ToFileTime(Int64 unixTime, FILETIME &ft) throw()
136 {
137   if (unixTime > (Int64)(kNumSecondsInFileTime - kUnixTimeOffset))
138   {
139     ft.dwLowDateTime = ft.dwHighDateTime = (UInt32)(Int32)-1;
140     return false;
141   }
142   Int64 v = (Int64)kUnixTimeOffset + unixTime;
143   if (v < 0)
144   {
145     ft.dwLowDateTime = ft.dwHighDateTime = 0;
146     return false;
147   }
148   UInt64 v2 = (UInt64)v * kNumTimeQuantumsInSecond;
149   ft.dwLowDateTime = (DWORD)v2;
150   ft.dwHighDateTime = (DWORD)(v2 >> 32);
151   return true;
152 }
153 
FileTimeToUnixTime64(const FILETIME & ft)154 Int64 FileTimeToUnixTime64(const FILETIME &ft) throw()
155 {
156   UInt64 winTime = (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
157   return (Int64)(winTime / kNumTimeQuantumsInSecond) - (Int64)kUnixTimeOffset;
158 }
159 
FileTimeToUnixTime(const FILETIME & ft,UInt32 & unixTime)160 bool FileTimeToUnixTime(const FILETIME &ft, UInt32 &unixTime) throw()
161 {
162   UInt64 winTime = (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
163   winTime /= kNumTimeQuantumsInSecond;
164   if (winTime < kUnixTimeOffset)
165   {
166     unixTime = 0;
167     return false;
168   }
169   winTime -= kUnixTimeOffset;
170   if (winTime > 0xFFFFFFFF)
171   {
172     unixTime = 0xFFFFFFFF;
173     return false;
174   }
175   unixTime = (UInt32)winTime;
176   return true;
177 }
178 
GetSecondsSince1601(unsigned year,unsigned month,unsigned day,unsigned hour,unsigned min,unsigned sec,UInt64 & resSeconds)179 bool GetSecondsSince1601(unsigned year, unsigned month, unsigned day,
180   unsigned hour, unsigned min, unsigned sec, UInt64 &resSeconds) throw()
181 {
182   resSeconds = 0;
183   if (year < kFileTimeStartYear || year >= 10000 || month < 1 || month > 12 ||
184       day < 1 || day > 31 || hour > 23 || min > 59 || sec > 59)
185     return false;
186   UInt32 numYears = year - kFileTimeStartYear;
187   UInt32 numDays = numYears * 365 + numYears / 4 - numYears / 100 + numYears / 400;
188   Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
189   if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
190     ms[1] = 29;
191   month--;
192   for (unsigned i = 0; i < month; i++)
193     numDays += ms[i];
194   numDays += day - 1;
195   resSeconds = ((UInt64)(numDays * 24 + hour) * 60 + min) * 60 + sec;
196   return true;
197 }
198 
GetCurUtcFileTime(FILETIME & ft)199 void GetCurUtcFileTime(FILETIME &ft) throw()
200 {
201   // Both variants provide same low resolution on WinXP: about 15 ms.
202   // But GetSystemTimeAsFileTime is much faster.
203 
204   #ifdef UNDER_CE
205   SYSTEMTIME st;
206   GetSystemTime(&st);
207   SystemTimeToFileTime(&st, &ft);
208   #else
209   GetSystemTimeAsFileTime(&ft);
210   #endif
211 }
212 
213 }}
214