Simple time class with milliseconds. More...

#include <time.hpp>

Public Types

enum  tz_t { local , utc }
 

Public Member Functions

 time ()
 
 time (time_t t)
 
 time (const time_info &ti)
 
 time (int year, int month, int day, int hour, int min, int sec, long millis=0)
 
 time (const time &x)=default
 
 time (time &&x)=default
 
timeoperator= (const time &x)=default
 
timeoperator= (time &&x)=default
 
 ~time ()=default
 
bool operator== (const time &x) const
 
bool operator!= (const time &x) const
 
bool operator<= (const time &x) const
 
bool operator< (const time &x) const
 
bool operator> (const time &x) const
 
bool operator>= (const time &x) const
 
time to_utc () const
 
void set (int year, int month, int day, int hour, int min, int sec, unsigned int millis)
 
void set (const char *timestr, const char *format="%Y-%m-%d %H:%M%S")
 
void set (time_t t, unsigned int millis)
 
void set (const date &d)
 
void set (const time_info &ti)
 
int year () const
 
int month () const
 
int day () const
 
int hour () const
 
int minute () const
 
int second () const
 
unsigned int milli_second () const
 
timeyear (int y)
 
timemonth (int m)
 
timeday (int d)
 
timehour (int h)
 
timeminute (int m)
 
timesecond (int s)
 
timemilli_second (int ms)
 
int day_of_week () const
 
int day_of_year () const
 
bool is_leap_year () const
 
bool is_daylight_saving () const
 
time_info get_time_info () const
 
struct tm get_tm () const
 
date to_date () const
 

Static Public Member Functions

static time now ()
 
static bool is_valid_time (int hour, int min, int sec, unsigned int millis)
 
static time parse (const std::string &tstr, const char *format)
 

Friends

OOS_UTILS_API std::ostream & operator<< (std::ostream &out, const time &x)
 

Detailed Description

Simple time class with milliseconds.

This class provides a simple interface for time representation. It is possible to create a time with millisecond precision.

Addition and subtraction of times is possible as well as parsing and formatting.

Member Enumeration Documentation

◆ tz_t

timezone enum

Enumerator
local 

use local timezone

utc 

use utc timezone

Constructor & Destructor Documentation

◆ time() [1/6]

matador::time::time ( )

Creates a new time with time now

◆ time() [2/6]

matador::time::time ( time_t  t)
explicit

Create a time from time_t

Parameters
ttime_t value to create time from

◆ time() [3/6]

matador::time::time ( const time_info &  ti)
explicit

Create a time from a timeval struct

Parameters
tvtimeval value to create time from

◆ time() [4/6]

matador::time::time ( int  year,
int  month,
int  day,
int  hour,
int  min,
int  sec,
long  millis = 0 
)

Create a time from discret values

Parameters
yearYear value of time
monthMonth value of time
dayDay value of time
hourHour part of time
minMinute part of time
secSecond part of time
millisMilliseconds of time (default is zero)

◆ time() [5/6]

matador::time::time ( const time x)
default

Create a time from another time

Parameters
xOther time object to be initialized from

◆ time() [6/6]

matador::time::time ( time &&  x)
default

Copy move from another time

Parameters
xOther time object to be move from

◆ ~time()

matador::time::~time ( )
default

Destroy time

Member Function Documentation

◆ day() [1/2]

int matador::time::day ( ) const

Return the day of the time.

Returns
The day part of the time.

◆ day() [2/2]

time & matador::time::day ( int  d)

Sets the day part of the time and returns the time object.

Parameters
dThe day part to set.
Returns
The time object.

◆ day_of_week()

int matador::time::day_of_week ( ) const

Returns the day of week number. By now the week starts with monday which is index 0 (zero).

Returns
The day of week number.

◆ day_of_year()

int matador::time::day_of_year ( ) const

Returns the day of the year number. First day of the year is number one (1).

Returns
The day of the year number.

◆ get_time_info()

time_info matador::time::get_time_info ( ) const

Returns the underlying timeval struct representing the time.

Returns
The underlying timeval struct

◆ get_tm()

struct tm matador::time::get_tm ( ) const

Returns the time as struct tm representing the time without milli seconds

Returns
The time as struct tm.

◆ hour() [1/2]

int matador::time::hour ( ) const

Return the hour of the time.

Returns
The hour part of the time.

◆ hour() [2/2]

time & matador::time::hour ( int  h)

Sets the hour part of the time and returns the time object.

Parameters
hThe hour part to set.
Returns
The time object.

◆ is_daylight_saving()

bool matador::time::is_daylight_saving ( ) const

Returns true if time is in daylight saving range which depends one the country.

Returns
True if time is in daylight saving range.

◆ is_leap_year()

bool matador::time::is_leap_year ( ) const

Returns true if times year is a leap year.

Returns
True If year of time is leap year.

◆ is_valid_time()

static bool matador::time::is_valid_time ( int  hour,
int  min,
int  sec,
unsigned int  millis 
)
static

Checks if given time parts are valid

0 <= hour < 24 0 <= minutes < 60 0 <= seconds < 60 0 <= milliseconds < 1000

Parameters
hourHour to check
minMinute to check
secSeconds to check
millisMilliseconds to check
Returns
true if time parts are valid

◆ milli_second() [1/2]

unsigned int matador::time::milli_second ( ) const

Return the milliseconds of the time.

Returns
The millisecond part of the time.

◆ milli_second() [2/2]

time & matador::time::milli_second ( int  ms)

Sets the millisecond part of the time and returns the time object.

Parameters
msThe millisecond part to set.
Returns
The time object.

◆ minute() [1/2]

int matador::time::minute ( ) const

Return the minute of the time.

Returns
The minute part of the time.

◆ minute() [2/2]

time & matador::time::minute ( int  m)

Sets the minute part of the time and returns the time object.

Parameters
mThe minute part to set.
Returns
The time object.

◆ month() [1/2]

int matador::time::month ( ) const

Return the month of the time.

Returns
The month part of the time.

◆ month() [2/2]

time & matador::time::month ( int  m)

Sets the month part of the time and returns the time object.

Parameters
mThe month part to set.
Returns
The time object.

◆ now()

static time matador::time::now ( )
static

Gets current time.

Returns
Returns current time.

◆ operator!=()

bool matador::time::operator!= ( const time x) const

Check if two times are not equal

Parameters
xTime object to compare with
Returns
True if times are not equal

◆ operator<()

bool matador::time::operator< ( const time x) const

Check if time is less than other time

Parameters
xTime object to compare with
Returns
True if time is less than other time

◆ operator<=()

bool matador::time::operator<= ( const time x) const

Check if time is less equal than other time

Parameters
xTime object to compare with
Returns
True if time is less equal than other time

◆ operator=() [1/2]

time & matador::time::operator= ( const time x)
default

Assign time from another time object

Parameters
xAssign time from this time object
Returns
Return reference of this

◆ operator=() [2/2]

time & matador::time::operator= ( time &&  x)
default

Assignment move from another time

Parameters
xAssign move time from this time object
Returns
Return reference of this

◆ operator==()

bool matador::time::operator== ( const time x) const

Check if two times are equal

Parameters
xTime object to compare with
Returns
True if times are equal

◆ operator>()

bool matador::time::operator> ( const time x) const

Check if time is greater than other time

Parameters
xTime object to compare with
Returns
True if time is greater than other time

◆ operator>=()

bool matador::time::operator>= ( const time x) const

Check if time is greater equal than other time

Parameters
xTime object to compare with
Returns
True if time is greater equal than other time

◆ parse()

static time matador::time::parse ( const std::string &  tstr,
const char *  format 
)
static

Parse a given time string with a valid format and return a corresponding time object.

Parameters
tstrTime string.
formatTime strings format.
Returns
A time object.

◆ second() [1/2]

int matador::time::second ( ) const

Return the seconds of the time.

Returns
The second part of the time.

◆ second() [2/2]

time & matador::time::second ( int  s)

Sets the second part of the time and returns the time object.

Parameters
sThe second part to set.
Returns
The time object.

◆ set() [1/5]

void matador::time::set ( const char *  timestr,
const char *  format = "%Y-%m-%d %H:%M%S" 
)

Sets the date from a given date/time string and a format string

Parameters
timestrThe date/time string to parse
formatThe format to use for parsing

◆ set() [2/5]

void matador::time::set ( const date d)

Sets the time from a date. The hour, minute, seconds and millisecond part is set to zero

Parameters
dThe date to set.

◆ set() [3/5]

void matador::time::set ( const time_info &  ti)

Sets the time from a given timeval object.

Parameters
tvTimeval object to set the time from.

◆ set() [4/5]

void matador::time::set ( int  year,
int  month,
int  day,
int  hour,
int  min,
int  sec,
unsigned int  millis 
)

Sets time by its parts.

Parameters
yearThe year part of the time.
monthThe month part of the time.
dayThe day part of the time.
hourThe hour part of the time.
minThe minute part of the time.
secThe seconds part of the time.
millisThe milliseconds part of the time.

◆ set() [5/5]

void matador::time::set ( time_t  t,
unsigned int  millis 
)

Sets the time from a given time_t and milliseconds

Parameters
tThe time_t to set.
millisThe milliseconds to set.

◆ to_date()

date matador::time::to_date ( ) const

Converts and returns the date part of the time to a date objects

Returns
The date part of the time as date object.

◆ year() [1/2]

int matador::time::year ( ) const

Return the year of the time.

Returns
The year part of the time.

◆ year() [2/2]

time & matador::time::year ( int  y)

Sets the year part of the time and returns the time object.

Parameters
yThe year part to set.
Returns
The time object.

Friends And Related Symbol Documentation

◆ operator<<

OOS_UTILS_API std::ostream & operator<< ( std::ostream &  out,
const time x 
)
friend

Writes the time to an std::ostream in ISO8601 format

Parameters
outThe stream to write to.
xThe time object to be printed
Returns
Reference to the passed ostream object.

The documentation for this class was generated from the following file:
  • matador/utils/time.hpp