Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cTimeDouble.h
Go to the documentation of this file.
1 //
6 //
7 
8 #ifndef _INC_cTimeDouble_H
9 #define _INC_cTimeDouble_H
10 #ifndef NO_PRAGMA_ONCE
11 #pragma once
12 #endif
13 
14 #include "cString.h"
15 #include "cTimeUnits.h"
16 #include "cTimeFile.h"
17 
18 namespace Gray
19 {
21  {
28  public:
29  static const int k_nDaysDiffTimeInt = 25569;
30  static const double k_nY2K;
31  static const double k_nY10;
32  static const int k_nZero = 0;
33 
34  protected:
35  double m_dateTime;
36 
37  protected:
38  bool InitTimeUnits(const cTimeUnits& rTu);
39  void DecodeDate(cTimeUnits& rTu) const;
40 
41  public:
43  : m_dateTime(dt.m_dateTime)
44  {
45  }
46  cTimeDouble(const double dTime = k_nZero)
47  : m_dateTime(dTime)
48  {
49  }
50 
51  static cTimeDouble GRAYCALL EncodeSeconds(double s);
52  static cTimeDouble GRAYCALL EncodeTime(short h, short m, short s, short ms = 0);
53  static cTimeDouble GRAYCALL EncodeDate(short year = 0, short month = 0, short day = 0);
54  static cTimeDouble GRAYCALL GetTimeFromSec(TIMESEC_t nTimeSec);
55 
56  cTimeDouble(const TIMESEC_t nTimeSec)
57  : m_dateTime(GetTimeFromSec(nTimeSec))
58  {
59  }
60 
61  static cTimeDouble GRAYCALL GetTimeFromFile(const cTimeFile& ft);
63  : m_dateTime(GetTimeFromFile(ft))
64  {
65  }
66 
67  static cTimeDouble GRAYCALL GetTimeFromStr(const GChar_t* pszDateTime, TZ_TYPE nTimeZoneOffset)
68  {
69  // Ignore HRESULT.
70  cTimeDouble t;
71  t.SetTimeStr(pszDateTime, nTimeZoneOffset);
72  return t;
73  }
74  cTimeDouble(const cTimeUnits& rTu)
75  {
78  InitTimeUnits(rTu);
79  }
80 
81  void InitTime(double dTime = k_nZero)
82  {
83  m_dateTime = dTime;
84  }
85  void InitTimeNow();
86 
87  static inline bool IsTimeValid(double dTime)
88  {
89  return(dTime > k_nZero);
90  }
91  bool isTimeValid() const
92  {
93  return IsTimeValid(m_dateTime);
94  }
95  double get_Double() const
96  {
98  return m_dateTime;
99  }
100  double get_Days() const
101  {
103  return m_dateTime;
104  }
105  operator double(void) const
106  {
108  return m_dateTime;
109  }
110 
111  void operator = (const cTimeDouble& date)
112  {
113  m_dateTime = date.m_dateTime;
114  }
115  void operator = (const GChar_t* pszDateTime)
116  {
117  SetTimeStr(pszDateTime, TZ_UTC);
118  }
119 
121  {
123  return cTimeDouble(m_dateTime + i);
124  }
125  cTimeDouble operator - (int i)
126  {
128  return cTimeDouble(m_dateTime - i);
129  }
131  {
132  return cTimeDouble(m_dateTime + dt.m_dateTime);
133  }
134  cTimeDouble operator - (const cTimeDouble& dt)
135  {
136  return cTimeDouble(m_dateTime - dt.m_dateTime);
137  }
138 
139  cTimeDouble& operator += (int idays)
140  {
142  m_dateTime += idays;
143  return *this;
144  }
145  cTimeDouble& operator -= (int idays)
146  {
148  m_dateTime -= idays;
149  return *this;
150  }
151  cTimeDouble& operator += (const cTimeDouble& dt)
152  {
153  m_dateTime += dt.m_dateTime;
154  return *this;
155  }
156  cTimeDouble& operator -= (const cTimeDouble& dt)
157  {
158  m_dateTime -= dt.m_dateTime;
159  return *this;
160  }
161 
162  cTimeDouble& operator ++ ()
163  {
164  m_dateTime += 1;
165  return *this;
166  }
167  cTimeDouble& operator ++ (int)
168  {
169  m_dateTime += 1; // add a day
170  return *this;
171  }
172  cTimeDouble& operator -- ()
173  {
174  m_dateTime -= 1;
175  return *this;
176  }
177  cTimeDouble& operator -- (int)
178  {
179  m_dateTime -= 1;
180  return *this;
181  }
182 
183  friend bool operator < (const cTimeDouble &dt1, const cTimeDouble &dt2);
184  friend bool operator <= (const cTimeDouble &dt1, const cTimeDouble &dt2);
185  friend bool operator > (const cTimeDouble &dt1, const cTimeDouble &dt2);
186  friend bool operator >= (const cTimeDouble &dt1, const cTimeDouble &dt2);
187  friend bool operator == (const cTimeDouble &dt1, const cTimeDouble &dt2);
188  friend bool operator != (const cTimeDouble &dt1, const cTimeDouble &dt2);
189 
190  static cTimeDouble GRAYCALL GetTimeNow();
191  static cTimeDouble GRAYCALL Date();
192  static cTimeDouble GRAYCALL Time();
193 
194  cTimeFile GetAsFileTime() const;
195  bool GetTimeUnits(OUT cTimeUnits& rTu, TZ_TYPE nTimeZoneOffset) const;
196 
197  TIMEDOW_TYPE get_DayOfWeek() const;
198 
199  unsigned GetDate() const
200  {
202  return unsigned(m_dateTime);
203  }
204 
205  double get_DaysTil() const
206  {
210  return(get_Double() - GetTimeNow());
211  }
212  double get_DaysAge() const
213  {
218  return(-get_DaysTil());
219  }
220 
221  // to/from strings.
222  HRESULT SetTimeStr(const GChar_t* pszDateTime, TZ_TYPE nTimeZoneOffset = TZ_UTC);
223  cString GetTimeFormStr(const GChar_t* pszFormat = nullptr, TZ_TYPE nTimeZoneOffset = TZ_LOCAL) const;
224  cString GetTimeFormStr(TIME_FORMAT_TYPE eFormat, TZ_TYPE nTimeZoneOffset = TZ_LOCAL) const
225  {
226  return GetTimeFormStr((const GChar_t*)eFormat, nTimeZoneOffset);
227  }
228 
229  static cString GRAYCALL GetTimeSpanStr(double dDays, TIMEUNIT_TYPE eUnitHigh = TIMEUNIT_Day, int iUnitsDesired = 2, bool bShortText = false);
230 
232  };
233 
234  bool inline operator < (const cTimeDouble &dt1, const cTimeDouble &dt2)
235  {
236  return(dt1.m_dateTime < dt2.m_dateTime);
237  }
238  bool inline operator <= (const cTimeDouble &dt1, const cTimeDouble &dt2)
239  {
240  return((dt1.m_dateTime <= dt2.m_dateTime));
241  }
242  bool inline operator > (const cTimeDouble &dt1, const cTimeDouble &dt2)
243  {
244  return(dt1.m_dateTime > dt2.m_dateTime);
245  }
246  bool inline operator >= (const cTimeDouble &dt1, const cTimeDouble &dt2)
247  {
248  return((dt1.m_dateTime >= dt2.m_dateTime));
249  }
250  bool inline operator == (const cTimeDouble &dt1, const cTimeDouble &dt2)
251  {
252  return(dt1.m_dateTime == dt2.m_dateTime);
253  }
254  bool inline operator != (const cTimeDouble &dt1, const cTimeDouble &dt2)
255  {
256  return(dt1.m_dateTime != dt2.m_dateTime);
257  }
258 };
259 #endif // _INC_cTimeDouble_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: cTimeDouble.h:21
static const double k_nY2K
The static value for y2k = January 1, 2000 in UTC/GMT.
Definition: cTimeDouble.h:30
double m_dateTime
DATE = days since (midnight, 30 December 1899 GMT), fraction = time of day.
Definition: cTimeDouble.h:35
cTimeDouble(const cTimeDouble &dt)
Definition: cTimeDouble.h:42
cTimeDouble(const cTimeFile &ft)
Definition: cTimeDouble.h:62
unsigned GetDate() const
< Numeric date of date object
Definition: cTimeDouble.h:199
bool isTimeValid() const
Definition: cTimeDouble.h:91
static bool IsTimeValid(double dTime)
Definition: cTimeDouble.h:87
static const double k_nY10
The first 10 years are sometimes reserved to act as offsets.
Definition: cTimeDouble.h:31
static cTimeDouble __stdcall GetTimeFromStr(const GChar_t *pszDateTime, TZ_TYPE nTimeZoneOffset)
Definition: cTimeDouble.h:67
cTimeDouble(const cTimeUnits &rTu)
Definition: cTimeDouble.h:74
double get_DaysAge() const
Definition: cTimeDouble.h:212
double get_DaysTil() const
Definition: cTimeDouble.h:205
HRESULT SetTimeStr(const GChar_t *pszDateTime, TZ_TYPE nTimeZoneOffset=TZ_UTC)
Definition: cTimeDouble.cpp:290
cTimeDouble(const double dTime=k_nZero)
Definition: cTimeDouble.h:46
void InitTime(double dTime=k_nZero)
Definition: cTimeDouble.h:81
cString GetTimeFormStr(TIME_FORMAT_TYPE eFormat, TZ_TYPE nTimeZoneOffset=TZ_LOCAL) const
Definition: cTimeDouble.h:224
double get_Double() const
Definition: cTimeDouble.h:95
double get_Days() const
Definition: cTimeDouble.h:100
cTimeDouble(const TIMESEC_t nTimeSec)
Definition: cTimeDouble.h:56
Definition: cTimeFile.h:31
Definition: cTimeUnits.h:146
< 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
bool operator<(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:234
TIMEUNIT_TYPE
Definition: cTimeUnits.h:124
@ TIMEUNIT_Day
day of month. base 1. (1<=x<=31)
Definition: cTimeUnits.h:130
TIMEDOW_TYPE
Definition: cTimeUnits.h:77
bool operator>=(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:246
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
cStringA operator+(const char *pStr1, const cStringA &s2)
Definition: cString.h:642
bool operator!=(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:254
bool operator==(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:250
bool operator>(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:242
TIME_FORMAT_TYPE
Definition: cTimeUnits.h:48
bool operator<=(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:238