libUTL++
utl::Time Class Reference

Date/time. More...

#include <Time.h>

Inheritance diagram for utl::Time:

Public Member Functions

 Time (const String &fmt, const String &str)
 Constructor. More...
 
 Time (uint_t year, uint_t month=1, uint_t day=1, uint_t hour=0, uint_t min=0, double sec=0, bool isGMT=false)
 Constructor. More...
 
 Time (time_t t)
 Constructor. More...
 
virtual int compare (const Object &rhs) const
 Compare with another object. More...
 
virtual void copy (const Object &rhs)
 Copy another instance. More...
 
virtual void serialize (Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize to or from a stream. More...
 
Accessors
 operator time_t () const
 Return a time_t representation. More...
 
time_t get () const
 Get the time as a time_t value. More...
 
void getComponents (uint_t *year, uint_t *month, uint_t *day, uint_t *wday=nullptr, uint_t *hour=nullptr, uint_t *min=nullptr, double *sec=nullptr, bool *isDST=nullptr, bool isGMT=false) const
 Get the year, month, and day. More...
 
bool isNull () const
 Get the Null flag. More...
 
void setNull ()
 Set to null. More...
 
uint_t getYear () const
 Get the year. More...
 
uint_t getMonth () const
 Get the month. More...
 
uint_t getDay () const
 Get the day. More...
 
uint_t getDayOfWeek () const
 Get the day of week [0 = Sunday, . More...
 
uint_t getHour () const
 Get the hour [0,23]. More...
 
uint_t getMin () const
 Get the minute [0,59]. More...
 
uint_t getSec () const
 Get the second [0,59]. More...
 
uint_t getTimeOfDay () const
 Get the number of seconds since midnight. More...
 
uint_t maxDay () const
 Get the last day of this month. More...
 
void set (time_t dateTime)
 Set with a time_t value. More...
 
void set (uint_t year, uint_t month, uint_t day, uint_t hour=0, uint_t min=0, double sec=0, bool isGMT=false)
 Set a new date/time. More...
 
TimesetCurrent ()
 Set to the current time. More...
 
String Representation
virtual String toString () const
 Return a string representation of self. More...
 
String toString (const String &fmt) const
 Return a string representation of self. More...
 
String toGMTstring () const
 Return a string represenation of self (in GMT). More...
 
String toGMTstring (const String &fmt) const
 Return a string representation of self. More...
 
void set (const String &fmt, const String &str)
 Set with a string. More...
 
Rounding
TimeroundDown (uint_t unit)
 Round down to the given unit. More...
 
TimeroundDown (const Duration &dur)
 Round down to the given resolution. More...
 
TimeroundUp (uint_t unit)
 Round up to the given unit. More...
 
TimeroundUp (const Duration &dur)
 Round up to the given resolution. More...
 
Operators
Timeoperator= (time_t t)
 Assignment from time_t. More...
 
Time operator+ (int i) const
 Return self plus the given number of seconds. More...
 
Time operator+ (const Duration &rhs) const
 Return self plus the given duration. More...
 
Time operator- (const Duration &rhs) const
 Return self minus the given duration. More...
 
Time operator- (int i) const
 Return self minus the given number of seconds. More...
 
Duration operator- (const Time &rhs) const
 Return self minus the given time. More...
 
const Timeoperator+= (const Duration &rhs)
 Add the given duration to self. More...
 
const Timeoperator-= (const Duration &rhs)
 Subtract the given duration from self. More...
 
- Public Member Functions inherited from utl::Object
void clear ()
 Revert to initial state. More...
 
virtual void vclone (const Object &rhs)
 Make an exact copy of another instance. More...
 
virtual void steal (Object &rhs)
 "Steal" the internal representation from another instance. More...
 
virtual void dump (Stream &os, uint_t level=uint_t_max) const
 Dump a human-readable representation of self to the given output stream. More...
 
void dumpWithClassName (Stream &os, uint_t indent=4, uint_t level=uint_t_max) const
 Front-end for dump() that prints the object's class name. More...
 
virtual const ObjectgetKey () const
 Get the key for this object. More...
 
bool hasKey () const
 Determine whether or not the object has a key. More...
 
virtual const ObjectgetProxiedObject () const
 Get the proxied object (= self if none). More...
 
virtual ObjectgetProxiedObject ()
 Get the proxied object (= self if none). More...
 
virtual size_t hash (size_t size) const
 Get the hash code for the object. More...
 
bool _isA (const RunTimeClass *runTimeClass) const
 Determine whether self's class is a descendent of the given class. More...
 
 operator String () const
 Conversion to String. More...
 
size_t allocatedSize () const
 Get the total allocated size of this object. More...
 
virtual size_t innerAllocatedSize () const
 Get the "inner" allocated size. More...
 
virtual void addOwnedIt (const class FwdIt *it) const
 Notify self that it owns the given iterator. More...
 
virtual void removeOwnedIt (const class FwdIt *it) const
 Notify self that the given owned iterator has been destroyed. More...
 
bool operator< (const Object &rhs) const
 Less-than operator. More...
 
bool operator<= (const Object &rhs) const
 Less-than-or-equal-to operator. More...
 
bool operator> (const Object &rhs) const
 Greater-than operator. More...
 
bool operator>= (const Object &rhs) const
 Greater-than-or-equal-to operator. More...
 
bool operator== (const Object &rhs) const
 Equal-to operator. More...
 
bool operator!= (const Object &rhs) const
 Unequal-to operator. More...
 
void serializeIn (Stream &is, uint_t mode=ser_default)
 Serialize from an input stream. More...
 
void serializeOut (Stream &os, uint_t mode=ser_default) const
 Serialize to an output stream. More...
 
void serializeOutBoxed (Stream &os, uint_t mode=ser_default) const
 Serialize a boxed object to an output stream. More...
 

Static Public Member Functions

Current Time
static Time current ()
 Return the current time. More...
 
static Time today (uint_t hour=0, uint_t min=0, double sec=0)
 Return a Time object for the current date at the given time. More...
 
- Static Public Member Functions inherited from utl::Object
static ObjectserializeInNullable (Stream &is, uint_t mode=ser_default)
 Serialize a nullptr-able object from an input stream. More...
 
static void serializeOutNullable (const Object *object, Stream &os, uint_t mode=ser_default)
 Serialize a nullptr-able object to an output stream. More...
 
static void serializeNullable (Object *&object, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a nullptr-able object to or from a stream. More...
 
static ObjectserializeInBoxed (Stream &is, uint_t mode=ser_default)
 Serialize a boxed object from an input stream. More...
 
static void serializeBoxed (Object *&object, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a boxed object to or from a stream. More...
 

Static Public Attributes

static const char * abbrevWeekDayName [7]
 Abbreviated weekday names. More...
 
static const char * weekDayName [7]
 Weekday names. More...
 
static const char * abbrevMonthName [12]
 Abbreviated month names. More...
 
static const char * monthName [12]
 Month names. More...
 
static const uint_t monthDays [12]
 Number of days in each month. More...
 

Detailed Description

Date/time.

Getting a String Representation

A format string given to the toString() method contains special codes that are to be substituted with strings based on the represented time. The codes are always preceded by a $ character so the parser can recognize them. If you want to have a $ character in the format string without a format code, just put two in a row: $$.

The formatting codes for toString() are:

  • d : day of month [1,31]
  • dd : day of month (2 digits)
  • dst : " DST" if DST enabled
  • h : hour [0,23]
  • hh : hour [0,11]
  • Mn : month name
  • mn : abbreviated month name (e.g. "Jan")
  • m : month [1,12]
  • mm : month (2 digits)
  • n : minute [0,59]
  • nn : minute (2 digits)
  • P : "AM" or "PM"
  • p : "am" or "pm"
  • s : second [0,59]
  • ss : second (2 digits)
  • Wn : day of week (e.g. "Monday")
  • wn : abbreviated day of week (e.g. "Mon")
  • y : year (e.g. 05 => 2005)
  • yyyy : year (4 digits - e.g. "2005")

For example, suppose the represented time is June 15th 2000 5:43 pm DST. Here are some format strings and the corresponding results that toString() would provide.

  • "year = $yyyy, month = $Mn" => "year = 2000, month = June"
  • "$mn $d, $yyyy @ $hh:$mm" => "Jun 15, 2000 @ 17:43"
  • "$h:$mm $p $DST" => "5:43 pm DST"

Setting Time With a String

When setting the time with a string, you provide two strings to set(). The first string is the format string that tells the parser what to expect in the second string which contains the string representation of the time. The formatting codes are not preceded by a $ character as they are with toString() because the format string contains only formatting codes.

Here are the format strings recognized by set():

  • y : year (2 or 4 digits)
  • Mn : month name (e.g. "January")
  • mn : abbreviated month name (e.g. "Jan")
  • m : month [1,12]
  • d : day [1,31]
  • h : hour [0,23]
  • n : minute [0,59]
  • s : second [0,59]
  • p : am or pm (case-insensitive)
  • gmt : interpret as GMT
Author
Adam McKee

Definition at line 148 of file Time.h.

Constructor & Destructor Documentation

◆ Time() [1/3]

utl::Time::Time ( const String fmt,
const String str 
)
inline

Constructor.

Parameters
fmtformat string
strstring representation

Definition at line 158 of file Time.h.

◆ Time() [2/3]

utl::Time::Time ( uint_t  year,
uint_t  month = 1,
uint_t  day = 1,
uint_t  hour = 0,
uint_t  min = 0,
double  sec = 0,
bool  isGMT = false 
)
inline

Constructor.

Parameters
yearyear
month(optional : 1) month [1,12]
day(optional : 1) day [1,31]
hour(optional : 0) hour [0,23]
min(optional : 0) minute [0,59]
sec(optional : 0) second [0,59]
isGMT(optional : false) interpret as GMT ? (otherwise interpret as local time)

Definition at line 174 of file Time.h.

References utl::min().

◆ Time() [3/3]

utl::Time::Time ( time_t  t)
inline

Constructor.

Parameters
ttime_t value

Definition at line 189 of file Time.h.

References utl::compare(), utl::copy(), utl::ser_default, and utl::serialize().

Member Function Documentation

◆ compare()

virtual int utl::Time::compare ( const Object rhs) const
virtual

Compare with another object.

If no overridden version succeeds in doing the comparison, then an attempt will be made to re-start the comparison process using one or both of the objects' keys. Usually, an override of compare() should call the superclass's compare() if it doesn't know how to compare itself with the rhs object.

See also
getKey
Returns
< 0 if self < rhs, 0 if self = rhs, > 0 if self > rhs
Parameters
rhsobject to compare with

Reimplemented from utl::Object.

◆ copy()

virtual void utl::Time::copy ( const Object rhs)
virtual

Copy another instance.

When you override copy(), you should usually call the superclass's copy().

Parameters
rhsobject to copy

Reimplemented from utl::Object.

◆ serialize()

virtual void utl::Time::serialize ( Stream stream,
uint_t  io,
uint_t  mode = ser_default 
)
virtual

Serialize to or from a stream.

This is the only virtual method for serialization. You must override this in any class that has data to be serialized, and ensure that the superclass's serialize() gets called.

Parameters
streamstream to serialize from/to
iosee utl::io_t
modesee utl::serialize_t

Reimplemented from utl::Object.

◆ operator time_t()

utl::Time::operator time_t ( ) const
inline

Return a time_t representation.

Definition at line 203 of file Time.h.

◆ get()

time_t utl::Time::get ( ) const
inline

Get the time as a time_t value.

Definition at line 210 of file Time.h.

References utl::min().

◆ getComponents()

void utl::Time::getComponents ( uint_t year,
uint_t month,
uint_t day,
uint_t wday = nullptr,
uint_t hour = nullptr,
uint_t min = nullptr,
double *  sec = nullptr,
bool *  isDST = nullptr,
bool  isGMT = false 
) const

Get the year, month, and day.

◆ isNull()

bool utl::Time::isNull ( ) const
inline

Get the Null flag.

Definition at line 228 of file Time.h.

◆ setNull()

void utl::Time::setNull ( )
inline

Set to null.

Definition at line 235 of file Time.h.

◆ getYear()

uint_t utl::Time::getYear ( ) const
inline

Get the year.

Definition at line 242 of file Time.h.

◆ getMonth()

uint_t utl::Time::getMonth ( ) const
inline

Get the month.

Definition at line 250 of file Time.h.

◆ getDay()

uint_t utl::Time::getDay ( ) const
inline

Get the day.

Definition at line 258 of file Time.h.

◆ getDayOfWeek()

uint_t utl::Time::getDayOfWeek ( ) const
inline

Get the day of week [0 = Sunday, .

. ., 6 = Saturday].

Definition at line 266 of file Time.h.

◆ getHour()

uint_t utl::Time::getHour ( ) const
inline

Get the hour [0,23].

Definition at line 274 of file Time.h.

◆ getMin()

uint_t utl::Time::getMin ( ) const
inline

Get the minute [0,59].

Definition at line 282 of file Time.h.

◆ getSec()

uint_t utl::Time::getSec ( ) const
inline

Get the second [0,59].

Definition at line 290 of file Time.h.

◆ getTimeOfDay()

uint_t utl::Time::getTimeOfDay ( ) const
inline

Get the number of seconds since midnight.

Definition at line 298 of file Time.h.

◆ maxDay()

uint_t utl::Time::maxDay ( ) const

Get the last day of this month.

◆ set() [1/3]

void utl::Time::set ( time_t  dateTime)
inline

Set with a time_t value.

Definition at line 311 of file Time.h.

References utl::min().

◆ set() [2/3]

void utl::Time::set ( uint_t  year,
uint_t  month,
uint_t  day,
uint_t  hour = 0,
uint_t  min = 0,
double  sec = 0,
bool  isGMT = false 
)

Set a new date/time.

Parameters
yearyear
month(optional : 1) month [1,12]
day(optional : 1) day [1,31]
hour(optional : 0) hour [0,23]
min(optional : 0) minute [0,59]
sec(optional : 0) second [0,59]
isGMT(optional : false) interpret as GMT ? (interpret as local time if false)

◆ setCurrent()

Time& utl::Time::setCurrent ( )
inline

Set to the current time.

Definition at line 337 of file Time.h.

References utl::roundDown(), utl::roundUp(), and utl::toString().

Referenced by current().

◆ toString() [1/2]

virtual String utl::Time::toString ( ) const
virtual

Return a string representation of self.

Reimplemented from utl::Object.

◆ toString() [2/2]

String utl::Time::toString ( const String fmt) const

Return a string representation of self.

Returns
string representation of self
Parameters
fmtformat string

◆ toGMTstring() [1/2]

String utl::Time::toGMTstring ( ) const

Return a string represenation of self (in GMT).

◆ toGMTstring() [2/2]

String utl::Time::toGMTstring ( const String fmt) const

Return a string representation of self.

Returns
string representation of self
Parameters
fmtformat string

◆ set() [3/3]

void utl::Time::set ( const String fmt,
const String str 
)

Set with a string.

Parameters
fmtformat string
strstring representation

◆ roundDown() [1/2]

Time& utl::Time::roundDown ( uint_t  unit)

Round down to the given unit.

Parameters
unitunit (see utl::tm_unit_t)

◆ roundDown() [2/2]

Time& utl::Time::roundDown ( const Duration dur)

Round down to the given resolution.

Parameters
durrounding resolution

◆ roundUp() [1/2]

Time& utl::Time::roundUp ( uint_t  unit)

Round up to the given unit.

Parameters
unitunit (see utl::tm_unit_t)

◆ roundUp() [2/2]

Time& utl::Time::roundUp ( const Duration dur)

Round up to the given resolution.

Parameters
durrounding resolution

◆ operator=()

Time& utl::Time::operator= ( time_t  t)
inline

Assignment from time_t.

Definition at line 404 of file Time.h.

◆ operator+() [1/2]

Time utl::Time::operator+ ( int  i) const
inline

Return self plus the given number of seconds.

Definition at line 411 of file Time.h.

◆ operator+() [2/2]

Time utl::Time::operator+ ( const Duration rhs) const
inline

Return self plus the given duration.

Definition at line 417 of file Time.h.

References utl::Number< T >::get().

◆ operator-() [1/3]

Time utl::Time::operator- ( const Duration rhs) const
inline

Return self minus the given duration.

Definition at line 425 of file Time.h.

References utl::Number< T >::get().

◆ operator-() [2/3]

Time utl::Time::operator- ( int  i) const
inline

Return self minus the given number of seconds.

Definition at line 433 of file Time.h.

◆ operator-() [3/3]

Duration utl::Time::operator- ( const Time rhs) const

Return self minus the given time.

◆ operator+=()

const Time& utl::Time::operator+= ( const Duration rhs)
inline

Add the given duration to self.

Definition at line 442 of file Time.h.

References utl::Number< T >::get().

◆ operator-=()

const Time& utl::Time::operator-= ( const Duration rhs)
inline

Subtract the given duration from self.

Definition at line 449 of file Time.h.

References utl::Number< T >::get().

◆ current()

static Time utl::Time::current ( )
inlinestatic

Return the current time.

Definition at line 460 of file Time.h.

References setCurrent().

◆ today()

static Time utl::Time::today ( uint_t  hour = 0,
uint_t  min = 0,
double  sec = 0 
)
static

Return a Time object for the current date at the given time.

Parameters
hour(optional : 0) hour [0,23]
min(optional : 0) minute [0,59]
sec(optional : 0) second [0,59]

Member Data Documentation

◆ abbrevWeekDayName

const char* utl::Time::abbrevWeekDayName[7]
static

Abbreviated weekday names.

[0=Sunday,..,6=Saturday].

Definition at line 477 of file Time.h.

◆ weekDayName

const char* utl::Time::weekDayName[7]
static

Weekday names.

[0=Sunday,..,6=Saturday].

Definition at line 479 of file Time.h.

◆ abbrevMonthName

const char* utl::Time::abbrevMonthName[12]
static

Abbreviated month names.

[0=January,..,11=December].

Definition at line 481 of file Time.h.

◆ monthName

const char* utl::Time::monthName[12]
static

Month names.

[0=January,..,11=December].

Definition at line 483 of file Time.h.

◆ monthDays

const uint_t utl::Time::monthDays[12]
static

Number of days in each month.

[0=January,..,11=December].

Definition at line 485 of file Time.h.


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