The Battle for Wesnoth  1.15.3+dev
date_time.hpp
Go to the documentation of this file.
1 //
2 // M A R I A D B + +
3 //
4 // Copyright Sylvain Rochette Langlois 2013,
5 // The ViaDuck Project 2016 - 2020.
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 #ifndef _MARIADB_DATE_TIME_HPP_
11 #define _MARIADB_DATE_TIME_HPP_
12 
13 #include <iostream>
14 #include <mariadb++/time.hpp>
15 
16 namespace mariadb {
17 /**
18  * Class used to represent SQL date_time
19  */
20 class date_time : public time {
21  public:
22  /**
23  * Construct date_time from given data. An exception will be thrown on invalid data.
24  *
25  * @param year The year to set.
26  * @param month The month to set. [1-12]
27  * @param day The day to set. Make sure it exists in the month. [1-28/39/30/31]
28  * @param hour The hour to set. [0-23}
29  * @param minute The minute to set. [0-59]
30  * @param second The second to set. [0-60]
31  * @param millisecond The millisecond to set. [0-999]
32  */
33  date_time(u16 year = 1970, u8 month = 1, u8 day = 1, u8 hour = 0, u8 minute = 0, u8 second = 0,
34  u16 millisecond = 0);
35 
36  /**
37  * Copy constructor
38  *
39  * @param dt Datetime to copy from
40  */
41  date_time(const date_time& dt);
42 
43  /**
44  * Construct date_time with given time. Sets the date to Jan, 1st of 1900
45  *
46  * @param t Time to copy from
47  */
48  date_time(const time& t);
49 
50  /**
51  * Construct date_time from tm struct. Since tm cannot represent milliseconds, none will be set.
52  *
53  * @param time_struct Struct to copy from
54  */
55  date_time(const tm& time_struct);
56 
57  /**
58  * Construct date_time from given time_t. Converts the time to local timezone before setting it.
59  * Note that no milliseconds will be set.
60  *
61  * @param time Timetype to set.
62  */
63  date_time(const time_t& time);
64 
65  /**
66  * Construct date_time from MYSQL_TIME. No milliseconds will be set.
67  *
68  * @param time MYSQL_TIME to copy from.
69  */
70  date_time(const MYSQL_TIME& time);
71 
72  /**
73  * Construct date_time from ISO yyyy-mm-dd hh:mm:ss.nnn date format. Throws an exception on
74  * invalid input
75  *
76  * @param dt String containing ISO date
77  */
78  date_time(const std::string& dt);
79 
80  //
81  // Operators
82  //
83  int compare(const date_time& dt) const;
84  date_time& operator=(const date_time& dt);
85  bool operator==(const date_time& dt) const;
86  bool operator!=(const date_time& dt) const;
87  bool operator<(const date_time& dt) const;
88  bool operator<=(const date_time& dt) const;
89  bool operator>(const date_time& dt) const;
90  bool operator>=(const date_time& dt) const;
91 
92  /**
93  * Get currently set year
94  *
95  * @return Current year
96  */
97  u16 year() const;
98 
99  /**
100  * Set current year. If date becomes invalid, month and day will be reset to 1-1
101  *
102  * @param year Year to set
103  * @return Newly set year
104  */
105  u16 year(u16 year);
106 
107  /**
108  * Get currently set month
109  *
110  * @return Current month
111  */
112  u8 month() const;
113 
114  /**
115  * Set current month. If date becomes invalid, day will be reset to 1
116  *
117  * @param month Month to set
118  * @return Newly set month
119  */
120  u8 month(u8 month);
121 
122  /**
123  * Get currently set day of month
124  *
125  * @return Current day
126  */
127  u8 day() const;
128 
129  /**
130  * Set current day. If date becomes invalid, an exception will be thrown
131  *
132  * @param day Day to set
133  * @return Newly set day
134  */
135  u8 day(u8 day);
136 
137  /**
138  * Calculates the day of year from current date.
139  *
140  * @return Day of year
141  */
142  u16 day_of_year() const;
143 
144  /**
145  * Sets the date by calculating which date the day_of_year corresponds to.
146  *
147  * @return Day of year that was set
148  */
149  u16 day_of_year(u16 day_of_year);
150 
151  /**
152  * Set only date part. Invalid dates will throw an exception
153  *
154  * @param year Year to set.
155  * @param month Month to set. [1-12]
156  * @param day Day to set. [1-28/29/30/31]
157  * @return True on success
158  */
159  bool set(u16 year, u8 month, u8 day);
160 
161  /**
162  * Set date and time part. Invalid dates will throw an exception
163  *
164  * @param year Year to set.
165  * @param month Month to set. [1-12]
166  * @param day Day to set. [1-28/29/30/31]
167  * @param hour The hour to set. [0-23}
168  * @param minute The minute to set. [0-59]
169  * @param second The second to set. [0-60]
170  * @param millisecond The millisecond to set. [0-999]
171  * @return True on success
172  */
173  bool set(u16 year, u8 month, u8 day, u8 hour, u8 minute, u8 second, u16 millisecond);
174 
175  /**
176  * Set date and time to ISO yyyy-mm-dd hh:mm:ss.nnn date format.
177  * Throws an exception on invalid input
178  *
179  * @param dt String containing ISO date
180  * @return True on success
181  */
182  bool set(const std::string& dt) override;
183 
184  /**
185  * Add years to current date.
186  *
187  * @param years Number of years to add
188  * @return Newly created date_time containing result
189  */
190  date_time add_years(s32 years) const;
191 
192  /**
193  * Add months to current date with year wrapping.
194  *
195  * @param months Number of months to add
196  * @return Newly created date_time containing result
197  */
198  date_time add_months(s32 months) const;
199 
200  /**
201  * Add days to current date with month wrapping.
202  *
203  * @param days Number of days to add
204  * @return Newly created date_time containing result
205  */
206  date_time add_days(s32 days) const;
207 
208  /**
209  * Add hours to current date with day wrapping.
210  *
211  * @param hours Number of hours to add
212  * @return Newly created date_time containing result
213  */
214  date_time add_hours(s32 hours) const;
215 
216  /**
217  * Add minutes to current date with hour wrapping.
218  *
219  * @param minutes Number of minutes to add
220  * @return Newly created date_time containing result
221  */
222  date_time add_minutes(s32 minutes) const;
223 
224  /**
225  * Add seconds to current date with minute wrapping.
226  *
227  * @param seconds Number of seconds to add
228  * @return Newly created date_time containing result
229  */
230  date_time add_seconds(s32 seconds) const;
231 
232  /**
233  * Add milliseconds to current date with second wrapping.
234  *
235  * @param milliseconds Number of milliseconds to add
236  * @return Newly created date_time containing result
237  */
238  date_time add_milliseconds(s32 milliseconds) const;
239 
240  /**
241  * Adds a timespan to the date_time.
242  *
243  * @param dur The duration to add
244  * @return Newly created date_time containing result
245  */
246  date_time add(const time_span& dur) const;
247 
248  /**
249  * Subtracts a timespan from the date_time.
250  *
251  * @param dur The duration to subtract
252  * @return Newly created date_time containing result
253  */
254  date_time subtract(const time_span& dur) const;
255 
256  /**
257  * Adds a mariadb::time to the date_time.
258  *
259  * @param t Time to add
260  * @return Newly created date_time containing result
261  */
262  date_time add(const time& t) const;
263 
264  /**
265  * Substract a mariadb::time from the date_time.
266  *
267  * @param t Time to subtract
268  * @return Newly created date_time containing result
269  */
270  date_time substract(const time& t) const;
271 
272  /**
273  * Calculates the time_span between this date_time and dt. If dt > this the time_span will be
274  * negative
275  *
276  * @param dt Datetime to calculate the timespan to
277  * @return Timespan representing the time between this and dt
278  */
279  time_span time_between(const date_time& dt) const;
280 
281  /**
282  * Indicates whether this date is considered valid
283  *
284  * @return True if date is valid
285  */
286  bool is_valid() const override;
287 
288  /**
289  * Indicates whether a given year is leap according to gregorian calendar
290  *
291  * @param year The year to check
292  * @return True if year is leap
293  */
294  static bool is_leap_year(u16 year);
295 
296  /**
297  * Indicates whether a given date is valid in terms of existing month and day in month. Accounts
298  * for leap years
299  *
300  * @param year Year to check
301  * @param month Month to check
302  * @param day Day to check
303  * @return True if date is valid
304  */
305  static bool valid_date(u16 year, u8 month, u8 day);
306 
307  /**
308  * Gets the number of days in a given year. Accounts for leap years
309  *
310  * @param year Year to check
311  * @return Number of days in year
312  */
313  static u16 days_in_year(u16 year);
314 
315  /**
316  * Gets the number of days in a given month of a given year. Accounts for leap years
317  *
318  * @param year Year to check
319  * @param month Month to check
320  * @return Number of days in month of year
321  */
322  static u8 days_in_month(u16 year, u8 month);
323 
324  /**
325  * Gets the day of year for given date. Accounts for leap years
326  *
327  * @param year Year of date
328  * @param month Month of date
329  * @param day Day of date
330  * @return Day of year of given date
331  */
332  static u16 day_of_year(u16 year, u8 month, u8 day);
333 
334  /**
335  * Gets the date from given year and day of year
336  *
337  * @param year Year in which to position day_of_year
338  * @param day_of_year Position of day within the year
339  * @return Datetime representing the exact date
340  */
341  static date_time reverse_day_of_year(u16 year, u16 day_of_year);
342 
343  /**
344  * Convert the date_time to a time_t. Precision is limited to seconds
345  *
346  * @return Timetype representing the date_time
347  */
348  time_t mktime() const;
349 
350  /**
351  * Uses ::difftime to calculate number of seconds between two dates
352  *
353  * @param dt Datetime to calculate difference to
354  * @return Number of seconds with fractions between the two dates
355  */
356  double diff_time(const date_time& dt) const;
357 
358  /**
359  * Gets only the date part of this date_time
360  *
361  * @return Datetime representing only the date part
362  */
363  date_time date() const;
364 
365  /**
366  * Converts the date_time to a MYSQL_TIME. Precision is limited to seconds
367  *
368  * @return MYSQL_TIME representing this date_time
369  */
370  MYSQL_TIME mysql_time() const;
371 
372  /**
373  * Gets the current date and time as date_time. Does not set milliseconds
374  *
375  * @return Current date and time in local timezone
376  */
377  static date_time now();
378 
379  /**
380  * Gets the current date and time as date_time. Does not set milliseconds
381  *
382  * @return Current date and time in UTC
383  */
384  static date_time now_utc();
385 
386  /**
387  * Converts the date and time to ISO 8601 string yyyy-mm-dd hh:mm:ss[.nnn]
388  *
389  * @param with_millisecond Controls whether or not to print optional .nnn part
390  * @return String containing ISO date and time
391  */
392  const std::string str(bool with_millisecond = false) const;
393 
394  /**
395  * Converts only the date part of this date_time to ISO 8601 date string yyyy-mm-dd
396  *
397  * @return String containing ISO date
398  */
399  const std::string str_date() const;
400 
401  private:
405 };
406 
407 std::ostream& operator<<(std::ostream& os, const date_time& ddt);
408 }
409 
410 #endif
u16 millisecond() const
Get the current millisecond 0-999.
Definition: time.cpp:121
bool is_valid() const override
Indicates whether this date is considered valid.
Definition: date_time.cpp:412
signed int s32
Definition: types.hpp:25
u16 day_of_year() const
Calculates the day of year from current date.
Definition: date_time.cpp:152
Class representing SQL time.
Definition: time.hpp:23
bool operator>=(const date_time &dt) const
Definition: date_time.cpp:91
date_time add_minutes(s32 minutes) const
Add minutes to current date with hour wrapping.
Definition: date_time.cpp:239
static u16 days_in_year(u16 year)
Gets the number of days in a given year.
Definition: date_time.cpp:433
date_time add_years(s32 years) const
Add years to current date.
Definition: date_time.cpp:163
u8 second() const
Get the current second 0-61 (leap second possible)
Definition: time.cpp:110
date_time subtract(const time_span &dur) const
Subtracts a timespan from the date_time.
Definition: date_time.cpp:308
date_time substract(const time &t) const
Substract a mariadb::time from the date_time.
Definition: date_time.cpp:338
u8 minute() const
Get the current minute 0-59.
Definition: time.cpp:99
static date_time now()
Gets the current date and time as date_time.
Definition: date_time.cpp:507
date_time add_hours(s32 hours) const
Add hours to current date with day wrapping.
Definition: date_time.cpp:216
static bool is_leap_year(u16 year)
Indicates whether a given year is leap according to gregorian calendar.
Definition: date_time.cpp:416
std::ostream & operator<<(std::ostream &os, const date_time &ddt)
Definition: date_time.cpp:587
MYSQL_TIME mysql_time() const
Converts the date_time to a MYSQL_TIME.
Definition: date_time.cpp:481
u16 year() const
Get currently set year.
Definition: date_time.cpp:108
u8 hour() const
Get the current hour 0-23.
Definition: time.cpp:88
date_time & operator=(const date_time &dt)
Definition: date_time.cpp:76
bool operator>(const date_time &dt) const
Definition: date_time.cpp:89
int compare(const date_time &dt) const
Definition: date_time.cpp:60
date_time add(const time_span &dur) const
Adds a timespan to the date_time.
Definition: date_time.cpp:316
double diff_time(const date_time &dt) const
Uses ::difftime to calculate number of seconds between two dates.
Definition: date_time.cpp:498
bool operator!=(const date_time &dt) const
Definition: date_time.cpp:83
unsigned char u8
Definition: types.hpp:20
static u8 days_in_month(u16 year, u8 month)
Gets the number of days in a given month of a given year.
Definition: date_time.cpp:435
const std::string str_date() const
Converts only the date part of this date_time to ISO 8601 date string yyyy-mm-dd. ...
Definition: date_time.cpp:573
static date_time reverse_day_of_year(u16 year, u16 day_of_year)
Gets the date from given year and day of year.
Definition: date_time.cpp:454
unsigned short u16
Definition: types.hpp:21
date_time add_seconds(s32 seconds) const
Add seconds to current date with minute wrapping.
Definition: date_time.cpp:262
date_time add_milliseconds(s32 milliseconds) const
Add milliseconds to current date with second wrapping.
Definition: date_time.cpp:285
date_time add_days(s32 days) const
Add days to current date with month wrapping.
Definition: date_time.cpp:195
bool operator<(const date_time &dt) const
Definition: date_time.cpp:85
date_time add_months(s32 months) const
Add months to current date with year wrapping.
Definition: date_time.cpp:172
bool operator<=(const date_time &dt) const
Definition: date_time.cpp:87
Class used to represent SQL date_time.
Definition: date_time.hpp:20
time_span time_between(const date_time &dt) const
Calculates the time_span between this date_time and dt.
Definition: date_time.cpp:348
time_t mktime() const
Convert the date_time to a time_t.
Definition: date_time.cpp:468
date_time date() const
Gets only the date part of this date_time.
Definition: date_time.cpp:505
bool operator==(const date_time &dt) const
Definition: date_time.cpp:81
double t
Definition: astarsearch.cpp:64
static bool valid_date(u16 year, u8 month, u8 day)
Indicates whether a given date is valid in terms of existing month and day in month.
Definition: date_time.cpp:425
u8 day() const
Get currently set day of month.
Definition: date_time.cpp:139
u8 month() const
Get currently set month.
Definition: date_time.cpp:124
date_time(u16 year=1970, u8 month=1, u8 day=1, u8 hour=0, u8 minute=0, u8 second=0, u16 millisecond=0)
Construct date_time from given data.
Definition: date_time.cpp:31
static date_time now_utc()
Gets the current date and time as date_time.
Definition: date_time.cpp:519
const std::string str(bool with_millisecond=false) const
Converts the date and time to ISO 8601 string yyyy-mm-dd hh:mm:ss[.nnn].
Definition: date_time.cpp:565