utils_time.h
Go to the documentation of this file.
1
11#ifndef CCGL_UTILS_TIME_H
12#define CCGL_UTILS_TIME_H
13
14#include "basic.h"
15
16namespace ccgl {
21namespace utils_time {
25double TimeCounting();
26
31inline bool IsLeapYear(const int yr) { return !(yr % 4) && (yr % 100 || !(yr % 400)); }
32
39string ConvertToString(const time_t date, bool utc_time = true);
40
47string ConvertToString2(const time_t date, bool utc_time = true);
54string ConvertToString3(const time_t date, bool utc_time = true);
69time_t ConvertToTime(const string& str_date, string const& format, bool include_hour, bool utc_time = true);
70
79time_t ConvertYMDToTime(int& year, int& month, int& day, bool utc_time = true);
80
87int GetDateInfoFromTimet(time_t t, int* year, int* month, int* day, bool utc_time = true);
88
94void LocalTime(time_t date, struct tm* t);
95
101void UTCTime(time_t date, struct tm* t);
102
109void GetDateTime(time_t date, struct tm* t, bool utc_time = true);
110
115int GetYear(time_t date, bool utc_time = true);
116
121int GetMonth(time_t date, bool utc_time = true);
122
127int GetDay(time_t date, bool utc_time = true);
128
132int DayOfYear(time_t date, bool utc_time = true);
133
137int DayOfYear(int year, int month, int day);
138
142int JulianDay(time_t date, bool utc_time = true);
143
149int JulianDay(int year, int month, int day);
150
156struct DateTime {
157 int year;
158 int month;
159 int day;
162 int hour;
163 int minute;
164 int second;
167 vuint64_t filetime;
168
172 static DateTime LocalTime();
173
177 static DateTime UTCTime();
178
182 static DateTime FromDateTime(int iyear, int imonth, int iday, int ihour = 0,
183 int iminute = 0, int isecond = 0, int imillisecond = 0);
184
188 static DateTime FromFileTime(vuint64_t ifiletime);
189
190 DateTime();
191
193
195
196 DateTime Forward(int imilliseconds);
197
198 DateTime Backward(int imilliseconds);
199
200 bool operator==(const DateTime& value) const { return filetime == value.filetime; }
201 bool operator!=(const DateTime& value) const { return filetime != value.filetime; }
202 bool operator<(const DateTime& value) const { return filetime < value.filetime; }
203 bool operator<=(const DateTime& value) const { return filetime <= value.filetime; }
204 bool operator>(const DateTime& value) const { return filetime > value.filetime; }
205 bool operator>=(const DateTime& value) const { return filetime >= value.filetime; }
206};
207
208} /* namespace: utils_time */
209} /* namespace: ccgl */
210
211#endif /* CCGL_UTILS_TIME_H */
Basic definitions.
time_t ConvertYMDToTime(int &year, int &month, int &day, const bool utc_time)
Convert integer year, month, and day to date time.
Definition: utils_time.cpp:157
int GetDateInfoFromTimet(const time_t t, int *year, int *month, int *day, const bool utc_time)
Get date information from time_t variable.
Definition: utils_time.cpp:182
bool IsLeapYear(const int yr)
Check the given year is a leap year or not.
Definition: utils_time.h:31
void UTCTime(const time_t date, struct tm *t)
Get UTC:+00:00 time.
Definition: utils_time.cpp:205
int JulianDay(const time_t date, const bool utc_time)
Get the Julian day from time_t date.
Definition: utils_time.cpp:257
string ConvertToString(const time_t date, const bool utc_time)
Convert date time to string as the format of "YYYY-MM-DD".
Definition: utils_time.cpp:30
double TimeCounting()
Precisely and cross-platform time counting function.
Definition: utils_time.cpp:13
void GetDateTime(const time_t date, struct tm *t, const bool utc_time)
Get UTC:+00:00 time.
Definition: utils_time.cpp:213
string ConvertToString3(const time_t date, const bool utc_time)
format: 2022_11_17_092000
Definition: utils_time.cpp:76
int DayOfYear(const time_t date, const bool utc_time)
Get the day of one year, [1, 366].
Definition: utils_time.cpp:245
int GetMonth(const time_t date, const bool utc_time)
Get the month.
Definition: utils_time.cpp:229
int GetYear(const time_t date, const bool utc_time)
Get the year.
Definition: utils_time.cpp:221
string ConvertToString2(const time_t date, const bool utc_time)
Convert date time to string as the format of "YYYY-MM-DD HH".
Definition: utils_time.cpp:42
int GetDay(const time_t date, const bool utc_time)
Get the day.
Definition: utils_time.cpp:237
void LocalTime(time_t date, struct tm *t)
Get local time.
Definition: utils_time.cpp:197
time_t ConvertToTime(const string &str_date, string const &format, const bool include_hour, const bool utc_time)
Convert string to date time, string format could be %4d%2d%2d or d-d-d.
Definition: utils_time.cpp:113
Common Cross-platform Geographic Library (CCGL)
Definition: basic.cpp:17
A type representing the combination of date and time.
Definition: utils_time.h:156
static DateTime FromDateTime(int iyear, int imonth, int iday, int ihour=0, int iminute=0, int isecond=0, int imillisecond=0)
Create a date time value from each time element value.
Definition: utils_time.cpp:379
DateTime Backward(int imilliseconds)
Move backward by the delta in milliseconds.
Definition: utils_time.cpp:496
static DateTime UTCTime()
Get the current UTC time.
Definition: utils_time.cpp:360
int day_of_week
Day of the week since Sunday - [0, 6].
Definition: utils_time.h:160
int month
Month since January - [1, 12].
Definition: utils_time.h:158
int minute
Minutes after the hour - [0, 59].
Definition: utils_time.h:163
static DateTime FromFileTime(vuint64_t ifiletime)
Create a date time value from FILETIME.
Definition: utils_time.cpp:413
int day
Day of the month - [1, 31].
Definition: utils_time.h:159
int day_of_year
Day of the year - [0, 365].
Definition: utils_time.h:161
int second
Seconds after the minute - [0, 59].
Definition: utils_time.h:164
DateTime ToLocalTime()
Convert the UTC time to the local time.
Definition: utils_time.cpp:445
int hour
Hour of the day since midnight - [0, 23].
Definition: utils_time.h:162
int year
Year.
Definition: utils_time.h:157
DateTime Forward(int imilliseconds)
Move forward by the delta in milliseconds.
Definition: utils_time.cpp:488
DateTime()
Create an empty date time value.
Definition: utils_time.cpp:438
static DateTime LocalTime()
Get the current local time.
Definition: utils_time.cpp:341
DateTime ToUTCTime()
Convert the local time to the UTC time.
Definition: utils_time.cpp:467
vuint64_t filetime
The number of 100-nanosecond intervals since January 1, 1601 (UTC).
Definition: utils_time.h:167
int milliseconds
Milliseconds after the second - [0, 999].
Definition: utils_time.h:165
vuint64_t total_milliseconds
Total milliseconds of the time.
Definition: utils_time.h:166