The Battle for Wesnoth  1.15.3+dev
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 // Frantisek Boranek 2015,
6 // The ViaDuck Project 2016 - 2020.
7 // Distributed under the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 
11 #ifndef _MARIADB_TIME_HPP_
12 #define _MARIADB_TIME_HPP_
13 
14 #include <iostream>
15 #include <mariadb++/time_span.hpp>
16 
17 #include <mysql.h>
18 
19 namespace mariadb {
20 /**
21  * Class representing SQL time
22  */
23 class time {
24  public:
25  /**
26  * Construct time using given values
27  *
28  * @param hour Hours 0-23
29  * @param minute Minutes 0-59
30  * @param second Seconds 0-61 (for leap seconds)
31  * @param millisecond Milliseconds 0-999
32  */
33  time(u8 hour = 0, u8 minute = 0, u8 second = 0, u16 millisecond = 0);
34 
35  /**
36  * Copy constructor
37  *
38  * @param t Time to copy from
39  */
40  time(const time& t);
41 
42  /**
43  * Construct time from time.h functions
44  *
45  * @param time_struct Timestruct to copy from
46  */
47  time(const tm& time_struct);
48 
49  /**
50  * Construct time from time.h functions
51  *
52  * @param time Timestruct to copy from
53  */
54  time(const time_t& time);
55 
56  /**
57  * Construct time from SQL time
58  *
59  * @param time SQL time to copy from
60  */
61  time(const MYSQL_TIME& time);
62 
63  /**
64  * Construct time from string
65  * The format needs to be hh[:mm][:ss][:nnn]
66  * where less digits are possible and the delimiter may be any non digit
67  *
68  * @param t String containing time representation
69  */
70  time(const std::string& t);
71 
72  /**
73  * Allow proper destruction in derived classes
74  */
75  virtual ~time() = default;
76 
77  /**
78  * Compare this instance to given instance
79  *
80  * @param t Time to compare to
81  * @return 1 if this instance is greater, -1 if t is greater, 0 on equality
82  */
83  int compare(const time& t) const;
84 
85  /**
86  * Assigns a value to this instance
87  */
88  time& operator=(const time& t);
89 
90  /**
91  * Checks for equality
92  */
93  bool operator==(const time& t) const;
94 
95  /**
96  * Checks for unequality
97  */
98  bool operator!=(const time& t) const;
99 
100  /**
101  * Checks if this instance is lesser than t
102  */
103  bool operator<(const time& t) const;
104 
105  /**
106  * Checks if this instance is lesser or equal to t
107  */
108  bool operator<=(const time& t) const;
109 
110  /**
111  * Checks if this instance is greater than t
112  */
113  bool operator>(const time& t) const;
114 
115  /**
116  * Checks if this instance is greater or equal to t
117  */
118  bool operator>=(const time& t) const;
119 
120  /**
121  * Get the current hour 0-23
122  */
123  u8 hour() const;
124 
125  /**
126  * Set the current hour 0-23
127  */
128  u8 hour(u8 hour);
129 
130  /**
131  * Get the current minute 0-59
132  */
133  u8 minute() const;
134 
135  /**
136  * Set the current minute 0-59
137  */
138  u8 minute(u8 minute);
139 
140  /**
141  * Get the current second 0-61 (leap second possible)
142  */
143  u8 second() const;
144 
145  /**
146  * Set the current second 0-61 (leap second possible)
147  */
148  u8 second(u8 second);
149 
150  /**
151  * Get the current millisecond 0-999
152  */
153  u16 millisecond() const;
154 
155  /**
156  * Set the current millisecond 0-999
157  */
158  u16 millisecond(u16 millisecond);
159 
160  /**
161  * Set the time from string
162  * The format needs to be hh[:mm][:ss][.nnn]
163  * where less digits are possible and the delimiter may be any non digit
164  *
165  * Examples:
166  * h
167  * h:mm.s?n
168  * hh,mm!ss-n
169  *
170  * @return True on success
171  */
172  virtual bool set(const std::string& t);
173 
174  /**
175  * Set the time from given values
176  *
177  * @param hour Hour to set
178  * @param minute Minute to set
179  * @param second Second to set
180  * @param millisecond Milliseconds to set
181  * @return True on success
182  */
183  bool set(u8 hour, u8 minute, u8 second, u16 millisecond);
184 
185  /**
186  * Adds a certain amount of hours to the current time. Negative values subtract hours
187  *
188  * @param hours Number of hours to add
189  * @return Time containing sum
190  */
191  time add_hours(s32 hours) const;
192 
193  /**
194  * Adds a certain amount of minutes to the current time. Negative values subtract minutes
195  *
196  * @param minutes Number of minutes to add
197  * @return Time containing sum
198  */
199  time add_minutes(s32 minutes) const;
200 
201  /**
202  * Adds a certain amount of seconds to the current time. Negative values subtract seconds
203  *
204  * @param minutes Number of seconds to add
205  * @return Time containing sum
206  */
207  time add_seconds(s32 seconds) const;
208 
209  /**
210  * Adds a certain amount of milliseconds to the current time. Negative values subtract
211  * milliseconds
212  *
213  * @param minutes Number of milliseconds to add
214  * @return Time containing sum
215  */
216  time add_milliseconds(s32 milliseconds) const;
217 
218  /**
219  * Subtracts the given timespan from the current time
220  *
221  * @param dur A duration to subtract
222  * @return Time containing result
223  */
224  time subtract(const time_span& dur) const;
225 
226  /**
227  * Adds the given timespan to the current time
228  *
229  * @param dur A duration to add
230  * @return Time containing sum
231  */
232  time add(const time_span& dur) const;
233 
234  /**
235  * Calculates the timespan between the current time instance and given instance t
236  *
237  * @param t Time to calculate the duration between
238  * @return Timespan containing the duration
239  */
240  time_span time_between(const time& t) const;
241 
242  /**
243  * Converts the time to time_t (time.h representation)
244  *
245  * @return Time as time.h time_t
246  */
247  time_t mktime() const;
248 
249  /**
250  * Calculates the time difference using ::difftime.
251  * Calculates (this - t)
252  *
253  * @return Difference in seconds as double
254  */
255  double diff_time(const time& t) const;
256 
257  /**
258  * Indicates whether this time is considered valid
259  *
260  * @return True if time is valid
261  */
262  virtual bool is_valid() const;
263 
264  /**
265  * Indicates whether a given time is valid in terms of limits. Accounts for leap seconds
266  *
267  * @param hour Hour to check
268  * @param minute Minute to check
269  * @param second Second to check
270  * @param millisecond Milliseconds to check
271  * @return True if valid
272  */
273  static bool valid_time(u8 hour, u8 minute, u8 second, u16 millisecond);
274 
275  /**
276  * Converts the time to MySQL time representation. Note that MySQL time does not support
277  * milliseconds
278  *
279  * @return Time as MYSQL_TIME with no milliseconds
280  */
281  MYSQL_TIME mysql_time() const;
282 
283  /**
284  * Converts the time to a string with the format hh:mm:ss[.nnn]
285  *
286  * @param with_millisecond Indicates if milliseconds should be printed or not.
287  *
288  * @return String representing time with optional milliseconds
289  */
290  const std::string str_time(bool with_millisecond = false) const;
291 
292  /**
293  * Uses time.h to determine the current time in the local timezone.
294  *
295  * @return Time representing now
296  */
297  static time now();
298 
299  /**
300  * Uses time.h to determine the current time in UTC timezone.
301  *
302  * @return Time representing now in UTC
303  */
304  static time now_utc();
305 
306  protected:
311 };
312 
313 std::ostream& operator<<(std::ostream& os, const time& t);
314 }
315 
316 #endif
bool operator<(const time &t) const
Checks if this instance is lesser than t.
Definition: time.cpp:72
u16 millisecond() const
Get the current millisecond 0-999.
Definition: time.cpp:121
signed int s32
Definition: types.hpp:25
Class representing SQL time.
Definition: time.hpp:23
virtual bool is_valid() const
Indicates whether this time is considered valid.
Definition: time.cpp:293
u8 second() const
Get the current second 0-61 (leap second possible)
Definition: time.cpp:110
time & operator=(const time &t)
Assigns a value to this instance.
Definition: time.cpp:63
time subtract(const time_span &dur) const
Subtracts the given timespan from the current time.
Definition: time.cpp:200
u8 minute() const
Get the current minute 0-59.
Definition: time.cpp:99
int compare(const time &t) const
Compare this instance to given instance.
Definition: time.cpp:45
double diff_time(const time &t) const
Calculates the time difference using ::difftime.
Definition: time.cpp:286
bool operator<=(const time &t) const
Checks if this instance is lesser or equal to t.
Definition: time.cpp:74
time add_milliseconds(s32 milliseconds) const
Adds a certain amount of milliseconds to the current time.
Definition: time.cpp:181
std::ostream & operator<<(std::ostream &os, const date_time &ddt)
Definition: date_time.cpp:587
time(u8 hour=0, u8 minute=0, u8 second=0, u16 millisecond=0)
Construct time using given values.
Definition: time.cpp:26
const std::string str_time(bool with_millisecond=false) const
Converts the time to a string with the format hh:mm:ss[.nnn].
Definition: time.cpp:357
bool operator>(const time &t) const
Checks if this instance is greater than t.
Definition: time.cpp:76
time add_minutes(s32 minutes) const
Adds a certain amount of minutes to the current time.
Definition: time.cpp:143
u8 hour() const
Get the current hour 0-23.
Definition: time.cpp:88
bool operator!=(const time &t) const
Checks for unequality.
Definition: time.cpp:70
bool operator>=(const time &t) const
Checks if this instance is greater or equal to t.
Definition: time.cpp:78
unsigned char u8
Definition: types.hpp:20
unsigned short u16
Definition: types.hpp:21
time add_seconds(s32 seconds) const
Adds a certain amount of seconds to the current time.
Definition: time.cpp:162
time add(const time_span &dur) const
Adds the given timespan to the current time.
Definition: time.cpp:209
static time now()
Uses time.h to determine the current time in the local timezone.
Definition: time.cpp:302
time add_hours(s32 hours) const
Adds a certain amount of hours to the current time.
Definition: time.cpp:132
u16 m_millisecond
Definition: time.hpp:310
static time now_utc()
Uses time.h to determine the current time in UTC timezone.
Definition: time.cpp:314
double t
Definition: astarsearch.cpp:64
time_span time_between(const time &t) const
Calculates the timespan between the current time instance and given instance t.
Definition: time.cpp:221
virtual ~time()=default
Allow proper destruction in derived classes.
static bool valid_time(u8 hour, u8 minute, u8 second, u16 millisecond)
Indicates whether a given time is valid in terms of limits.
Definition: time.cpp:297
time_t mktime() const
Converts the time to time_t (time.h representation)
Definition: time.cpp:258
MYSQL_TIME mysql_time() const
Converts the time to MySQL time representation.
Definition: time.cpp:271
bool operator==(const time &t) const
Checks for equality.
Definition: time.cpp:68