Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cTimeInt.h
Go to the documentation of this file.
1 //
10 //
11 
12 #ifndef _INC_cTimeInt_H
13 #define _INC_cTimeInt_H
14 #ifndef NO_PRAGMA_ONCE
15 #pragma once
16 #endif
17 
18 #include "cString.h"
19 #include "cTimeFile.h"
20 
21 namespace Gray
22 {
23 #ifndef _MFC_VER
25  {
33 
34  protected:
36 
37  public:
38  CTime(TIMESEC_t nTime = ((TIMESEC_t)0)) noexcept
39  : m_time(nTime)
40  {
41  }
42  CTime(const cTimeFile& fileTime, int nDST = -1);
43 
44  const CTime& operator=(const CTime& timeSrc) noexcept
45  {
46  m_time = timeSrc.m_time;
47  return *this;
48  }
49  const CTime& operator=(TIMESEC_t nTime) noexcept
50  {
51  m_time = nTime;
52  return *this;
53  }
54 
55  bool operator<=(TIMESEC_t nTime) const noexcept
56  {
57  return(m_time <= nTime);
58  }
59  bool operator==(TIMESEC_t nTime) const noexcept
60  {
61  return(m_time == nTime);
62  }
63  bool operator!=(TIMESEC_t nTime) const noexcept
64  {
65  return(m_time != nTime);
66  }
67  bool operator>=(CTime ttime) const noexcept
68  {
69  return(m_time >= ttime.m_time);
70  }
71 
72  operator TIMESEC_t() const noexcept
73  {
74  return m_time;
75  }
76  TIMESEC_t GetTime() const noexcept // Assume time in seconds. (MFC like)
77  {
78  return m_time;
79  }
80  TIMESEC_t GetTotalSeconds() const noexcept
81  {
82  return m_time;
83  }
84  };
85 
87  {
90  public:
92  public:
94  {
95  }
96  };
97 #endif
98 
100  : public CTime
101  {
107 
108  typedef CTime SUPER_t;
109 
110  public:
111  static const TIMESEC_t k_nZero = ((TIMESEC_t)0);
112  static const TIMESEC_t k_nY2K = ((TIMESEC_t)0x386d4380);
113 
114  protected:
115  bool InitTimeUnits(const cTimeUnits& rTu);
116 
117  public:
118  cTimeInt() // init to zero
119  {}
120  cTimeInt(TIMESEC_t time) : CTime(time)
121  {}
122  cTimeInt(const cTimeFile& fileTime) : CTime(fileTime, -1)
123  {
125  }
126  cTimeInt(const cTimeUnits& rTu)
127  {
128  InitTimeUnits(rTu);
129  }
130  static TIMESEC_t GRAYCALL GetTimeFromDays(double dTimeDays) noexcept;
131 
132  cTimeInt(double dTimeDays) : CTime(GetTimeFromDays(dTimeDays))
133  {
134  }
135 
136  static cTimeInt GRAYCALL GetTimeFromStr(const GChar_t* pszDateTime, TZ_TYPE nTimeZoneOffset)
137  {
138  // Ignore HRESULT.
139  cTimeInt t;
140  t.SetTimeStr(pszDateTime, nTimeZoneOffset);
141  return t;
142  }
143 
144 #ifdef _MFC_VER
145  TIMESEC_t GetTime() const // Assume time in total seconds. (MFC like)
146  { return (TIMESEC_t) SUPER_t::GetTime(); } // convert 64 bit time to old 32 bit form?
147 #endif
148 
149  static cTimeInt GRAYCALL GetTimeNow() noexcept;
150 
151  static cTimeInt GRAYCALL GetCurrentTime() noexcept
152  {
155  return GetTimeNow();
156  }
157 
158  void InitTimeNow();
159  void InitTimeNowPlusSec(TIMESECD_t iOffsetInSeconds);
160  void InitTime(TIMESEC_t nTime = k_nZero);
161 
162  cTimeFile GetAsFileTime() const;
163  bool GetTimeUnits(OUT cTimeUnits& rTu, TZ_TYPE nTimeZoneOffset= TZ_UTC) const;
164 
165  // non MFC CTime operations.
167  {
171  return((TIMESECD_t)(GetTime() - time.GetTime()));
172  }
174  {
178  cTimeInt timeNow;
179  timeNow.InitTimeNow();
180  return((TIMESECD_t)(GetTime() - timeNow.GetTime()));
181  }
183  {
186  return(-get_TimeTilSec());
187  }
188  bool isTimeFuture() const
189  {
190  return((unsigned)GetTime() > (unsigned)GetTimeNow().GetTime());
191  }
192 
193  static inline bool IsTimeValid(TIMESEC_t nTime)
194  {
195  return(nTime > k_nZero);
196  }
197  bool isTimeValid() const
198  {
200  return(IsTimeValid((TIMESEC_t)GetTime()));
201  }
202  int get_TotalDays() const // like in COleDateTimeSpan
203  {
206  return((int)(GetTime() / cTimeUnits::k_nSecondsPerDay));
207  }
208 
209  // to/from strings.
210  HRESULT SetTimeStr(const GChar_t* pszTimeDate, TZ_TYPE nTimeZoneOffset = TZ_LOCAL);
211  StrLen_t GetTimeFormStr(GChar_t* pszOut, StrLen_t iOutSizeMax, const GChar_t* pszFormat, TZ_TYPE nTimeZoneOffset = TZ_LOCAL) const;
212  cString GetTimeFormStr(const GChar_t* pszFormat = nullptr, TZ_TYPE nTimeZoneOffset = TZ_LOCAL) const;
213  cString GetTimeFormStr(TIME_FORMAT_TYPE eFormat, TZ_TYPE nTimeZoneOffset = TZ_LOCAL) const
214  {
215  return GetTimeFormStr((const GChar_t*)eFormat, nTimeZoneOffset);
216  }
217 
218  static cString GRAYCALL GetTimeSpanStr(TIMESECD_t dwSeconds, TIMEUNIT_TYPE eUnitHigh = TIMEUNIT_Day, int iUnitsDesired = 2, bool bShortText = false);
219  static cString GRAYCALL GetTimeDeltaBriefStr(TIMESECD_t dwSeconds);
220  static cString GRAYCALL GetTimeDeltaSecondsStr(TIMESECD_t dwSeconds);
221 
223  };
224 
225 };
226 
227 #endif // _INC_cTimeInt_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
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#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: cTimeInt.h:25
const CTime & operator=(const CTime &timeSrc) noexcept
Definition: cTimeInt.h:44
bool operator!=(TIMESEC_t nTime) const noexcept
Definition: cTimeInt.h:63
bool operator<=(TIMESEC_t nTime) const noexcept
Definition: cTimeInt.h:55
bool operator==(TIMESEC_t nTime) const noexcept
Definition: cTimeInt.h:59
TIMESEC_t GetTime() const noexcept
Definition: cTimeInt.h:76
const CTime & operator=(TIMESEC_t nTime) noexcept
Definition: cTimeInt.h:49
TIMESEC_t GetTotalSeconds() const noexcept
Definition: cTimeInt.h:80
TIMESEC_t m_time
Seconds. Essentially the UNIX long time format. (not usable after 2018 unless 64 bit?...
Definition: cTimeInt.h:35
bool operator>=(CTime ttime) const noexcept
Definition: cTimeInt.h:67
CTime(TIMESEC_t nTime=((TIMESEC_t) 0)) noexcept
Definition: cTimeInt.h:38
Definition: cTimeFile.h:31
< similar to the MFC CTime and cTimeSpan, not as accurate or large ranged as COleDateTime
Definition: cTimeInt.h:101
cTimeInt(double dTimeDays)
Definition: cTimeInt.h:132
HRESULT SetTimeStr(const GChar_t *pszTimeDate, TZ_TYPE nTimeZoneOffset=TZ_LOCAL)
Definition: cTimeInt.cpp:273
TIMESECD_t GetSecondsSince(const cTimeInt &time) const
Definition: cTimeInt.h:166
cTimeInt(TIMESEC_t time)
Definition: cTimeInt.h:120
TIMESECD_t get_TimeTilSec() const
Definition: cTimeInt.h:173
cTimeInt(const cTimeUnits &rTu)
Definition: cTimeInt.h:126
cTimeInt()
Definition: cTimeInt.h:118
int get_TotalDays() const
Definition: cTimeInt.h:202
static bool IsTimeValid(TIMESEC_t nTime)
Definition: cTimeInt.h:193
static cTimeInt __stdcall GetTimeFromStr(const GChar_t *pszDateTime, TZ_TYPE nTimeZoneOffset)
Definition: cTimeInt.h:136
cString GetTimeFormStr(TIME_FORMAT_TYPE eFormat, TZ_TYPE nTimeZoneOffset=TZ_LOCAL) const
Definition: cTimeInt.h:213
bool isTimeValid() const
Definition: cTimeInt.h:197
TIMESECD_t get_AgeSec() const
Definition: cTimeInt.h:182
void InitTimeNow()
Definition: cTimeInt.cpp:83
bool isTimeFuture() const
Definition: cTimeInt.h:188
cTimeInt(const cTimeFile &fileTime)
Definition: cTimeInt.h:122
Definition: cTimeInt.h:87
int m_nDiffSeconds
Definition: cTimeInt.h:91
cTimeSpan()
Definition: cTimeInt.h:93
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 StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
int TIMESECD_t
signed delta seconds. like TIMESEC_t. redefined in TimeUnits.h.
Definition: cTimeSys.h:19
TIMEUNIT_TYPE
Definition: cTimeUnits.h:124
@ TIMEUNIT_Day
day of month. base 1. (1<=x<=31)
Definition: cTimeUnits.h:130
time_t TIMESEC_t
absolute seconds since January 1, 1970. (GMT?)(signed) NOTE: Changing to __time64_t just adds more ra...
Definition: cTimeUnits.h:23
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26
TIME_FORMAT_TYPE
Definition: cTimeUnits.h:48