Evo C++ Library v0.5.1
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
TimeOfDay Struct Reference

#include <evo/time.h>

Detailed Description

Structure holding a time of day.

Example
#include <evo/time.h>
#include <evo/io.h>
using namespace evo;
int main() {
TimeOfDay time(12, 30, 59);
time.add_minutes(15);
time.format(con().out) << NL;
return 0;
}

Output:

12:45:59

Public Member Functions

 TimeOfDay ()
 Constructor. More...
 
 TimeOfDay (int hour, int min=0, int sec=0, int msec=0)
 Constructor to initialize with time of day. More...
 
 TimeOfDay (const TimeOfDay &src)
 Copy constructor. More...
 
void add_milliseconds (int milliseconds)
 Add milliseconds to current time, subtract if negative. More...
 
void add_minutes (int minutes)
 Add minutes to current time, subtract if negative. More...
 
void add_seconds (int seconds)
 Add seconds to current time, subtract if negative. More...
 
int compare (const TimeOfDay &oth) const
 Compare to another time of day. More...
 
template<class T >
T & format (T &out, char delim=':', char msdelim=0) const
 Format time of day to String or Stream. More...
 
int get_days () const
 Get number of days from current hour value. More...
 
int get_days (int &result_hour) const
 Get number of days and hours from current hour value. More...
 
double get_fraction () const
 Get current time as a day fraction. More...
 
bool operator!= (const TimeOfDay &oth) const
 Compare for inequality with another time of day. More...
 
bool operator< (const TimeOfDay &oth) const
 Compare whether less than another time of day. More...
 
bool operator<= (const TimeOfDay &oth) const
 Compare whether less than or equal to another time of day. More...
 
TimeOfDayoperator= (const TimeOfDay &src)
 Assignment operator. More...
 
bool operator== (const TimeOfDay &oth) const
 Compare for equality with another time of day. More...
 
bool operator> (const TimeOfDay &oth) const
 Compare whether greater than another time of day. More...
 
bool operator>= (const TimeOfDay &oth) const
 Compare whether greater than or equal to another time of day. More...
 
bool parse (const SubString &str, bool allow_hour_overflow=false)
 Set time of day from parsing standard time string. More...
 
bool parse_std_impl (const char *&inp, const char *end, bool allow_hour_overflow=false)
 Set time of day from parsing standard time string (used internally). More...
 
void set ()
 Set all fields to 0, which is a valid time (midnight). More...
 
bool set (int new_hour, int new_minute=0, int new_second=0, int new_msecond=0)
 Set new time of day fields. More...
 
void set_fraction (double tm, int msec=0)
 Set time fields from day fraction. More...
 
void set_local ()
 Set to current local time of day. More...
 
void set_utc ()
 Set to current UTC time of day. More...
 
bool validate (bool allow_hour_overflow=false) const
 Validate current time of day. More...
 

Public Attributes

int hour
 Hour of day (0 - 23) More...
 
int minute
 Minute of hour (0 - 59) More...
 
int msecond
 Millisecond of second (0 - 999) More...
 
int second
 Second of minute (0 - 60), 60 is a special case for a leap second (rare) More...
 

Static Public Attributes

static const int HOUR_MAX = 23
 Maximum hour value. More...
 
static const int HOUR_MIN = 0
 Minimum hour value. More...
 
static const int HOURS_PER_DAY = 24
 Number of hours per day. More...
 
static const int MIN_PER_DAY = 1440
 Number of minutes per day. More...
 
static const int MIN_PER_HOUR = 60
 Number of minutes per hour. More...
 
static const int MINUTE_MAX = 59
 Maximum minute value. More...
 
static const int MINUTE_MIN = 0
 Minimum minute value. More...
 
static const long MSEC_PER_DAY = 86400000
 Number of milliseconds per day. More...
 
static const long MSEC_PER_HOUR = 3600000
 Number of milliseconds per hour. More...
 
static const int MSEC_PER_MIN = 60000
 Number of milliseconds per minute. More...
 
static const int MSEC_PER_SEC = 1000
 Number of milliseconds per second. More...
 
static const int MSECOND_MAX = 999
 Maximum millisecond value. More...
 
static const int MSECOND_MIN = 0
 Minimum millisecond value. More...
 
static const int SEC_PER_DAY = 86400
 Number of seconds per day. More...
 
static const int SEC_PER_HOUR = 3600
 Number of seconds per hour. More...
 
static const int SEC_PER_MIN = 60
 Number of seconds per minute. More...
 
static const int SECOND_MAX = 60
 Maximum second value. More...
 
static const int SECOND_MIN = 0
 Minimum second value. More...
 

Constructor & Destructor Documentation

◆ TimeOfDay() [1/3]

TimeOfDay ( )
inline

Constructor.

◆ TimeOfDay() [2/3]

TimeOfDay ( int  hour,
int  min = 0,
int  sec = 0,
int  msec = 0 
)
inline

Constructor to initialize with time of day.

Parameters
hourHour of day
minMinute of hour
secSecond of minute
msecMillisecond of second

◆ TimeOfDay() [3/3]

TimeOfDay ( const TimeOfDay src)
inline

Copy constructor.

Parameters
srcSource to copy

Member Function Documentation

◆ add_milliseconds()

void add_milliseconds ( int  milliseconds)
inline

Add milliseconds to current time, subtract if negative.

  • This adds the given milliseconds and adjusts the second, minute, and hour accordingly
  • Current millisecond, second, and minute are all kept in range, but this does not check if the resulting hour is within a valid range – see validate() to check that
Parameters
millisecondsNumber of milliseconds to add, negative to subtract

◆ add_minutes()

void add_minutes ( int  minutes)
inline

Add minutes to current time, subtract if negative.

  • This adds the given minutes and adjusts the hour accordingly
  • Current minute is kept in range, but this does not check if the resulting hour is within a valid range – see validate() to check that
Parameters
minutesNumber of minutes to add, negative to subtract

◆ add_seconds()

void add_seconds ( int  seconds)
inline

Add seconds to current time, subtract if negative.

  • This adds the given seconds and adjusts the minute and hour accordingly
  • Current second and minute are kept in range, but this does not check if the resulting hour is within a valid range – see validate() to check that
Parameters
secondsNumber of seconds to add, negative to subtract

◆ compare()

int compare ( const TimeOfDay oth) const
inline

Compare to another time of day.

Parameters
othOther time of day to compare to
Returns
Result (<0 if this is less, 0 if equal, >0 if this is greater)

◆ format()

T& format ( T &  out,
char  delim = ':',
char  msdelim = 0 
) const
inline

Format time of day to String or Stream.

Template Parameters
TString/Stream type, inferred from argument
Parameters
outOutput String/Stream to format to
delimTime delimiter, 0 for none, ':' for standard – other delimiters are not supported by parse()
msdelimDelimiter for milliseconds, 0 to not format milliseconds
Returns
Reference to out

◆ get_days() [1/2]

int get_days ( ) const
inline

Get number of days from current hour value.

  • This simply dvides the current hour by 24
Returns
Number of days, negative if current hour is under -23

◆ get_days() [2/2]

int get_days ( int &  result_hour) const
inline

Get number of days and hours from current hour value.

  • This also gets a result_hour value and results are normalized so hour isn't negative (when current input hour is negative)
    • Examples:
      • hour=47 returns 1 day, 23 hours
      • hour=-47 returns -2 days, 1 hour
  • This is useful when a TimeOfDay is combined with a Date – see DateTime
Parameters
result_hourStores result hour
Returns
Number of days, negative if current hour is under -23

◆ get_fraction()

double get_fraction ( ) const
inline

Get current time as a day fraction.

  • This turns the separate time fields into a single floating-point number, where the fraction is the time of day
  • Examples:
    • 0.0 for midnight
    • 0.25 for 6 AM
    • 0.5 for noon
    • 0.75 for 6 PM
  • Caution: Milliseconds aren't stored in the fraction (not enough precision) and are ignored here
  • See set_fraction()
Returns
Time as a day fraction, 0.0 for midnight, 0.5 for noon, etc

◆ operator!=()

bool operator!= ( const TimeOfDay oth) const
inline

Compare for inequality with another time of day.

Parameters
othOther time of day to compare to
Returns
Whether inequal

◆ operator<()

bool operator< ( const TimeOfDay oth) const
inline

Compare whether less than another time of day.

Parameters
othOther time of day to compare to
Returns
Whether less than

◆ operator<=()

bool operator<= ( const TimeOfDay oth) const
inline

Compare whether less than or equal to another time of day.

Parameters
othOther time of day to compare to
Returns
Whether less than or equal

◆ operator=()

TimeOfDay& operator= ( const TimeOfDay src)
inline

Assignment operator.

Parameters
srcSource to copy
Returns
This

◆ operator==()

bool operator== ( const TimeOfDay oth) const
inline

Compare for equality with another time of day.

Parameters
othOther time of day to compare to
Returns
Whether equal

◆ operator>()

bool operator> ( const TimeOfDay oth) const
inline

Compare whether greater than another time of day.

Parameters
othOther time of day to compare to
Returns
Whether greater than

◆ operator>=()

bool operator>= ( const TimeOfDay oth) const
inline

Compare whether greater than or equal to another time of day.

Parameters
othOther time of day to compare to
Returns
Whether greater than or equal

◆ parse()

bool parse ( const SubString str,
bool  allow_hour_overflow = false 
)
inline

Set time of day from parsing standard time string.

  • Supported time syntax is based on ISO 8601:
    • HH:MM:SS – full time of day with delimiters, but without milliseconds
    • HH:MM – time of day with delimiter and only hour and minute
    • HH – time of day with only hour value
    • HHMM – time of day with hour and minute, without delimimter
    • HHMMSS – full time of day without delimiters
    • HH:MM:SS.fff or HHMMSS.fff – full time of day with fractional second (milliseconds)
    • HH:MM.fff or HHMM.fff – full time of day with fractional minute (to give second and millisecond values)
    • HH.fff or HH.fff – full time of day with fractional hour (to give minute, second, and millisecond values)
    • Alternate delimiter supported for fractional or millisecond values: COMMA (,)
Parameters
strString to parse
allow_hour_overflowAllow hour higher than 23, but still must be 2 digits
Returns
Whether successful, false on bad input or unrecognized format

◆ parse_std_impl()

bool parse_std_impl ( const char *&  inp,
const char *  end,
bool  allow_hour_overflow = false 
)
inline

Set time of day from parsing standard time string (used internally).

  • Supported time syntax is based on ISO 8601:
    • HH:MM:SS – full time of day with delimiters, but without milliseconds
    • HH:MM – time of day with delimiter and only hour and minute
    • HH – time of day with only hour value
    • HHMM – time of day with hour and minute, without delimimter
    • HHMMSS – full time of day without delimiters
    • HH:MM:SS.fff or HHMMSS.fff – full time of day with fractional second (milliseconds)
    • HH:MM.fff or HHMM.fff – full time of day with fractional minute (to give second and millisecond values)
    • HH.fff or HH.fff – full time of day with fractional hour (to give minute, second, and millisecond values)
    • Alternate delimiter supported for fractional or millisecond values: COMMA (,)
Parameters
inpStart parsing pointer (must be <= end), set to stop position [in/out]
endEnd of string pointer, must not be NULL
allow_hour_overflowAllow hour higher than 23, but still must be 2 digits
Returns
Whether successful, false on bad input or unrecognized format

◆ set() [1/2]

void set ( )
inline

Set all fields to 0, which is a valid time (midnight).

◆ set() [2/2]

bool set ( int  new_hour,
int  new_minute = 0,
int  new_second = 0,
int  new_msecond = 0 
)
inline

Set new time of day fields.

  • This does validate the fields before setting them
Parameters
new_hourNew hour value
new_minuteNew minute value
new_secondNew second value
new_msecondNew millisecond value
Returns
Whether successful, false if one of the fields is invalid

◆ set_fraction()

void set_fraction ( double  tm,
int  msec = 0 
)
inline

Set time fields from day fraction.

  • Caution: Milliseconds aren't stored in the fraction (not enough precision) so msecond is set from msec
  • See get_fraction()
Parameters
tmTime as a day fraction, 0.0 for midnight, 0.5 for noon, etc
msecMilliseconds to set (0 - 999), defaults to 0

◆ set_local()

void set_local ( )
inline

Set to current local time of day.

◆ set_utc()

void set_utc ( )
inline

Set to current UTC time of day.

◆ validate()

bool validate ( bool  allow_hour_overflow = false) const
inline

Validate current time of day.

Parameters
allow_hour_overflowAllow hour higher than 23 – use get_days() to get number of days
Returns
Whether time of day is valid, false if any field is invalid (negative or out of range)

Member Data Documentation

◆ hour

int hour

Hour of day (0 - 23)

◆ HOUR_MAX

const int HOUR_MAX = 23
static

Maximum hour value.

◆ HOUR_MIN

const int HOUR_MIN = 0
static

Minimum hour value.

◆ HOURS_PER_DAY

const int HOURS_PER_DAY = 24
static

Number of hours per day.

◆ MIN_PER_DAY

const int MIN_PER_DAY = 1440
static

Number of minutes per day.

◆ MIN_PER_HOUR

const int MIN_PER_HOUR = 60
static

Number of minutes per hour.

◆ minute

int minute

Minute of hour (0 - 59)

◆ MINUTE_MAX

const int MINUTE_MAX = 59
static

Maximum minute value.

◆ MINUTE_MIN

const int MINUTE_MIN = 0
static

Minimum minute value.

◆ MSEC_PER_DAY

const long MSEC_PER_DAY = 86400000
static

Number of milliseconds per day.

◆ MSEC_PER_HOUR

const long MSEC_PER_HOUR = 3600000
static

Number of milliseconds per hour.

◆ MSEC_PER_MIN

const int MSEC_PER_MIN = 60000
static

Number of milliseconds per minute.

◆ MSEC_PER_SEC

const int MSEC_PER_SEC = 1000
static

Number of milliseconds per second.

◆ msecond

int msecond

Millisecond of second (0 - 999)

◆ MSECOND_MAX

const int MSECOND_MAX = 999
static

Maximum millisecond value.

◆ MSECOND_MIN

const int MSECOND_MIN = 0
static

Minimum millisecond value.

◆ SEC_PER_DAY

const int SEC_PER_DAY = 86400
static

Number of seconds per day.

◆ SEC_PER_HOUR

const int SEC_PER_HOUR = 3600
static

Number of seconds per hour.

◆ SEC_PER_MIN

const int SEC_PER_MIN = 60
static

Number of seconds per minute.

◆ second

int second

Second of minute (0 - 60), 60 is a special case for a leap second (rare)

◆ SECOND_MAX

const int SECOND_MAX = 60
static

Maximum second value.

◆ SECOND_MIN

const int SECOND_MIN = 0
static

Minimum second value.


The documentation for this struct was generated from the following file: