21 #define MS_PER_SEC 1000 22 #define MS_PER_MIN (MS_PER_SEC * 60) 23 #define MS_PER_HOUR (MS_PER_MIN * 60) 24 #define MS_PER_DAY (MS_PER_HOUR * 24) 38 set(ts.tm_hour, ts.tm_min, ts.tm_sec, 0);
124 if (millisecond > 999)
135 if (hours == 0)
return tmp;
138 hours = (hours +
hour() + 24) % 24;
146 if (minutes == 0)
return tmp;
148 s32 hours = minutes / 60;
149 minutes = (minutes % 60) +
minute();
153 else if (minutes < 0)
156 if (hours != 0) tmp = tmp.
add_hours(hours);
158 tmp.
minute((minutes + 60) % 60);
165 if (seconds == 0)
return tmp;
167 s32 minutes = seconds / 60;
168 seconds = (seconds % 60) +
second();
172 else if (seconds < 0)
177 tmp.
second((seconds + 60) % 60);
184 if (milliseconds == 0)
return tmp;
186 s32 seconds = milliseconds / 1000;
187 milliseconds = (milliseconds % 1000) +
millisecond();
189 if (milliseconds > 999)
191 else if (milliseconds < 0)
223 if (t == *
this)
return time_span(0, 0, 0, 0, 0);
244 total_ms = ms - t_ms;
255 return time_span(0, hours, minutes, seconds, static_cast<u32>(total_ms),
false);
261 time_struct.tm_year = 1900;
262 time_struct.tm_mon = 0;
263 time_struct.tm_mday = 1;
264 time_struct.tm_hour =
hour();
265 time_struct.tm_min =
minute();
266 time_struct.tm_sec =
second();
268 return ::mktime(&time_struct);
282 t.time_type = MYSQL_TIMESTAMP_TIME;
287 time_t time_val =
mktime();
288 time_t t_time_val = t.
mktime();
290 return ::difftime(time_val, t_time_val);
299 return hour < 24 && minute < 60 && second <= 61 && millisecond < 1000;
303 using namespace std::chrono;
304 auto now = system_clock::now();
306 time_t local_time = system_clock::to_time_t(
now);
310 auto millis = duration_cast<milliseconds>(
now.time_since_epoch()).count() % 1000;
311 return mariadb::time(ts).add_milliseconds(static_cast<s32>(millis));
315 using namespace std::chrono;
316 auto now = system_clock::now();
318 time_t utc_time = system_clock::to_time_t(
now);
322 auto millis = duration_cast<milliseconds>(
now.time_since_epoch()).count() % 1000;
323 return mariadb::time(ts).add_milliseconds(static_cast<s32>(millis));
327 std::stringstream stream(t);
330 u16 s_h = 0, s_m = 0, s_s = 0, s_ms = 0;
334 if (stream >> s_h && s_h < 24) {
335 h =
static_cast<u8>(s_h);
336 if (stream.eof())
return set(h, 0, 0, 0);
339 if (stream >> delim && stream >> s_m && s_m < 60) {
340 m =
static_cast<u8>(s_m);
341 if (stream.eof())
return set(h, m, 0, 0);
344 if (stream >> delim && stream >> s_s && s_s < 62) {
345 s =
static_cast<u8>(s_s);
346 if (stream.eof())
return set(h, m, s, 0);
349 if (stream >> delim && stream >> s_ms)
return set(
h, m,
s, s_ms);
354 throw std::invalid_argument(
"invalid time format");
358 std::stringstream stream;
366 stream << std::setw(2) << +
hour() <<
":" << std::setw(2) << +
minute() <<
":" << std::setw(2)
369 if (with_millisecond) stream <<
"." << std::setw(3) <<
millisecond();
bool operator<(const time &t) const
Checks if this instance is lesser than t.
u16 millisecond() const
Get the current millisecond 0-999.
Class representing SQL time.
virtual bool is_valid() const
Indicates whether this time is considered valid.
u8 second() const
Get the current second 0-61 (leap second possible)
time & operator=(const time &t)
Assigns a value to this instance.
time subtract(const time_span &dur) const
Subtracts the given timespan from the current time.
u8 minute() const
Get the current minute 0-59.
u8 seconds() const
Get number of seconds.
int compare(const time &t) const
Compare this instance to given instance.
double diff_time(const time &t) const
Calculates the time difference using ::difftime.
bool operator<=(const time &t) const
Checks if this instance is lesser or equal to t.
time add_milliseconds(s32 milliseconds) const
Adds a certain amount of milliseconds to the current time.
std::ostream & operator<<(std::ostream &os, const date_time &ddt)
time(u8 hour=0, u8 minute=0, u8 second=0, u16 millisecond=0)
Construct time using given values.
u16 milliseconds() const
Get number of milliseconds.
const std::string str_time(bool with_millisecond=false) const
Converts the time to a string with the format hh:mm:ss[.nnn].
bool operator>(const time &t) const
Checks if this instance is greater than t.
time add_minutes(s32 minutes) const
Adds a certain amount of minutes to the current time.
u8 hour() const
Get the current hour 0-23.
bool operator!=(const time &t) const
Checks for unequality.
bool operator>=(const time &t) const
Checks if this instance is greater or equal to t.
int gmtime_safe(struct tm *_tm, const time_t *_time)
time add_seconds(s32 seconds) const
Adds a certain amount of seconds to the current time.
time add(const time_span &dur) const
Adds the given timespan to the current time.
static map_location::DIRECTION s
static time now()
Uses time.h to determine the current time in the local timezone.
time add_hours(s32 hours) const
Adds a certain amount of hours to the current time.
static time now_utc()
Uses time.h to determine the current time in UTC timezone.
bool negative() const
Indicates whether this time_span is negative.
time_span time_between(const time &t) const
Calculates the timespan between the current time instance and given instance t.
u8 minutes() const
Get number of hours.
virtual bool set(const std::string &t)
Set the time from string The format needs to be hh[:mm][:ss][.nnn] where less digits are possible and...
#define MARIADB_ERROR_THROW_TIME(_hour, _minute, _second, _millisecond)
static bool valid_time(u8 hour, u8 minute, u8 second, u16 millisecond)
Indicates whether a given time is valid in terms of limits.
time_t mktime() const
Converts the time to time_t (time.h representation)
u8 hours() const
Get number of hours.
int localtime_safe(struct tm *_tm, const time_t *_time)
MYSQL_TIME mysql_time() const
Converts the time to MySQL time representation.
bool operator==(const time &t) const
Checks for equality.