ccgl::utils_time Namespace Reference

Time related functions. More...

Classes

struct  DateTime
 A type representing the combination of date and time. More...
 

Functions

double TimeCounting ()
 Precisely and cross-platform time counting function.
 
bool IsLeapYear (const int yr)
 Check the given year is a leap year or not. More...
 
string ConvertToString (const time_t date, bool utc_time=true)
 Convert date time to string as the format of "YYYY-MM-DD". More...
 
string ConvertToString2 (const time_t date, bool utc_time=true)
 Convert date time to string as the format of "YYYY-MM-DD HH". More...
 
string ConvertToString3 (const time_t date, bool utc_time=true)
 Convert date time to string as the format of "YYYY_MM_DD_HHMMSS". More...
 
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. More...
 
time_t ConvertYMDToTime (int &year, int &month, int &day, bool utc_time=true)
 Convert integer year, month, and day to date time. More...
 
int GetDateInfoFromTimet (time_t t, int *year, int *month, int *day, bool utc_time=true)
 Get date information from time_t variable. More...
 
void LocalTime (time_t date, struct tm *t)
 Get local time. More...
 
void UTCTime (time_t date, struct tm *t)
 Get UTC:+00:00 time. More...
 
void GetDateTime (time_t date, struct tm *t, bool utc_time=true)
 Get UTC:+00:00 time. More...
 
int GetYear (time_t date, bool utc_time=true)
 Get the year. More...
 
int GetMonth (time_t date, bool utc_time=true)
 Get the month. More...
 
int GetDay (time_t date, bool utc_time=true)
 Get the day. More...
 
int DayOfYear (time_t date, bool utc_time=true)
 Get the day of one year, [1, 366].
 
int DayOfYear (int year, int month, int day)
 Get the day of one year, [1, 366].
 
int JulianDay (time_t date, bool utc_time=true)
 Get the Julian day from time_t date.
 
int JulianDay (int year, int month, int day)
 Get the Julian day of one day from year, month, and day. More...
 

Detailed Description

Time related functions.

Function Documentation

◆ ConvertToString()

string ccgl::utils_time::ConvertToString ( const time_t  date,
bool  utc_time = true 
)

Convert date time to string as the format of "YYYY-MM-DD".

Parameters
[in]datetime_t data type
[in]utc_timeBy default, the input date is under UTC+00:00 timezone.
Returns
Date time string

◆ ConvertToString2()

string ccgl::utils_time::ConvertToString2 ( const time_t  date,
bool  utc_time = true 
)

Convert date time to string as the format of "YYYY-MM-DD HH".

Parameters
[in]datetime_t data type
[in]utc_timeBy default, the input date is under UTC+00:00 timezone.
Returns
Date time string

◆ ConvertToString3()

string ccgl::utils_time::ConvertToString3 ( const time_t  date,
bool  utc_time = true 
)

Convert date time to string as the format of "YYYY_MM_DD_HHMMSS".

Parameters
[in]datetime_t data type
[in]utc_timeBy default, the input date is under UTC+00:00 timezone.
Returns
Date time string

◆ ConvertToTime()

time_t ccgl::utils_time::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.

Example:

  • 1. str_date => 20000323, format=> %4d%2d%2d
  • 2. str_date => 2000-03-23, format => d-d-d
  • 3. str_date => 2000-03-23 18:01:30, => d-d-d d:d:d or %4d-%2d-%2d %2d:%2d:%2d
Parameters
[in]str_datestring date
[in]formatstring format
[in]include_hourbool Include Hour?
[in]utc_timeBy default, the input date is under UTC+00:00 timezone.
Returns
Date time time_t

◆ ConvertYMDToTime()

time_t ccgl::utils_time::ConvertYMDToTime ( int &  year,
int &  month,
int &  day,
bool  utc_time = true 
)

Convert integer year, month, and day to date time.

Parameters
[in]yearyear number from 1970
[in]monthmonth range from 1 to 12
[in]dayday range from 1 to 31
[in]utc_timeBy default, the input date is under UTC+00:00 timezone.
Returns
Date time time_t

◆ GetDateInfoFromTimet()

int ccgl::utils_time::GetDateInfoFromTimet ( time_t  t,
int *  year,
int *  month,
int *  day,
bool  utc_time = true 
)

Get date information from time_t variable.

Parameters
[in]ttime_t date
[out]year,month,dayint value
[in]utc_timeBy default, the input date is under UTC+00:00 timezone.

◆ GetDateTime()

void ccgl::utils_time::GetDateTime ( time_t  date,
struct tm *  t,
bool  utc_time = true 
)

Get UTC:+00:00 time.

Parameters
[in]datetime_t date
[out]ttm struct date
[in]utc_timeBy default, the input date is under UTC+00:00 timezone.

◆ GetDay()

int ccgl::utils_time::GetDay ( time_t  date,
bool  utc_time = true 
)

Get the day.

Returns
int day, [1, 31]

◆ GetMonth()

int ccgl::utils_time::GetMonth ( time_t  date,
bool  utc_time = true 
)

Get the month.

Returns
int month, [1, 12]

◆ GetYear()

int ccgl::utils_time::GetYear ( time_t  date,
bool  utc_time = true 
)

Get the year.

Returns
int year from 1970

◆ IsLeapYear()

bool ccgl::utils_time::IsLeapYear ( const int  yr)

Check the given year is a leap year or not.

divisible by 4, not if divisible by 100, but true if divisible by 400

◆ JulianDay()

int ccgl::utils_time::JulianDay ( int  year,
int  month,
int  day 
)

Get the Julian day of one day from year, month, and day.

Algorithm adopted from boost::date_time::gregorian_calendar_base::day_number.

Returns
int Julian day

◆ LocalTime()

void ccgl::utils_time::LocalTime ( time_t  date,
struct tm *  t 
)

Get local time.

Parameters
[in]datetime_t date
[out]ttm struct date

◆ UTCTime()

void ccgl::utils_time::UTCTime ( time_t  date,
struct tm *  t 
)

Get UTC:+00:00 time.

Parameters
[in]datetime_t date
[out]ttm struct date