KSquare Utilities
DateTime.h
Go to the documentation of this file.
1 /* DateTime.h -- Classes to support Date and Time functionality.
2  * Copyright (C) 1994-2011 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 
6 /* Originally developed in ObjectPascal 1989; converted to c++ in 1995 */
7 
8 #ifndef _DATETIME_
9 #define _DATETIME_
10 
11 #include "KKStr.h"
12 
13 /**
14  * @file DateTime.h
15  * @brief Their are three classes in this file meant to represent Date, Time, and DateTime called
16  * DateType, TimeType, and DateTimeType.
17  */
18 
19 
20 namespace KKB
21 {
22  ///<summary> Represents a calendar date consisting of three fields, Year, Month, and Day. </summary>
23  ///<remarks>
24  /// This date format will support operations that will allow you to determine the number of days between two
25  /// dates; derive a new date by adding or subtracting Days, Months, or Years from an existing date, etc; and
26  /// convert date into one of several displayable formats.
27  ///</remarks>
28  class DateType
29  {
30  public:
31  DateType ();
32 
33  DateType (const DateType& _date);
34 
35  DateType (short _year,
36  uchar _month,
37  uchar _day
38  );
39 
40  ///<summary> Constructs a date from an integer that represents the total number of days since 1/1/0000 */ </summary>
41  DateType (kkint32 days);
42 
43  ///<summary> Constructs a date from a displayable string; the year, month, and days fields are detertmie3d from context. </summary>
44  ///<remarks>
45  /// Strings can be both all numeric or combination of alpha numeric. Each part of the date, (Year, Month, Day) can be separated
46  /// by &quot; &quot;, &quot;/&quot;, tab, line-feed, or carriage-return. Examples of valid strings are &quot;1997/02/25&quot;,
47  /// &quot;02/25/1997&quot;, &quot;Feb 02 1997&quot;, &quot;02/Feb/1997&quot;, &quot;1997 February 02&quot;. The ordering of
48  /// the numeric fields will be determined by context with one of three possible formats, mm/dd/yyyy, yyyy/mm/dd, and dd/mm/yyyy.
49  ///
50  /// A flaw in this schema is that if the day of the month is in the range of (1 thru 12), an all numeric format is used, and the
51  /// ordering is supposed to be &quot;dd/mm/yyyy&quot; this routine will assume that it is looking at &quot;mm/dd/yyyy&quot; with
52  /// the result of swapping the &quot;Month&quot; and &quot;Day&quot; fields. As a rule I try to always use the alpha numeric
53  /// version of the month.
54  ///</remarks>
55  DateType (KKStr s);
56 
57 
58  ///<summary> Adds a number of days to the date. </summary>
59  ///<example>
60  /// Example: Date d (&quot;April 14 1962&quot;); // Sets the instance of &quot;d&quot; to the date &quot;1962-04-14&quot;.
61  /// d.AddDays (12); // &quot;d&quot; now contains the date &quot;1962-04-24&quot;.
62  /// d.AddDays (-25); // &quot;d&quot; now contains the date &quot;1962-03-30&quot;.
63  ///</example>
64  void AddDays (kkint32 _days);
65 
66  void AddMonths (kkint32 _months);
67 
68  void SubtractDays (kkint32 _days);
69 
70  short Year () const {return year;}
71  uchar Month () const {return month;}
72  uchar Day () const {return day;}
73 
74  ///<summary> Returns back the number of days since &quot;1/1/0000&quot;. </summary>
75  ///<remarks>
76  /// This can be used to determine the number of days between two different dates. For example you have two
77  /// instances of &quot;DateType&quot; &quot;d1&quot; and &quot;d2&quot;. The number of days between the two dates can be determined by
78  /// &quot;int deltaDays = d1.Days () - d2.Days ();&quot;.
79  ///</remarks>
80  kkint32 Days () const;
81 
82  kkuint64 Seconds () const;
83 
84  kkuint32 ToDays () const;
85  kkuint32 ToHours () const;
86  kkuint64 ToSeconds () const;
87 
88  KKStr MM_DD_YY () const;
89  KKStr MMM_DD_YYYY () const; /**< @brief Convert into displayable string; ex: May/02/2010. */
90  KKStr YY_MM_DD () const; /**< @brief Convert into displayable string; ex: 11/05/17 */
91  KKStr YYYY_MM_DD () const; /**< @brief Convert into displayable string; ex: 20011/05/17 */
92  KKStr YYYY_MMM_DD () const; /**< @brief Convert into displayable string; ex: 20011/May/17 */
93  KKStr YYYYMMDD () const; /**< @brief Convert into displayable string; ex: 20110517 */
94 
95  DateType& operator= (const DateType& right);
96  bool operator== (const DateType& right) const;
97  bool operator!= (const DateType& right) const;
98  bool operator> (const DateType& right) const;
99  bool operator>= (const DateType& right) const;
100  bool operator< (const DateType& right) const;
101  bool operator<= (const DateType& right) const;
102  DateType operator+ (const DateType& right) const;
103  DateType operator+ (kkint32 right) const;
104  DateType operator- (kkint32 right) const;
105  DateType operator- (const DateType& right) const;
106  DateType& operator++ ();
107 
108  uchar DaysThisMonth () const; /**< @brief returns the number of days in 'month' and if leapYear and February returns 29. */
109 
110  static uchar MonthFromStr (const KKStr& monthStr);
111 
112  static uchar DaysInTheMonth (kkint32 year,
113  uchar month
114  );
115 
116  static kkint32 DaysYTDforMonth (kkint32 year,
117  uchar month
118  );
119 
120  private:
121  void AdjustYear ();
122  void SetFromNumOfDaysInTime (kkint32 days);
123 
124  kkint32 Compare (const DateType& right) const;
125 
126 
127 
128  static void AdjustDateFields (kkint32& years,
129  kkint32& months,
130  kkint32& days
131  );
132 
133  static const uchar daysInEachMonth[];
134  static const short daysYTDforMonth[];
135  static const char* monthlyShortNames[];
136  static const char* monthlyNames[];
137 
138  friend class DateTime;
139 
140  short year;
141  uchar month;
142  uchar day;
143  }; /* DateType */
144 
145 
146 
147 
148  ///<summary> Represents a Time, consisting of three fields, Hour, Minute, and Second. </summary>
149  class TimeType
150  {
151  public:
152  TimeType ();
153 
154  TimeType (const TimeType& time);
155 
156  TimeType (uchar _hour,
157  uchar _minute,
158  uchar _second
159  );
160 
161  TimeType (kkint32 seconds);
162 
163  TimeType (KKStr s);
164 
165  uchar Hour () const {return hour;}
166  uchar Minute () const {return minute;}
167  uchar Second () const {return second;}
168 
169  kkuint32 Seconds () const;
170  double ToHours () const;
171  double ToMinutes () const;
172 
173  void Hour (uchar _hour) {hour = _hour;}
174  void Minute (uchar _minute) {minute = _minute;}
175  void Second (uchar _second) {second = _second;}
176 
177  KKStr HH_MM_SS () const;
178  KKStr HHMMSS () const;
179 
180 
181  TimeType& operator= (const TimeType& right);
182  bool operator== (const TimeType& right) const;
183  bool operator!= (const TimeType& right) const;
184  bool operator> (const TimeType& right) const;
185  bool operator>= (const TimeType& right) const;
186  bool operator< (const TimeType& right) const;
187  bool operator<= (const TimeType& right) const;
188  TimeType operator+ (const TimeType& right) const; /**< Add one day */
189  TimeType operator- (const TimeType& right) const; /**< Subtract one day */
190 
191  private:
192  friend class DateTime;
193 
194  kkint32 Compare (const TimeType& right) const;
195 
196  uchar hour;
197  uchar minute;
198  uchar second;
199  };
200 
201 
202 
203  ///<summary>Represents Date and Time, consists of two member classes DateType and TimeType.</summary>
204  class DateTime
205  {
206  public:
207  DateTime ();
208 
209  DateTime (const DateTime& dateTime);
210 
211  DateTime (const DateType& _date,
212  const TimeType& _time
213  );
214 
215  DateTime (short _year,
216  uchar _month,
217  uchar _day,
218  uchar _hour,
219  uchar _minute,
220  uchar _second
221  );
222 
223  DateTime (const KKStr& s);
224 
225  void AddDays (kkint32 _days);
226  void AddHours (kkint32 _hours);
227  void AddMinutes (kkint32 _mins);
228  void AddSeconds (long _secs);
229 
230  const KKB::DateType& Date () const {return date;}
231 
232  const TimeType& Time () const {return time;}
233 
234  kkuint64 Seconds () const;
235 
236  short Year () const {return date.Year ();}
237  uchar Month () const {return date.Month ();}
238  uchar Day () const {return date.Day ();}
239 
240  kkuint32 ToDays () const;
241  double ToHours () const;
242  kkuint64 ToSeconds () const; /**< Number seconds since "0000/01/01 00:00:00" */
243 
244  void HoursAdd (kkint32 hours); /**< @brief Add _hours to DateTime, will adjust date to accommodate 24 hour clock. */
245  void MinutesAdd (kkint32 _mins);
246  void SecondsAdd (long _secs);
247 
248  KKStr YYYYMMDDHHMMSS () const;
249 
250  KKStr YYYY_MM_DD_HH_MM_SS () const;
251 
252  KKStr HH_MM_SS () const; /**< The date part will be converted into hours. */
253 
254  DateTime& operator= (const DateTime& right);
255  bool operator== (const DateTime& right) const;
256  bool operator!= (const DateTime& right) const;
257  bool operator> (const DateTime& right) const;
258  bool operator>= (const DateTime& right) const;
259  bool operator< (const DateTime& right) const;
260  bool operator<= (const DateTime& right) const;
261  DateTime operator+ (const DateTime& right) const;
262  DateTime operator- (const DateTime& right) const;
263 
264  private:
265  kkint32 Compare (const DateTime& right) const;
266 
267  DateType date;
268  TimeType time;
269  }; /* DateTime */
270 
271 
272  KKStr& operator<< (KKB::KKStr& left, const DateType& right);
273  KKStr& operator<< (KKB::KKStr& left, const TimeType& right);
274  KKStr& operator<< (KKB::KKStr& left, const DateTime& right);
275 
276  std::ostream& operator<< (std::ostream& os, const DateType& right);
277  std::ostream& operator<< (std::ostream& os, const TimeType& right);
278  std::ostream& operator<< (std::ostream& os, const DateTime& right);
279 
280 } /* KKB */
281 #endif
bool operator>(const DateTime &right) const
Definition: DateTime.cpp:1255
bool operator==(const TimeType &right) const
Definition: DateTime.cpp:890
void AddMinutes(kkint32 _mins)
Definition: DateTime.cpp:1139
bool operator==(const DateType &right) const
Definition: DateTime.cpp:542
bool operator<=(const DateTime &right) const
Definition: DateTime.cpp:1274
kkint32 Days() const
Definition: DateTime.cpp:475
bool operator>=(const DateType &right) const
Definition: DateTime.cpp:562
uchar Month() const
Definition: DateTime.h:71
kkuint32 Seconds() const
Definition: DateTime.cpp:925
kkuint32 ToDays() const
Definition: DateTime.cpp:510
void AddDays(kkint32 _days)
Definition: DateTime.cpp:1108
KKStr YYYYMMDDHHMMSS() const
Definition: DateTime.cpp:1209
bool operator<(const DateTime &right) const
Definition: DateTime.cpp:1268
DateType operator-(kkint32 right) const
Definition: DateTime.cpp:634
DateTime operator+(const DateTime &right) const
Definition: DateTime.cpp:1281
double ToHours() const
summary>Number seconds since "0000/01/01 00:00:00"
Definition: DateTime.cpp:1101
void AddSeconds(long _secs)
Definition: DateTime.cpp:1159
void MinutesAdd(kkint32 _mins)
Definition: DateTime.cpp:1194
__int32 kkint32
Definition: KKBaseTypes.h:88
KKStr HHMMSS() const
Definition: DateTime.cpp:847
uchar Month() const
Definition: DateTime.h:237
DateType(KKStr s)
Definition: DateTime.cpp:187
bool operator==(const DateTime &right) const
Definition: DateTime.cpp:1241
KKStr YYYYMMDD() const
Convert into displayable string; ex: 20110517.
Definition: DateTime.cpp:727
bool operator<=(const TimeType &right) const
Definition: DateTime.cpp:961
TimeType & operator=(const TimeType &right)
Definition: DateTime.cpp:879
KKStr HH_MM_SS() const
Definition: DateTime.cpp:841
uchar Hour() const
Definition: DateTime.h:165
DateType(short _year, uchar _month, uchar _day)
summary> Constructs a date from an integer that represents the total number of days since 1/1/0000 */...
Definition: DateTime.cpp:175
DateType(const DateType &_date)
Definition: DateTime.cpp:166
void HoursAdd(kkint32 hours)
Add _hours to DateTime, will adjust date to accommodate 24 hour clock.
Definition: DateTime.cpp:1186
void SubtractDays(kkint32 _days)
Definition: DateTime.cpp:414
TimeType(kkint32 seconds)
Definition: DateTime.cpp:785
bool operator!=(const TimeType &right) const
Definition: DateTime.cpp:897
DateTime(const KKStr &s)
Definition: DateTime.cpp:1043
KKStr HH_MM_SS() const
Definition: DateTime.cpp:1222
void Hour(uchar _hour)
Definition: DateTime.h:173
DateType operator-(const DateType &right) const
Definition: DateTime.cpp:641
KKStr YYYY_MM_DD() const
Convert into displayable string; ex: 20011/05/17.
Definition: DateTime.cpp:701
KKStr YY_MM_DD() const
Convert into displayable string; ex: 11/05/17.
Definition: DateTime.cpp:691
kkuint64 ToSeconds() const
Definition: DateTime.cpp:512
KKStr MMM_DD_YYYY() const
Convert into displayable string; ex: May/02/2010.
Definition: DateTime.cpp:676
void AddDays(kkint32 _days)
Definition: DateTime.cpp:381
static uchar MonthFromStr(const KKStr &monthStr)
Definition: DateTime.cpp:95
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
static uchar DaysInTheMonth(kkint32 year, uchar month)
Definition: DateTime.cpp:114
KKStr MM_DD_YY() const
Definition: DateTime.cpp:665
uchar Day() const
Definition: DateTime.h:72
const KKB::DateType & Date() const
Definition: DateTime.h:230
bool operator>(const TimeType &right) const
Definition: DateTime.cpp:905
bool operator>(const DateType &right) const
Definition: DateTime.cpp:555
short Year() const
Definition: DateTime.h:236
bool operator<=(const DateType &right) const
Definition: DateTime.cpp:577
KKTHread * KKTHreadPtr
bool operator<(const DateType &right) const
Definition: DateTime.cpp:569
void AddHours(kkint32 _hours)
Definition: DateTime.cpp:1114
uchar Day() const
Definition: DateTime.h:238
DateType operator+(kkint32 right) const
Definition: DateTime.cpp:628
uchar Second() const
Definition: DateTime.h:167
kkuint64 ToSeconds() const
Definition: DateTime.cpp:1105
void Second(uchar _second)
Definition: DateTime.h:175
static kkint32 DaysYTDforMonth(kkint32 year, uchar month)
Definition: DateTime.cpp:132
DateType & operator=(const DateType &right)
Definition: DateTime.cpp:462
DateTime operator-(const DateTime &right) const
Definition: DateTime.cpp:1293
bool operator<(const TimeType &right) const
Definition: DateTime.cpp:919
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
TimeType(KKStr s)
Definition: DateTime.cpp:802
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
DateTime(short _year, uchar _month, uchar _day, uchar _hour, uchar _minute, uchar _second)
Definition: DateTime.cpp:1028
bool operator>=(const TimeType &right) const
Definition: DateTime.cpp:912
DateTime(const DateTime &dateTime)
Definition: DateTime.cpp:1006
Represents a Time, consisting of three fields, Hour, Minute, and Second. summary>Represents Date and ...
Definition: DateTime.h:149
unsigned __int64 kkuint64
Definition: KKBaseTypes.h:91
KKStr YYYY_MM_DD_HH_MM_SS() const
Definition: DateTime.cpp:1216
kkuint32 ToDays() const
Definition: DateTime.cpp:1100
uchar DaysThisMonth() const
returns the number of days in &#39;month&#39; and if leapYear and February returns 29.
Definition: DateTime.cpp:317
uchar Minute() const
Definition: DateTime.h:166
friend std::ostream & operator<<(std::ostream &os, const Matrix &matrix)
kkuint64 Seconds() const
Definition: DateTime.cpp:1092
TimeType operator+(const TimeType &right) const
Definition: DateTime.cpp:946
kkuint32 ToHours() const
Definition: DateTime.cpp:511
DateType operator+(const DateType &right) const
Definition: DateTime.cpp:621
bool operator!=(const DateType &right) const
Definition: DateTime.cpp:548
double ToMinutes() const
Definition: DateTime.cpp:937
DateType(kkint32 days)
Definition: DateTime.cpp:311
TimeType(const TimeType &time)
Definition: DateTime.cpp:766
void SecondsAdd(long _secs)
Definition: DateTime.cpp:1202
TimeType operator-(const TimeType &right) const
Definition: DateTime.cpp:953
DateType & operator++()
Definition: DateTime.cpp:647
void Minute(uchar _minute)
Definition: DateTime.h:174
double ToHours() const
Definition: DateTime.cpp:931
bool operator!=(const DateTime &right) const
Definition: DateTime.cpp:1248
KKStr YYYY_MMM_DD() const
Convert into displayable string; ex: 20011/May/17.
Definition: DateTime.cpp:712
short Year() const
Definition: DateTime.h:70
TimeType(uchar _hour, uchar _minute, uchar _second)
Definition: DateTime.cpp:774
bool operator>=(const DateTime &right) const
Definition: DateTime.cpp:1262
kkuint64 Seconds() const
Definition: DateTime.cpp:502
Represents a calendar date consisting of three fields, Year, Month, and Day.
Definition: DateTime.h:28
DateTime(const DateType &_date, const TimeType &_time)
Definition: DateTime.cpp:1016
DateTime & operator=(const DateTime &right)
Definition: DateTime.cpp:1231
const TimeType & Time() const
Definition: DateTime.h:232
void AddMonths(kkint32 _months)
Definition: DateTime.cpp:446