Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cTimeFile.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cTimeFile_H
7 #define _INC_cTimeFile_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cTimeUnits.h"
13 #include "cValT.h"
14 #include "cString.h"
15 #include "cDebugAssert.h"
16 
17 #ifdef __linux__
18 #include "cTimeVal.h"
19 // from '#include <windef.h>'
20 struct FILETIME
21 {
22  UINT64 qwDateTime; // FILETIME_t
23 };
24 #endif
25 
26 namespace Gray
27 {
28  typedef UINT64 FILETIME_t;
29 
30  class GRAYCORE_LINK cTimeFile : public FILETIME
31  {
37 
38  public:
39  static const int k_nDaysDiffTimeInt = ((369 * 365) + 89);
40  static const int k_nFreq = 10 * 1000000;
41 
42  public:
43  cTimeFile(FILETIME_t t = 0) noexcept
44  {
45  InitTime(t);
46  }
47  cTimeFile(const FILETIME& t) noexcept
48  {
49  *static_cast<FILETIME*>(this) = t;
50  }
51  cTimeFile(const cTimeUnits& tu)
52  {
53  InitTimeUnits(tu);
54  }
55 
56 #ifdef _WIN32
57  cTimeFile(const SYSTEMTIME& st, TZ_TYPE nTimeZoneOffset)
58  {
59  SetSys(st, nTimeZoneOffset);
60  }
61  void SetSys(const SYSTEMTIME& st, TZ_TYPE nTimeZoneOffset)
62  {
63  ::SystemTimeToFileTime(&st, this);
64  if (nTimeZoneOffset == TZ_LOCAL)
65  {
66  ::LocalFileTimeToFileTime(this, this);
67  }
68  ASSERT(nTimeZoneOffset == TZ_LOCAL || nTimeZoneOffset == TZ_UTC);
69  }
70  bool GetSys(SYSTEMTIME& st, TZ_TYPE nTimeZoneOffset) const
71  {
72  FILETIME ftTmp = *this;
73  if (nTimeZoneOffset == TZ_LOCAL) // adjust for TZ and DST
74  {
75  ::FileTimeToLocalFileTime(this, &ftTmp);
76  }
77  ::FileTimeToSystemTime(&ftTmp, &st);
78  ASSERT(nTimeZoneOffset == TZ_LOCAL || nTimeZoneOffset == TZ_UTC);
79  return true;
80  }
81 
82 #elif defined(__linux__)
83 
84  static inline FILETIME_t CvtFileTime(const struct timespec& tSpec)
85  {
86  // ASSUME struct timespec/cTimeSpec has same base/EPOCH as time_t cTimeInt
87  FILETIME_t nTmp = ((UINT64)k_nDaysDiffTimeInt*(UINT64)cTimeUnits::k_nSecondsPerDay);
88  nTmp += ((UINT64)tSpec.tv_sec);
89  nTmp *= k_nFreq;
90  nTmp += tSpec.tv_nsec / 100; // add nanoseconds.
91  return nTmp;
92  }
93  cTimeFile(const struct timespec& tSpec)
94  {
95  // convert struct timespec/cTimeSpec to 64 bit FILETIME number.
96  InitTime(CvtFileTime(tSpec));
97  }
98  cTimeVal get_TimeVal() const
99  {
100  FILETIME_t nTmpSec = this->get_Val() / k_nFreq; // seconds
101  return cTimeVal(nTmpSec - (k_nDaysDiffTimeInt * (FILETIME_t)cTimeUnits::k_nSecondsPerDay), // seconds
102  (this->get_Val() - (nTmpSec*k_nFreq)) / 10); // iMicroSecWait
103  }
104 #endif
105 
106  FILETIME_t& ref_Val() noexcept
107  {
110  return *reinterpret_cast<FILETIME_t*>(static_cast<FILETIME*>(this));
111  }
112  FILETIME_t get_Val() const noexcept
113  {
116  return *reinterpret_cast<const FILETIME_t*>(static_cast<const FILETIME*>(this));
117  }
118 
119  FILETIME_t get_FAT32() const noexcept
120  {
124  return get_Val() / (2 * k_nFreq);
125  }
126  TIMESECD_t get_AgeSec() const;
127 
128  bool isValid() const noexcept
129  {
130  if (get_Val() == 0)
131  return false;
132  return true;
133  }
134 
135  void InitTime(FILETIME_t t = 0) noexcept
136  {
137  ref_Val() = t;
138  }
139  void InitTimeNow();
140 
141  static cTimeFile GRAYCALL GetTimeNow();
143  {
146  return GetTimeNow();
147  }
148 
149  void InitTimeUnits(const cTimeUnits& rTu);
150  bool GetTimeUnits(OUT cTimeUnits& rTu, TZ_TYPE nTimeZoneOffset) const;
151 
152  cString GetTimeFormStr(const GChar_t* pszFormat, TZ_TYPE nTimeZoneOffset = TZ_LOCAL) const;
153 
155  };
156 
157  template <>
158  inline COMPARE_t cValT::Compare<cTimeFile>(const cTimeFile& t1, const cTimeFile& t2)
159  {
163  return cValT::Compare(t1.get_Val(), t2.get_Val());
164  }
165 
167  {
171  public:
173  public:
174  cTimeSpanFile(INT64 nDiffUnits = 0)
175  : m_nDiffUnits(nDiffUnits)
176  {
177  }
178 
179  cTimeSpanFile(int iDays, int iHours, int iMinutes, int iSeconds)
180  : m_nDiffUnits(iDays)
181  {
182  m_nDiffUnits *= 24;
183  m_nDiffUnits += iHours;
184  m_nDiffUnits *= 60;
185  m_nDiffUnits += iMinutes;
186  m_nDiffUnits *= 60;
187  m_nDiffUnits += iSeconds;
188  m_nDiffUnits *= cTimeFile::k_nFreq;
189  }
190 
191  INT64 get_Val() const
192  {
193  return m_nDiffUnits;
194  }
195  INT64 GetTotalSeconds() const
196  {
197  // MFC like call.
198  return ((INT64)m_nDiffUnits) / cTimeFile::k_nFreq;
199  }
200 
201  // cTimeUnits::GetTimeSpanStr
202 
203  };
204 };
205 
206 #endif // _INC_cTimeFile_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cTimeFile.h:31
static const int k_nFreq
100-nanosecond intervals per second = 10th of a micro second.
Definition: cTimeFile.h:40
FILETIME_t get_FAT32() const noexcept
Definition: cTimeFile.h:119
FILETIME_t get_Val() const noexcept
Definition: cTimeFile.h:112
static cTimeFile __stdcall GetCurrentTime()
Definition: cTimeFile.h:142
void InitTime(FILETIME_t t=0) noexcept
Definition: cTimeFile.h:135
cTimeFile(const cTimeUnits &tu)
Definition: cTimeFile.h:51
FILETIME_t & ref_Val() noexcept
Definition: cTimeFile.h:106
bool isValid() const noexcept
Definition: cTimeFile.h:128
cTimeFile(const FILETIME &t) noexcept
Definition: cTimeFile.h:47
cTimeFile(FILETIME_t t=0) noexcept
Definition: cTimeFile.h:43
Definition: cTimeFile.h:167
INT64 GetTotalSeconds() const
Definition: cTimeFile.h:195
cTimeSpanFile(int iDays, int iHours, int iMinutes, int iSeconds)
Definition: cTimeFile.h:179
INT64 m_nDiffUnits
Definition: cTimeFile.h:172
INT64 get_Val() const
Definition: cTimeFile.h:191
cTimeSpanFile(INT64 nDiffUnits=0)
Definition: cTimeFile.h:174
Definition: cTimeUnits.h:146
static const TIMESECD_t k_nSecondsPerDay
seconds in a day = 86400
Definition: cTimeUnits.h:165
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
TZ_TYPE
Definition: cTimeUnits.h:29
@ TZ_LOCAL
just use local time zone. might include DST ??
Definition: cTimeUnits.h:44
@ TZ_UTC
UTC = never use DST or any TZ offset.
Definition: cTimeUnits.h:35
int COMPARE_t
result of compare. 0=same, 1=a>b, -1=a<b
Definition: cValT.h:17
int TIMESECD_t
signed delta seconds. like TIMESEC_t. redefined in TimeUnits.h.
Definition: cTimeSys.h:19
UINT64 FILETIME_t
replace FILETIME for 64 bit math. Absolute 64-bit 100-nanosecond since January 1, 1601 GMT
Definition: cTimeFile.h:28
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26
static COMPARE_t Compare(const TYPE &a, const TYPE &b) noexcept
Definition: cValT.h:46