1 // Windows/Time.h |
|
2 |
|
3 #ifndef __WINDOWS_TIME_H |
|
4 #define __WINDOWS_TIME_H |
|
5 |
|
6 #include "Common/Types.h" |
|
7 #include "Windows/Defs.h" |
|
8 |
|
9 namespace NWindows { |
|
10 namespace NTime { |
|
11 |
|
12 inline bool DosTimeToFileTime(UInt32 dosTime, FILETIME &fileTime) |
|
13 { |
|
14 return BOOLToBool(::DosDateTimeToFileTime(UInt16(dosTime >> 16), |
|
15 UInt16(dosTime & 0xFFFF), &fileTime)); |
|
16 } |
|
17 |
|
18 const UInt32 kHighDosTime = 0xFF9FBF7D; |
|
19 const UInt32 kLowDosTime = 0x210000; |
|
20 |
|
21 inline bool FileTimeToDosTime(const FILETIME &fileTime, UInt32 &dosTime) |
|
22 { |
|
23 WORD datePart, timePart; |
|
24 if (!::FileTimeToDosDateTime(&fileTime, &datePart, &timePart)) |
|
25 { |
|
26 if (fileTime.dwHighDateTime >= 0x01C00000) // 2000 |
|
27 dosTime = kHighDosTime; |
|
28 else |
|
29 dosTime = kLowDosTime; |
|
30 return false; |
|
31 } |
|
32 dosTime = (((UInt32)datePart) << 16) + timePart; |
|
33 return true; |
|
34 } |
|
35 |
|
36 const UInt32 kNumTimeQuantumsInSecond = 10000000; |
|
37 const UInt64 kUnixTimeStartValue = ((UInt64)kNumTimeQuantumsInSecond) * 60 * 60 * 24 * 134774; |
|
38 |
|
39 inline void UnixTimeToFileTime(UInt32 unixTime, FILETIME &fileTime) |
|
40 { |
|
41 UInt64 v = kUnixTimeStartValue + ((UInt64)unixTime) * kNumTimeQuantumsInSecond; |
|
42 fileTime.dwLowDateTime = (DWORD)v; |
|
43 fileTime.dwHighDateTime = (DWORD)(v >> 32); |
|
44 } |
|
45 |
|
46 inline bool FileTimeToUnixTime(const FILETIME &fileTime, UInt32 &unixTime) |
|
47 { |
|
48 UInt64 winTime = (((UInt64)fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime; |
|
49 if (winTime < kUnixTimeStartValue) |
|
50 { |
|
51 unixTime = 0; |
|
52 return false; |
|
53 } |
|
54 winTime = (winTime - kUnixTimeStartValue) / kNumTimeQuantumsInSecond; |
|
55 if (winTime > 0xFFFFFFFF) |
|
56 { |
|
57 unixTime = 0xFFFFFFFF; |
|
58 return false; |
|
59 } |
|
60 unixTime = (UInt32)winTime; |
|
61 return true; |
|
62 } |
|
63 |
|
64 }} |
|
65 |
|
66 #endif |
|