utils_time.h
Go to the documentation of this file.
1/*!
2 * \file utils_time.h
3 * \brief Time and datetime related functions in CCGL.
4 *
5 * \remarks
6 * - 1. 2018-05-01 - lj - Make part of CCGL.
7 * - 2. 2018-05-23 - lj - Add DateTime struct which combines date and time.
8 *
9 * \author Liangjun Zhu, zlj(at)lreis.ac.cn
10 */
11#ifndef CCGL_UTILS_TIME_H
12#define CCGL_UTILS_TIME_H
13
14#include "basic.h"
15
16namespace ccgl {
17/*!
18 * \namespace ccgl::utils_time
19 * \brief Time related functions
20 */
21namespace utils_time {
22/*!
23 * \brief Precisely and cross-platform time counting function.
24 */
25double TimeCounting();
26
27/*!
28 * \brief Check the given year is a leap year or not.
29 * divisible by 4, not if divisible by 100, but true if divisible by 400
30 */
31inline bool IsLeapYear(const int yr) { return !(yr % 4) && (yr % 100 || !(yr % 400)); }
32
33/*!
34 * \brief Convert date time to string as the format of "YYYY-MM-DD"
35 * \param[in] date \a time_t data type
36 * \param[in] utc_time By default, the input date is under UTC+00:00 timezone.
37 * \return Date time \a string
38 */
39string ConvertToString(const time_t date, bool utc_time = true);
40
41/*!
42 * \brief Convert date time to string as the format of "YYYY-MM-DD HH"
43 * \param[in] date \a time_t data type
44 * \param[in] utc_time By default, the input date is under UTC+00:00 timezone.
45 * \return Date time \a string
46 */
47string ConvertToString2(const time_t date, bool utc_time = true);
48/*!
49 * \brief Convert date time to string as the format of "YYYY_MM_DD_HHMMSS"
50 * \param[in] date \a time_t data type
51 * \param[in] utc_time By default, the input date is under UTC+00:00 timezone.
52 * \return Date time \a string
53 */
54string ConvertToString3(const time_t date, bool utc_time = true);
55/*!
56 * \brief Convert string to date time, string format could be %4d%2d%2d or %d-%d-%d
57 *
58 * Example:
59 * - 1. str_date => 20000323, format=> %4d%2d%2d
60 * - 2. str_date => 2000-03-23, format => %d-%d-%d
61 * - 3. str_date => 2000-03-23 18:01:30, => %d-%d-%d %d:%d:%d or %4d-%2d-%2d %2d:%2d:%2d
62 *
63 * \param[in] str_date \a string date
64 * \param[in] format \a string format
65 * \param[in] include_hour \a bool Include Hour?
66 * \param[in] utc_time By default, the input date is under UTC+00:00 timezone.
67 * \return Date time \a time_t
68 */
69time_t ConvertToTime(const string& str_date, string const& format, bool include_hour, bool utc_time = true);
70
71/*!
72 * \brief Convert integer year, month, and day to date time
73 * \param[in] year year number from 1970
74 * \param[in] month month range from 1 to 12
75 * \param[in] day day range from 1 to 31
76 * \param[in] utc_time By default, the input date is under UTC+00:00 timezone.
77 * \return Date time \a time_t
78 */
79time_t ConvertYMDToTime(int& year, int& month, int& day, bool utc_time = true);
80
81/*!
82 * \brief Get date information from \a time_t variable
83 * \param[in] t \a time_t date
84 * \param[out] year, month, day \a int value
85 * \param[in] utc_time By default, the input date is under UTC+00:00 timezone.
86 */
87int GetDateInfoFromTimet(time_t t, int* year, int* month, int* day, bool utc_time = true);
88
89/*!
90 * \brief Get local time
91 * \param[in] date \a time_t date
92 * \param[out] t \a tm struct date
93 */
94void LocalTime(time_t date, struct tm* t);
95
96/*!
97 * \brief Get UTC:+00:00 time
98 * \param[in] date \a time_t date
99 * \param[out] t \a tm struct date
100 */
101void UTCTime(time_t date, struct tm* t);
102
103/*!
104 * \brief Get UTC:+00:00 time
105 * \param[in] date \a time_t date
106 * \param[out] t \a tm struct date
107 * \param[in] utc_time By default, the input date is under UTC+00:00 timezone.
108 */
109void GetDateTime(time_t date, struct tm* t, bool utc_time = true);
110
111/*!
112 * \brief Get the year
113 * \return int year from 1970
114 */
115int GetYear(time_t date, bool utc_time = true);
116
117/*!
118 * \brief Get the month
119 * \return int month, [1, 12]
120 */
121int GetMonth(time_t date, bool utc_time = true);
122
123/*!
124 * \brief Get the day
125 * \return int day, [1, 31]
126 */
127int GetDay(time_t date, bool utc_time = true);
128
129/*!
130 * \brief Get the day of one year, [1, 366]
131 */
132int DayOfYear(time_t date, bool utc_time = true);
133
134/*!
135* \brief Get the day of one year, [1, 366]
136*/
137int DayOfYear(int year, int month, int day);
138
139/*!
140* \brief Get the Julian day from time_t date
141*/
142int JulianDay(time_t date, bool utc_time = true);
143
144/*!
145 * \brief Get the Julian day of one day from year, month, and day.
146 * Algorithm adopted from boost::date_time::gregorian_calendar_base::day_number.
147 * \return int Julian day
148 */
149int JulianDay(int year, int month, int day);
150
151/*!
152 * \struct DateTime
153 * \brief A type representing the combination of date and time.
154 * Refers to the DateTime struct implemented in Vlpp by vczh.
155 */
156struct DateTime {
157 int year; ///< Year
158 int month; ///< Month since January - [1, 12]
159 int day; ///< Day of the month - [1, 31]
160 int day_of_week; ///< Day of the week since Sunday - [0, 6]
161 int day_of_year; ///< Day of the year - [0, 365]
162 int hour; ///< Hour of the day since midnight - [0, 23]
163 int minute; ///< Minutes after the hour - [0, 59]
164 int second; ///< Seconds after the minute - [0, 59]
165 int milliseconds; ///< Milliseconds after the second - [0, 999]
166 vuint64_t total_milliseconds; ///< Total milliseconds of the time
167 vuint64_t filetime; ///< The number of 100-nanosecond intervals since January 1, 1601 (UTC).
168
169 /*!
170 * \brief Get the current local time.
171 */
173
174 /*!
175 * \brief Get the current UTC time.
176 */
178
179 /*!
180 * \brief Create a date time value from each time element value.
181 */
182 static DateTime FromDateTime(int iyear, int imonth, int iday, int ihour = 0,
183 int iminute = 0, int isecond = 0, int imillisecond = 0);
184
185 /*!
186 * \brief Create a date time value from FILETIME.
187 */
188 static DateTime FromFileTime(vuint64_t ifiletime);
189
190 DateTime(); ///< Create an empty date time value.
191
192 DateTime ToLocalTime(); ///< Convert the UTC time to the local time.
193
194 DateTime ToUTCTime(); ///< Convert the local time to the UTC time.
195
196 DateTime Forward(int imilliseconds); ///< Move forward by the delta in milliseconds.
197
198 DateTime Backward(int imilliseconds); ///< Move backward by the delta in milliseconds.
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, bool utc_time=true)
Convert integer year, month, and day to date time.
int GetDateInfoFromTimet(time_t t, int *year, int *month, int *day, bool utc_time=true)
Get date information from time_t variable.
bool IsLeapYear(const int yr)
Check the given year is a leap year or not.
Definition: utils_time.h:31
void UTCTime(time_t date, struct tm *t)
Get UTC:+00:00 time.
int JulianDay(time_t date, bool utc_time=true)
Get the Julian day from time_t date.
string ConvertToString(const time_t date, bool utc_time=true)
Convert date time to string as the format of "YYYY-MM-DD".
double TimeCounting()
Precisely and cross-platform time counting function.
void GetDateTime(time_t date, struct tm *t, bool utc_time=true)
Get UTC:+00:00 time.
time_t ConvertToTime(const string &str_date, string const &format, bool include_hour, bool utc_time=true)
Convert string to date time, string format could be %4d%2d%2d or d-d-d.
int DayOfYear(time_t date, bool utc_time=true)
Get the day of one year, [1, 366].
int GetMonth(time_t date, bool utc_time=true)
Get the month.
string ConvertToString3(const time_t date, bool utc_time=true)
Convert date time to string as the format of "YYYY_MM_DD_HHMMSS".
int GetYear(time_t date, bool utc_time=true)
Get the year.
string ConvertToString2(const time_t date, bool utc_time=true)
Convert date time to string as the format of "YYYY-MM-DD HH".
int GetDay(time_t date, bool utc_time=true)
Get the day.
void LocalTime(time_t date, struct tm *t)
Get local time.
Common Cross-platform Geographic Library (CCGL)
A type representing the combination of date and time.
Definition: utils_time.h:156
DateTime Backward(int imilliseconds)
Move backward by the delta in milliseconds.
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 UTCTime()
Get the current UTC time.
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.
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.
int hour
Hour of the day since midnight - [0, 23].
Definition: utils_time.h:162
int year
Year.
Definition: utils_time.h:157
static DateTime FromFileTime(vuint64_t ifiletime)
Create a date time value from FILETIME.
static DateTime LocalTime()
Get the current local time.
DateTime Forward(int imilliseconds)
Move forward by the delta in milliseconds.
DateTime()
Create an empty date time value.
DateTime ToUTCTime()
Convert the local time to the UTC time.
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