Utilities  1
Collection of utility classes and functions used by my C++ applications.
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Friends Macros
datetime.h
Go to the documentation of this file.
1 #ifndef DATETIME_H
2 #define DATETIME_H
3 
4 #include "timespan.h"
5 
6 #include "../conversion/types.h"
7 
8 #include <string>
9 
10 namespace ChronoUtilities
11 {
12 
19 {
20  DateAndTime,
21  DateOnly,
22  TimeOnly,
25 };
26 
32 enum class DayOfWeek
33 {
34  Monday,
35  Tuesday,
36  Wednesday,
37  Thursday,
38  Friday,
39  Saturday,
40  Sunday
41 };
42 
48 enum class DatePart
49 {
50  Year,
51  Month,
52  DayOfYear,
53  Day
54 };
55 
57 {
58 public:
59  constexpr DateTime();
60  constexpr DateTime(uint64 ticks);
61  static DateTime fromDate(int year = 1, int month = 1, int day = 1);
62  static DateTime fromTime(int hour = 0, int minute = 0, int second = 0, double millisecond = 0.0);
63  static DateTime fromDateAndTime(int year = 1, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, double millisecond = 0.0);
64  static DateTime fromString(const std::string &str);
65 
66  constexpr uint64 totalTicks() const;
67  int year() const;
68  int month() const;
69  int day() const;
70  int dayOfYear() const;
71  constexpr DayOfWeek dayOfWeek() const;
72  constexpr int hour() const;
73  constexpr int minute() const;
74  constexpr int second() const;
75  constexpr int millisecond() const;
76  constexpr bool isNull() const;
77  constexpr TimeSpan timeOfDay() const;
78  bool isLeapYear() const;
79  constexpr bool isSameDay(const DateTime &other) const;
80  std::string toString(DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
81  void toString(std::string &result, DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
82  static const char *printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation = false);
83 
84  static DateTime now();
85  constexpr static bool isLeapYear(int year);
86  static int daysInMonth(int year, int month);
87 
88  constexpr bool operator ==(const DateTime &other) const;
89  constexpr bool operator !=(const DateTime &other) const;
90  constexpr bool operator <(const DateTime &other) const;
91  constexpr bool operator >(const DateTime &other) const;
92  constexpr bool operator <=(const DateTime &other) const;
93  constexpr bool operator >=(const DateTime &other) const;
94  constexpr DateTime operator +(const TimeSpan &timeSpan) const;
95  constexpr DateTime operator -(const TimeSpan &timeSpan) const;
96  constexpr TimeSpan operator +(const DateTime &other) const;
97  constexpr TimeSpan operator -(const DateTime &other) const;
98  DateTime &operator +=(const TimeSpan &timeSpan);
99  DateTime &operator -=(const TimeSpan &timeSpan);
100 
101 private:
102  static uint64 dateToTicks(int year, int month, int day);
103  static uint64 timeToTicks(int hour, int minute, int second, double millisecond);
104  int getDatePart(DatePart part) const;
105 
106  uint64 m_ticks;
107  static const int m_daysPerYear;
108  static const int m_daysPer4Years;
109  static const int m_daysPer100Years;
110  static const int m_daysPer400Years;
111  static const int m_daysTo1601;
112  static const int m_daysTo1899;
113  static const int m_daysTo10000;
114  static const int m_daysToMonth365[13];
115  static const int m_daysToMonth366[13];
116  static const int m_daysInMonth365[12];
117  static const int m_daysInMonth366[12];
118 };
119 
123 constexpr inline DateTime::DateTime() :
124  m_ticks(0)
125 {}
126 
130 constexpr inline DateTime::DateTime(uint64 ticks) :
131  m_ticks(ticks)
132 {}
133 
137 inline DateTime DateTime::fromDate(int year, int month, int day)
138 {
139  return DateTime(dateToTicks(year, month, day));
140 }
141 
145 inline DateTime DateTime::fromTime(int hour, int minute, int second, double millisecond)
146 {
147  return DateTime(timeToTicks(hour, minute, second, millisecond));
148 }
149 
153 inline DateTime DateTime::fromDateAndTime(int year, int month, int day, int hour, int minute, int second, double millisecond)
154 {
155  uint64 ticks = dateToTicks(year, month, day);
156  if(ticks) {
157  return DateTime(ticks + timeToTicks(hour, minute, second, millisecond));
158  }
159  return DateTime();
160 }
161 
165 constexpr inline uint64 DateTime::totalTicks() const
166 {
167  return m_ticks;
168 }
169 
173 inline int DateTime::year() const
174 {
175  return getDatePart(DatePart::Year);
176 }
177 
181 inline int DateTime::month() const
182 {
183  return getDatePart(DatePart::Month);
184 }
185 
189 inline int DateTime::day() const
190 {
191  return getDatePart(DatePart::Day);
192 }
193 
197 inline int DateTime::dayOfYear() const
198 {
199  return getDatePart(DatePart::DayOfYear);
200 }
201 
206 constexpr inline DayOfWeek DateTime::dayOfWeek() const
207 {
208  return static_cast<DayOfWeek>((m_ticks / TimeSpan::m_ticksPerDay) % 7l);
209 }
210 
214 constexpr inline int DateTime::hour() const
215 {
216  return m_ticks / TimeSpan::m_ticksPerHour % 24ul;
217 }
218 
222 constexpr inline int DateTime::minute() const
223 {
224  return m_ticks / TimeSpan::m_ticksPerMinute % 60ul;
225 }
226 
230 constexpr inline int DateTime::second() const
231 {
232  return m_ticks / TimeSpan::m_ticksPerSecond % 60ul;
233 }
234 
238 constexpr inline int DateTime::millisecond() const
239 {
240  return m_ticks / TimeSpan::m_ticksPerMillisecond % 1000ul;
241 }
242 
247 constexpr inline bool DateTime::isNull() const
248 {
249  return m_ticks == 0;
250 }
251 
255 constexpr inline TimeSpan DateTime::timeOfDay() const
256 {
257  return TimeSpan(m_ticks % TimeSpan::m_ticksPerDay);
258 }
259 
263 inline bool DateTime::isLeapYear() const
264 {
265  return isLeapYear(year());
266 }
267 
271 constexpr inline bool DateTime::isLeapYear(int year)
272 {
273  return (year % 4 != 0)
274  ? false
275  : ((year % 100 == 0)
276  ? (year % 400 == 0)
277  : true);
278 }
279 
283 inline int DateTime::daysInMonth(int year, int month)
284 {
285  return (month >= 1 && month <= 12)
286  ? (isLeapYear(year)
287  ? m_daysInMonth366[month - 1]
288  : m_daysInMonth365[month - 1])
289  : (0);
290 }
291 
295 constexpr inline bool DateTime::isSameDay(const DateTime &other) const
296 {
297  return (m_ticks / TimeSpan::m_ticksPerDay) == (other.m_ticks / TimeSpan::m_ticksPerDay);
298 }
299 
303 constexpr inline bool DateTime::operator ==(const DateTime &other) const
304 {
305  return m_ticks == other.m_ticks;
306 }
307 
311 constexpr inline bool DateTime::operator !=(const DateTime &other) const
312 {
313  return m_ticks != other.m_ticks;
314 }
315 
319 constexpr inline bool DateTime::operator <(const DateTime &other) const
320 {
321  return m_ticks < other.m_ticks;
322 }
323 
327 constexpr inline bool DateTime::operator >(const DateTime &other) const
328 {
329  return m_ticks > other.m_ticks;
330 }
331 
335 constexpr inline bool DateTime::operator <=(const DateTime &other) const
336 {
337  return m_ticks <= other.m_ticks;
338 }
339 
343 constexpr inline bool DateTime::operator >=(const DateTime &other) const
344 {
345  return m_ticks >= other.m_ticks;
346 }
347 
352 constexpr inline DateTime DateTime::operator +(const TimeSpan &timeSpan) const
353 {
354  return DateTime(m_ticks + timeSpan.m_ticks);
355 }
356 
361 constexpr inline DateTime DateTime::operator -(const TimeSpan &timeSpan) const
362 {
363  return DateTime(m_ticks - timeSpan.m_ticks);
364 }
365 
370 constexpr inline TimeSpan DateTime::operator +(const DateTime &other) const
371 {
372  return TimeSpan(m_ticks + other.m_ticks);
373 }
374 
379 constexpr inline TimeSpan DateTime::operator -(const DateTime &other) const
380 {
381  return TimeSpan(m_ticks - other.m_ticks);
382 }
383 
387 inline DateTime &DateTime::operator +=(const TimeSpan &timeSpan)
388 {
389  m_ticks += timeSpan.m_ticks;
390  return *this;
391 }
392 
396 inline DateTime &DateTime::operator -=(const TimeSpan &timeSpan)
397 {
398  m_ticks -= timeSpan.m_ticks;
399  return *this;
400 }
401 
402 }
403 
404 #endif // DATETIME_H
constexpr int millisecond() const
Gets the millisecond component of the date represented by this instance.
Definition: datetime.h:238
DateTime & operator-=(const TimeSpan &timeSpan)
Substracts a TimeSpan from the current instance.
Definition: datetime.h:396
constexpr bool operator<(const DateTime &other) const
Indicates whether a specified DateTime is less than another specified DateTime.
Definition: datetime.h:319
static int daysInMonth(int year, int month)
Returns the number of days in the specified month and year.
Definition: datetime.h:283
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:56
Contains classes providing a means for handling date and time information.
Definition: datetime.h:10
constexpr int hour() const
Gets the hour component of the date represented by this instance.
Definition: datetime.h:214
constexpr TimeSpan timeOfDay() const
Gets the time of day as TimeSpan for this instance.
Definition: datetime.h:255
Represents a time interval.
Definition: timespan.h:27
constexpr bool isNull() const
Returns ture if the date represented by the current DateTime class is null.
Definition: datetime.h:247
static DateTime fromDateAndTime(int year=1, int month=1, int day=1, int hour=0, int minute=0, int second=0, double millisecond=0.0)
Constructs a DateTime to the specified year, month, day, hour, minute, second and millisecond...
Definition: datetime.h:153
static DateTime fromDate(int year=1, int month=1, int day=1)
Constructs a DateTime to the specified year, month, and day.
Definition: datetime.h:137
#define LIB_EXPORT
This macro marks a symbol for shared library export.
Definition: global.h:50
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
int year() const
Gets the year component of the date represented by this instance.
Definition: datetime.h:173
constexpr DateTime operator-(const TimeSpan &timeSpan) const
Substracts a TimeSpan.
Definition: datetime.h:361
constexpr bool operator==(const DateTime &other) const
Indicates whether two DateTime instances are equal.
Definition: datetime.h:303
DateTime & operator+=(const TimeSpan &timeSpan)
Adds a TimeSpan to the current instance.
Definition: datetime.h:387
constexpr bool isSameDay(const DateTime &other) const
Returns and indication whether two DateTime instances represent the same day.
Definition: datetime.h:295
constexpr uint64 totalTicks() const
Gets the number of ticks that represent the value of the current DateTime class.
Definition: datetime.h:165
constexpr int second() const
Gets the second component of the date represented by this instance.
Definition: datetime.h:230
constexpr bool operator!=(const DateTime &other) const
Indicates whether two DateTime instances are not equal.
Definition: datetime.h:311
DatePart
Specifies the date part.
Definition: datetime.h:48
constexpr DateTime()
Constructs a DateTime.
Definition: datetime.h:123
constexpr int minute() const
Gets the minute component of the date represented by this instance.
Definition: datetime.h:222
static DateTime fromTime(int hour=0, int minute=0, int second=0, double millisecond=0.0)
Constructs a DateTime to the specified hour, minute, second and millisecond.
Definition: datetime.h:145
int dayOfYear() const
Gets the day of the year represented by this instance.
Definition: datetime.h:197
DateTimeOutputFormat
Specifies the output format.
Definition: datetime.h:18
constexpr bool operator>(const DateTime &other) const
Indicates whether a specified DateTime is greater than another specified DateTime.
Definition: datetime.h:327
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds a TimeSpan.
Definition: datetime.h:352
constexpr bool operator>=(const DateTime &other) const
Indicates whether a specified DateTime is greater or equal than another specified DateTime...
Definition: datetime.h:343
constexpr DayOfWeek dayOfWeek() const
Gets the day of the week represented by this instance.
Definition: datetime.h:206
DayOfWeek
Specifies the day of the week.
Definition: datetime.h:32
constexpr bool operator<=(const DateTime &other) const
Indicates whether a specified DateTime is less or equal than another specified DateTime.
Definition: datetime.h:335
int month() const
Gets the month component of the date represented by this instance.
Definition: datetime.h:181
bool isLeapYear() const
Returns an indication whether the year of the dae represented by this instance is a leap year...
Definition: datetime.h:263
int day() const
Gets the day component of the date represented by this instance.
Definition: datetime.h:189