C12Adapter Opensource C++ Interface
MTimeSpan Class Reference

Time span represents the difference between two times, a duration. More...

Inheritance diagram for MTimeSpan:

Public Member Functions

 MTimeSpan ()
 Default constructor that initializes the object to no span, no duration.
 
 MTimeSpan (int secs, int mins=0, int hours=0, int days=0)
 Constructor, which explicitly sets the time span data, including days. More...
 
 MTimeSpan (const MTime &t1, const MTime &t2)
 Constructor, which initializes the object to a difference between the two time stamps. More...
 
 MTimeSpan (const MTimeSpan &other)
 Copy constructor.
 
virtual ~MTimeSpan ()
 Object destructor.
 
bool IsNull () const
 Whether the object represents no duration.
 
void SetToNull ()
 Set the span to null.
 
MStdString AsFormattedString (MConstChars formatString) const
 AsFormattedString the time span object as string. More...
 
void Set (int secs, int mins=0, int hours=0, int days=0)
 Set all parameters for the time span. More...
 
int GetDays () const
 Get days fraction of the time span, total 24 hour portions in the duration. More...
 
int GetHours () const
 Get hours part of the time span, -23 to 23. More...
 
int GetMinutes () const
 Get minutes fraction of the time span. More...
 
int GetSeconds () const
 Get seconds fraction of the time span. More...
 
int ToHours () const
 Convert time span to hours. More...
 
int ToMinutes () const
 Convert time span to minutes. More...
 
int ToSeconds () const
 Convert time span to seconds. More...
 
int Compare (const MTimeSpan &other) const
 Compare two time spans, ternary comparison service. More...
 
int CompareWithVariant (const MVariant &other) const
 Same as Compare, but uses a variant as the other type. More...
 
MVariant Add (const MVariant &) const
 Add an object to time span object and return the result, reflection-enabling service. More...
 
MVariant Subtract (const MVariant &) const
 Subtract an object to time span object and return the result, reflection-enabling service. More...
 
MVariant Multiply (int numberOfTimes) const
 Multiply a time span object with the given number of times, return the result. More...
 
MVariant Divide (int numberOfTimes) const
 Divide a time span object with the given number of times, return the result. More...
 
MVariant NewClone () const
 Reflection-enabling copy constructor. More...
 
void DoSet1 (int seconds)
 Set the new time span value from the given number if seconds. More...
 
void DoSet3 (int seconds, int minutes, int hours)
 Set the new time span value from the given time. More...
 
virtual unsigned GetEmbeddedSizeof () const
 Time span is an embedded object type, therefore return its size in bytes. More...
 
MTimeSpanoperator= (const MTimeSpan &other)
 Assignment operator. More...
 
MTimeSpanoperator+= (const MTimeSpan &other)
 Assignment with addition operator. More...
 
MTimeSpanoperator-= (const MTimeSpan &other)
 Assignment with subtraction operator. More...
 
MTimeSpanoperator*= (int numberOfTimes)
 Assignment with multiplication operator. More...
 
MTimeSpanoperator/= (int numberOfTimes)
 Assignment with division operator. More...
 
bool operator== (const MTimeSpan &other) const
 Equality comparison operator. More...
 
bool operator!= (const MTimeSpan &other) const
 Inequality comparison operator. More...
 
bool operator< (const MTimeSpan &other) const
 Less-than operator. More...
 
bool operator> (const MTimeSpan &other) const
 Greater-than operator. More...
 
bool operator<= (const MTimeSpan &other) const
 Less than or equal to operator. More...
 
bool operator>= (const MTimeSpan &other) const
 Greater than or equal to operator. More...
 
MTimeSpan operator- () const
 Negation operator, does not modify self.
 
MTimeSpan operator/ (int numberOfTimes) const
 Division operator. More...
 
MTimeSpan operator- (const MTimeSpan &other) const
 Subtraction operator. More...
 
MTimeSpan operator+ (const MTimeSpan &other) const
 Add operator. More...
 
MStdString AsString () const
 
void SetAsString (const MStdString &)
 
- Public Member Functions inherited from MObject
virtual ~MObject ()
 Object destructor.
 
virtual const MClassGetClass () const =0
 Get the final class of the object. More...
 
bool IsEmbeddedObject () const
 Tell if the object is of embedded kind. More...
 
SHOW_INTERNAL MVariant Call (const MStdString &name, const MVariant &params)
 Call the object service with parameters, given as variant. More...
 
MVariant Call0 (const MStdString &name)
 Call the object service with no parameters. More...
 
MVariant Call1 (const MStdString &name, const MVariant &p1)
 Call the object service with one parameter. More...
 
MVariant Call2 (const MStdString &name, const MVariant &p1, const MVariant &p2)
 Call the object service with two parameter. More...
 
MVariant Call3 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3)
 Call the object service with three parameter. More...
 
MVariant Call4 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4)
 Call the object service with four parameter. More...
 
MVariant Call5 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5)
 Call the object service with five parameter. More...
 
MVariant Call6 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5, const MVariant &p6)
 Call the object service with six parameter. More...
 
virtual MVariant CallV (const MStdString &name, const MVariant::VariantVector &params)
 Call the object service with parameters, given as variant vector. More...
 
virtual bool IsPropertyPresent (const MStdString &name) const
 Tell if the property with the given name exists.
 
virtual bool IsServicePresent (const MStdString &name) const
 Tell if the service with the given name exists.
 
virtual MVariant GetProperty (const MStdString &name) const
 Get the property value using name of the property. More...
 
virtual void SetProperty (const MStdString &name, const MVariant &value)
 Set the property using name of the property, and value. More...
 
virtual MStdStringVector GetAllPropertyNames () const
 Return the list of publicly available properties, persistent or not. More...
 
virtual MStdStringVector GetAllPersistentPropertyNames () const
 Return the list of persistent properties. More...
 
virtual void SetPersistentPropertiesToDefault ()
 Set the persistent properties of the object to their default values. More...
 
virtual MVariant GetPersistentPropertyDefaultValue (const MStdString &name) const
 Get the default value of persistent property with the name given. More...
 
virtual void SetPersistentPropertyToDefault (const MStdString &name)
 Set the persistent property with the name given to default value. More...
 
virtual const char * GetType () const
 Get the name of the type for the object (could be the same as class name).
 
virtual void SetType (const MStdString &)
 Intentionally, it will set the name of the type for the object, but the service will not allow setting the name to anything other than the current name. More...
 
virtual void Validate ()
 Validate internal structures of the object. More...
 

Friends

class M_CLASS MTime
 
MTimeSpan operator* (const MTimeSpan &p1, int p2)
 Assign-multiplicative operator.
 
MTimeSpan operator* (int p1, const MTimeSpan &p2)
 Assign-multiplicative operator.
 
M_FUNC MTimeoperator+= (MTime &, const MTimeSpan &)
 Add time span to this time and assign the result back to time. More...
 
M_FUNC MTimeoperator-= (MTime &, const MTimeSpan &)
 Subtract time span to this time and assign the result back to time. More...
 
M_FUNC MTime operator+ (const MTime &, const MTimeSpan &)
 Add a time span to time value, and without changing the two return the result. More...
 
M_FUNC MTime operator+ (const MTimeSpan &, const MTime &)
 Add a time span to time value, and without changing the two return the result. More...
 
M_FUNC MTime operator- (const MTime &, const MTimeSpan &)
 Subtract a time span from time value, and without changing the two return the result. More...
 
M_FUNC MTimeSpan operator- (const MTime &t1, const MTime &t2)
 Subtract one time value from another and return the resultant time span. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from MObject
static const MClassGetStaticClass ()
 Get the declared class of this particular object. More...
 
static bool IsClassPresent (const MStdString &name)
 Tells if the given class name is available. More...
 
- Static Public Attributes inherited from MObject
static const MClass s_class
 Class of MObject.
 
- Protected Member Functions inherited from MObject
 MObject ()
 Object constructor, protected as the class is abstract.
 
void DoSetPersistentPropertiesToDefault (const MClass *staticClass)
 Set the persistent properties to their default values for one object provided the class for that object. More...
 

Detailed Description

Time span represents the difference between two times, a duration.

Time span would be a result of subtraction of two MTime values, or it can be initialized directly. Time span can be convenient to keep the date and time separately from each other. Time span can be negative.

While null value of MTime cannot be manipulated with, null value of time span is a valid value, zero span, which corresponds to no difference between times. Default constructor initializes time span with such null value.

Time span is independent from time zone and daylight saving status. In fact, it can be used to represent a difference in time zones or a DST shift. For example, this is how the time is converted to time in another time zone:

MTimeSpan shiftESTtoUTC(0, 0, -6);
MTimeSpan shiftPSTtoUTC(0, 0, -8);
MTimeSpan shiftDST(0, 0, 1); // EST vs. EDT, PST vs. PDT
...
// Convert from EST to PST
myTime += shiftPSTtoUTC - shiftESTtoUTC;

Constructor & Destructor Documentation

MTimeSpan::MTimeSpan ( int  secs,
int  mins = 0,
int  hours = 0,
int  days = 0 
)
inlineexplicit

Constructor, which explicitly sets the time span data, including days.

Beware that the order of parameters is reverse to what is usually assumed. Negative values represent negative time span. Also, no check is done for overflow condition (when number of days is so big that it does not fit in 67 years range supported by this class).

Parameters
secsSeconds of time span.
minsMinutes of time span.
hoursHours of time span.
daysDays of time span.
MTimeSpan::MTimeSpan ( const MTime t1,
const MTime t2 
)

Constructor, which initializes the object to a difference between the two time stamps.

Equivalent to (t1 - t2).

  1. Neither time shall be null, or an exception is thrown.
  2. Absolute difference between the two times shall not be greater than internal representation allows (currently LONG_MAX), or an exception is thrown.

Member Function Documentation

MVariant MTimeSpan::Add ( const MVariant ) const

Add an object to time span object and return the result, reflection-enabling service.

Precondition
The given object shall not be NULL, and shall be compatible for addition to time.
MStdString MTimeSpan::AsFormattedString ( MConstChars  formatString) const

AsFormattedString the time span object as string.

The supported formats are:

  • %% – Replaced by a single %
  • N – For negative time span yield '-', for positive '+', ' ' for zero.
  • n – For negative time span yield '-', otherwise nothing.
  • d – Number of full days in the duration, same as D.
  • D – Number of full days in the duration, same as d.
  • h – Number of total hours in duration.
  • H – Number of hours in 24 hour format in excess of days, 00 .. 23.
  • m – Number of total minutes in duration.
  • M – Number of minutes in excess of hours, 00 .. 59.
  • s – Number of total seconds in duration.
  • S – Number of seconds in excess of minutes, 00 .. 59.
  • X – Full time span in the format days hours:minutes:seconds, same as AsString.
  • c – Full time span in a locale sensitive representation.

By default, formats d, D, h, H, m, M, s and S always give unsigned, absolute values, even if the span is negative. One can use N and n format to add sign in a desired place. Alternatively, when format has a minus sign character like %-d, %-D, %-h, %-H, %-m, %-M, %-s or %-S, the values will have sign if they are negative. For other formats, the minus sign character is ignored.

Precondition
The format string has to be valid.
MStdString MTimeSpan::AsString ( ) const

Represent time span as string.

Zero time span is represented as "00:00:00". If the string fits in one day, the property will have only hours, minutes and seconds, such as "00:00:01" (one second into the future), "23:00:00" (23 hours into the future). Otherwise, if the time is bigger than one day it will have number of days at the beginning, separated by a blank, such as "1000 00:00:00" for 1000 days into the future. Negative time span (referring to the past) will have a minus sign at the beginning, such as "-00:01:00" for one minute into the past, and "-100 00:00:00" for 100 days into the past.

The given time span string syntax is this:

  • A minus sign can appear at the very first position of the string, which represents the time span towards the past.
  • There can only be a single blank in the string input, and it should appear prior to the number of days of the span. After the blank there should be the time portion.
  • Time portion can be the number of seconds alone.
  • If the time portion has a single colon, it is assumed to be hours:minutes.
  • If the time portion has two colons, it is assumed to be hours:minutes:seconds.

Examples of valid string inputs:

"0" // Zero time span
"-1" // One second towards the past
"1234567" // 1234567 seconds into the future
"1 1234" // 1 day and 1234 seconds into the future
"-123 01:20" // 123 days, 1 hour, and 20 minutes into the past
"1 2:5:9" // 1 day, 2 hours, 5 minutes, and 9 seconds into the future
int MTimeSpan::Compare ( const MTimeSpan other) const
inline

Compare two time spans, ternary comparison service.

The exact value returned is not specified, only the signed value or zero.

Parameters
otherOther variant to compare with.
Returns
0 = spans are equal, negative = this one is shorter, positive = this one is longer
int MTimeSpan::CompareWithVariant ( const MVariant other) const

Same as Compare, but uses a variant as the other type.

Parameters
otherIf this is a numeric value, then it is the number of seconds. If this is an object of type MTimeSpan, then a regular Compare is called.
Returns
0 = spans are equal, negative = this one is shorter, positive = this one is longer
MVariant MTimeSpan::Divide ( int  numberOfTimes) const

Divide a time span object with the given number of times, return the result.

Precondition
The given object shall not be NULL, and shall be compatible for addition to time.
void MTimeSpan::DoSet1 ( int  seconds)

Set the new time span value from the given number if seconds.

Parameters
secondsSeconds value to initialize time span.
void MTimeSpan::DoSet3 ( int  seconds,
int  minutes,
int  hours 
)

Set the new time span value from the given time.

Parameters
secondsSeconds value of the result time span.
minutesMinutes value of the result time span.
hoursHours value of the result time span.
int MTimeSpan::GetDays ( ) const
inline

Get days fraction of the time span, total 24 hour portions in the duration.

Time span could be negative, in which case the value will be negative too. There is no correspondent ToDays function, as days is the largest quantity of MTimeSpan.

virtual unsigned MTimeSpan::GetEmbeddedSizeof ( ) const
virtual

Time span is an embedded object type, therefore return its size in bytes.

Returns
size of MTimeSpan in bytes.

Reimplemented from MObject.

int MTimeSpan::GetHours ( ) const

Get hours part of the time span, -23 to 23.

See also
ToHours which returns total hours in the time span.
int MTimeSpan::GetMinutes ( ) const

Get minutes fraction of the time span.

See also
ToMinutes which returns total minutes in the time span.
int MTimeSpan::GetSeconds ( ) const

Get seconds fraction of the time span.

See also
ToSeconds which returns total seconds in the time span.
MVariant MTimeSpan::Multiply ( int  numberOfTimes) const

Multiply a time span object with the given number of times, return the result.

Precondition
The given object shall not be NULL, and shall be compatible for addition to time.
MVariant MTimeSpan::NewClone ( ) const

Reflection-enabling copy constructor.

Precondition
The given object shall be of type TimeSpan, or an exception is thrown.
bool MTimeSpan::operator!= ( const MTimeSpan other) const
inline

Inequality comparison operator.

Precondition
Objects are initialized.
MTimeSpan& MTimeSpan::operator*= ( int  numberOfTimes)
inline

Assignment with multiplication operator.

Precondition
Object is created and initialized.
MTimeSpan MTimeSpan::operator+ ( const MTimeSpan other) const
inline

Add operator.

Precondition
Objects are initialized, no checks are done.
MTimeSpan& MTimeSpan::operator+= ( const MTimeSpan other)
inline

Assignment with addition operator.

Precondition
Object is created and initialized.
MTimeSpan MTimeSpan::operator- ( const MTimeSpan other) const
inline

Subtraction operator.

Precondition
Objects are initialized, no checks are done.
MTimeSpan& MTimeSpan::operator-= ( const MTimeSpan other)
inline

Assignment with subtraction operator.

Precondition
Object is created and initialized.
MTimeSpan MTimeSpan::operator/ ( int  numberOfTimes) const

Division operator.

Precondition
Object is initialized and numberOfTimes shall not be zero.
MTimeSpan& MTimeSpan::operator/= ( int  numberOfTimes)
inline

Assignment with division operator.

Precondition
Object is created and initialized.
bool MTimeSpan::operator< ( const MTimeSpan other) const
inline

Less-than operator.

Precondition
Objects are initialized.
bool MTimeSpan::operator<= ( const MTimeSpan other) const
inline

Less than or equal to operator.

Precondition
Objects are initialized.
MTimeSpan& MTimeSpan::operator= ( const MTimeSpan other)
inline

Assignment operator.

Precondition
Object is created.
bool MTimeSpan::operator== ( const MTimeSpan other) const
inline

Equality comparison operator.

Precondition
Objects are initialized.
bool MTimeSpan::operator> ( const MTimeSpan other) const
inline

Greater-than operator.

Precondition
Objects are initialized.
bool MTimeSpan::operator>= ( const MTimeSpan other) const
inline

Greater than or equal to operator.

Precondition
Objects are initialized.
void MTimeSpan::Set ( int  secs,
int  mins = 0,
int  hours = 0,
int  days = 0 
)
inline

Set all parameters for the time span.

Beware that the order of parameters is reverse to one usually assumed. Negative values indicate negative time span (into the past).

Also, no check is done for overflow condition (when number of days is so big that it does not fit in 67 years range supported by this class).

void MTimeSpan::SetAsString ( const MStdString )

Represent time span as string.

Zero time span is represented as "00:00:00". If the string fits in one day, the property will have only hours, minutes and seconds, such as "00:00:01" (one second into the future), "23:00:00" (23 hours into the future). Otherwise, if the time is bigger than one day it will have number of days at the beginning, separated by a blank, such as "1000 00:00:00" for 1000 days into the future. Negative time span (referring to the past) will have a minus sign at the beginning, such as "-00:01:00" for one minute into the past, and "-100 00:00:00" for 100 days into the past.

The given time span string syntax is this:

  • A minus sign can appear at the very first position of the string, which represents the time span towards the past.
  • There can only be a single blank in the string input, and it should appear prior to the number of days of the span. After the blank there should be the time portion.
  • Time portion can be the number of seconds alone.
  • If the time portion has a single colon, it is assumed to be hours:minutes.
  • If the time portion has two colons, it is assumed to be hours:minutes:seconds.

Examples of valid string inputs:

"0" // Zero time span
"-1" // One second towards the past
"1234567" // 1234567 seconds into the future
"1 1234" // 1 day and 1234 seconds into the future
"-123 01:20" // 123 days, 1 hour, and 20 minutes into the past
"1 2:5:9" // 1 day, 2 hours, 5 minutes, and 9 seconds into the future
MVariant MTimeSpan::Subtract ( const MVariant ) const

Subtract an object to time span object and return the result, reflection-enabling service.

Precondition
The given object shall not be NULL, and shall be compatible for addition to time.
int MTimeSpan::ToHours ( ) const
inline

Convert time span to hours.

This function differs from GetHours in that it returns the total value.

Precondition
Object is initialized.
int MTimeSpan::ToMinutes ( ) const
inline

Convert time span to minutes.

This function differs from GetMinutes in that it returns the total value.

Precondition
Object is initialized.
int MTimeSpan::ToSeconds ( ) const
inline

Convert time span to seconds.

This function differs from GetSeconds in that it returns the total value.

Precondition
Object is initialized.

Friends And Related Function Documentation

M_FUNC MTime operator+ ( const MTime ,
const MTimeSpan  
)
friend

Add a time span to time value, and without changing the two return the result.

Precondition
Time shall not be null, or an exception is thrown.
M_FUNC MTime operator+ ( const MTimeSpan ,
const MTime  
)
friend

Add a time span to time value, and without changing the two return the result.

Precondition
Time shall not be null, or an exception is thrown.
M_FUNC MTime& operator+= ( MTime ,
const MTimeSpan  
)
friend

Add time span to this time and assign the result back to time.

Precondition
Time shall not be null, or an exception is thrown.
M_FUNC MTime operator- ( const MTime ,
const MTimeSpan  
)
friend

Subtract a time span from time value, and without changing the two return the result.

Precondition
Time shall not be null, or an exception is thrown.
M_FUNC MTimeSpan operator- ( const MTime t1,
const MTime t2 
)
friend

Subtract one time value from another and return the resultant time span.

Precondition
Neither time shall be null, or an exception is thrown.
M_FUNC MTime& operator-= ( MTime ,
const MTimeSpan  
)
friend

Subtract time span to this time and assign the result back to time.

Precondition
Time shall not be null, or an exception is thrown.