1 // Windows/TimeUtils.h
2
3 #ifndef __WINDOWS_TIME_UTILS_H
4 #define __WINDOWS_TIME_UTILS_H
5
6 #include "../Common/MyTypes.h"
7 #include "../Common/MyWindows.h"
8 #include "PropVariant.h"
9
FILETIME_To_UInt64(const FILETIME & ft)10 inline UInt64 FILETIME_To_UInt64(const FILETIME &ft)
11 {
12 return (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
13 }
14
FILETIME_Clear(FILETIME & ft)15 inline void FILETIME_Clear(FILETIME &ft)
16 {
17 ft.dwLowDateTime = 0;
18 ft.dwHighDateTime = 0;
19 }
20
FILETIME_IsZero(const FILETIME & ft)21 inline bool FILETIME_IsZero(const FILETIME &ft)
22 {
23 return (ft.dwHighDateTime == 0 && ft.dwLowDateTime == 0);
24 }
25
26
27 #ifdef _WIN32
28 #define CFiTime FILETIME
29 #define Compare_FiTime ::CompareFileTime
FiTime_To_FILETIME(const CFiTime & ts,FILETIME & ft)30 inline void FiTime_To_FILETIME(const CFiTime &ts, FILETIME &ft)
31 {
32 ft = ts;
33 }
34 /*
35 inline void FILETIME_To_FiTime(const FILETIME &ft, CFiTime &ts)
36 {
37 ts = ft;
38 }
39 */
FiTime_Clear(CFiTime & ft)40 inline void FiTime_Clear(CFiTime &ft)
41 {
42 ft.dwLowDateTime = 0;
43 ft.dwHighDateTime = 0;
44 }
45 #else
46
47 #include <sys/stat.h>
48
49 #if defined(_AIX)
50 #define CFiTime st_timespec
51 #else
52 #define CFiTime timespec
53 #endif
54 int Compare_FiTime(const CFiTime *a1, const CFiTime *a2);
55 bool FILETIME_To_timespec(const FILETIME &ft, CFiTime &ts);
56 void FiTime_To_FILETIME(const CFiTime &ts, FILETIME &ft);
57 void FiTime_To_FILETIME_ns100(const CFiTime &ts, FILETIME &ft, unsigned &ns100);
FiTime_Clear(CFiTime & ft)58 inline void FiTime_Clear(CFiTime &ft)
59 {
60 ft.tv_sec = 0;
61 ft.tv_nsec = 0;
62 }
63
64 #ifdef __APPLE__
65 #define ST_MTIME(st) st.st_mtimespec
66 #define ST_ATIME(st) st.st_atimespec
67 #define ST_CTIME(st) st.st_ctimespec
68 #else
69 #define ST_MTIME(st) st.st_mtim
70 #define ST_ATIME(st) st.st_atim
71 #define ST_CTIME(st) st.st_ctim
72 #endif
73
74 #endif
75
76 // void FiTime_Normalize_With_Prec(CFiTime &ft, unsigned prec);
77
78 namespace NWindows {
79 namespace NTime {
80
81 bool DosTime_To_FileTime(UInt32 dosTime, FILETIME &fileTime) throw();
82 bool UtcFileTime_To_LocalDosTime(const FILETIME &utc, UInt32 &dosTime) throw();
83 bool FileTime_To_DosTime(const FILETIME &fileTime, UInt32 &dosTime) throw();
84
85 // UInt32 Unix Time : for dates 1970-2106
86 UInt64 UnixTime_To_FileTime64(UInt32 unixTime) throw();
87 void UnixTime_To_FileTime(UInt32 unixTime, FILETIME &fileTime) throw();
88
89 // Int64 Unix Time : negative values for dates before 1970
90 UInt64 UnixTime64_To_FileTime64(Int64 unixTime) throw(); // no check
91 bool UnixTime64_To_FileTime64(Int64 unixTime, UInt64 &fileTime) throw();
92 bool UnixTime64_To_FileTime(Int64 unixTime, FILETIME &fileTime) throw();
93
94 Int64 FileTime64_To_UnixTime64(UInt64 ft64) throw();
95 bool FileTime_To_UnixTime(const FILETIME &fileTime, UInt32 &unixTime) throw();
96 Int64 FileTime_To_UnixTime64(const FILETIME &ft) throw();
97 Int64 FileTime_To_UnixTime64_and_Quantums(const FILETIME &ft, UInt32 &quantums) throw();
98
99 bool GetSecondsSince1601(unsigned year, unsigned month, unsigned day,
100 unsigned hour, unsigned min, unsigned sec, UInt64 &resSeconds) throw();
101
102 void GetCurUtc_FiTime(CFiTime &ft) throw();
103 #ifdef _WIN32
104 #define GetCurUtcFileTime GetCurUtc_FiTime
105 #else
106 void GetCurUtcFileTime(FILETIME &ft) throw();
107 #endif
108
109 }}
110
PropVariant_SetFrom_UnixTime(NWindows::NCOM::CPropVariant & prop,UInt32 unixTime)111 inline void PropVariant_SetFrom_UnixTime(NWindows::NCOM::CPropVariant &prop, UInt32 unixTime)
112 {
113 FILETIME ft;
114 NWindows::NTime::UnixTime_To_FileTime(unixTime, ft);
115 prop.SetAsTimeFrom_FT_Prec(ft, k_PropVar_TimePrec_Unix);
116 }
117
PropVariant_SetFrom_NtfsTime(NWindows::NCOM::CPropVariant & prop,const FILETIME & ft)118 inline void PropVariant_SetFrom_NtfsTime(NWindows::NCOM::CPropVariant &prop, const FILETIME &ft)
119 {
120 prop.SetAsTimeFrom_FT_Prec(ft, k_PropVar_TimePrec_100ns);
121 }
122
PropVariant_SetFrom_FiTime(NWindows::NCOM::CPropVariant & prop,const CFiTime & fts)123 inline void PropVariant_SetFrom_FiTime(NWindows::NCOM::CPropVariant &prop, const CFiTime &fts)
124 {
125 #ifdef _WIN32
126 PropVariant_SetFrom_NtfsTime(prop, fts);
127 #else
128 unsigned ns100;
129 FILETIME ft;
130 FiTime_To_FILETIME_ns100(fts, ft, ns100);
131 prop.SetAsTimeFrom_FT_Prec_Ns100(ft, k_PropVar_TimePrec_1ns, ns100);
132 #endif
133 }
134
PropVariant_SetFrom_DosTime(NWindows::NCOM::CPropVariant & prop,UInt32 dosTime)135 inline bool PropVariant_SetFrom_DosTime(NWindows::NCOM::CPropVariant &prop, UInt32 dosTime)
136 {
137 FILETIME localFileTime, utc;
138 if (!NWindows::NTime::DosTime_To_FileTime(dosTime, localFileTime))
139 return false;
140 if (!LocalFileTimeToFileTime(&localFileTime, &utc))
141 return false;
142 prop.SetAsTimeFrom_FT_Prec(utc, k_PropVar_TimePrec_DOS);
143 return true;
144 }
145
146 #endif
147