Times

Chapter Updated 05/11/26




Class xbTime - Time Class.

The xbTime class is used for storing and manipulating time values.

The time is stored internally in a 32 bit integer using the following calculation:
Hours * 36000000L + minutes * 60000L + Seconds * 1000L

  • This class is available when the XB_DBF7_SUPPORT option is compiled into the library.

  • Methods for setting the time format at the system level:

    xbCore::SetDefaultTimeFormat()
    xbCore::GetDefaultTimeFormat()

  • Methods for setting the time format at the instance level (over ride default setting).

    xbTime::SetTimeFmt()
    xbTime::GetTimeFmt()

  • Methods for retrieving time values:

    xbDate::Str() - Returns time string in HH:MM:SS format.
    xbDate::Strf() - Returns time string in current specified format.

  • If the system level default is blank and the instance level format is blank, the system uses HH:MM:SS (%H:%M:%S) as the default time format.

  • Valid Time Format Specifiers:

    %H - Hour - 24 hour clock as a decimal number 00-23
    %I - Hour - 12 hour clock as a decimal number 01-12
    %M - Minutes as a decimal number 00-59
    %S - Second as a decimal number 00-59
    %p - Locale's equivalent of AM/PM with 12 hour clock

    The default format is %H:%M:%S


    Class xbTime methods

    MethodDescription
    xbTime()
    xbTime()
    xbTime( const xbString & sTime )
    xbTime( xbInt32 lTime )
    xbTime( xbInt16 iHH, xbInt16 iMM, xbInt16 iSS = 0 )
    Various xbTime constructors.
    xbInt32 Add( char cType, xbInt32 lCnt )Add or subtract from the time value.
    cType - one of H,h,M,m,S,s for Hour minute or second for operation.
    lCnt - Number to add or subtrat from the time. Use a negative number to subtract.
    Returns the number of days the time would have rolled. For example, adding 50 hours would return +2 days, as the time would have rolled forward two days. Conversely, subtracting 50 hours would return -2.
    void Dump()
    void Dump( xbString & sHeader )
    void Dump( const char *sHeader )
    Dump the internal contents of the screen to stdout.
    Available if XB_DEBUG_SUPPORT compiled into the library.
    sHeader is an optional header line to display.
    xbInt16 FromTimeT( time_t tIn )Set the time from standard C time_t structure.
    xbString &GetDefaultTimeFormat() constReturn the default time format.
    xbInt16 GetHour () constReturn the Hour for the time.
    xbInt16 GetMinute() constReturn the Minute for the time.
    xbInt16 GetSecond() constReturn the Second for the time.
    xbInt32 GetTime () constReturn the time value as a long int. See description for how the value is calculated.
    xbInt16 GetTime ( xbString &sTimeOut )Return formatted string in sTimeOut.
    The format is determinied by searching in the following order for a time format:
    Object level format
    System default time format
    HH:MM:SS
    xbInt16 GetTime ( xbString &sTimeOut, const char *sFmtIn )Return the formatted string in sTimeOut using format specified in sFmtIn.
    See the beginning of this chapter for list of valid format specifiers.
    xbInt16 GetTime ( xbString &sTimeOut, const xbString &sFmtIn )Return the formatted string in sTimeOut using format specified in sFmtIn.
    See the beginning of this chapter for list of valid format specifiers.
    const xbString &GetTimeFormat() constReturn the time format for the given instance.
    xbBool IsNull() constReturns true if time is null, otherwise returns false.
    xbBool operator!=( const xbTime & ) constCompare two times for inequality.
    xbInt32 operator+( xbInt32 l ) constAdd seconds to a time.
    xbTime &operator++( xbInt32 l )Increment a time by one second.
    xbTime &operator+=( xbInt32 l )Increment a time by specified numbed of seconds.
    xbInt32 operator-( xbInt32 l ) constSubtract seconds from a time.
    xbTime &operator--( xbInt32 l )Decrement a time by one second.
    xbTime &operator-=( xbInt32 l )Decrement a time by specified number of seconds.
    xbBool operator< ( const xbTime & ) constLess than compare.
    xbBool operator<=( const xbTime & ) constLess than or equal compare.
    void operator=( xbInt32 t )
    void operator=( const xbTime &t )
    Set a time equal to another time.
    xbBool operator==( const xbTime & ) constComapare two times for equality.
    xbBool operator> ( const xbTime & ) constGreater than compare.
    xbBool operator>=( const xbTime & ) constGreater than or equal compare.
    xbInt16 Set( const char * sTime )sTime formatted as HH:MM:SS or HH:MM
    xbInt16 Set( xbInt16 iHH = 0, xbInt16 iMM = 0, xbInt16 iSS = 0 )Set the time to the values as defined in iHH, iMM, iSS for hours, minutes and seconds.
    xbInt16 Set( xbInt32 lTime )Set the time to value defined in lTime.
    xbInt16 Set( const xbString &sTime )sTime formatted as HH:MM:SS or HH:MM
    xbInt16 Set( xbTime &tm )Set the time to the value defined in tm.
    void SetDefaultTimeFormat( const char * sDefaultTimeFormat )Set the default time format.<\td><\tr>
    void SetDefaultTimeFormat( const xbString &sDefaultTimeFormat )Set the default time format.<\td><\tr>
    void SetNull()Set time instance to null.
    void SetTimeFormat( const char *sFmtIn )Set the time format for the given instance to sFmtIn.
    See valid format specifiers in beginning of chapter.
    void SetTimeFormat( const xbString &sFmtIn )Set the time format for the given instance to sFmtIn.
    See valid format specifiers in beginning of chapter.
    const char *Str()Return the time in HH:MM:SS format.
    const char *Strf()Return the time in specified format.
    xbInt16 Systime()Set the time to the system time.
    time_t ToTimeT() const Return the time in a time_t structure.





    Example program using xbTime class

    /* xb_ex_time.cpp XBase64 Software Library Copyright (c) 1997,2003,2014,2021,2022, 2023, 2024 Gary A Kunkel The xb64 software library is covered under the terms of the GPL Version 3, 2007 license. Email Contact: XDB-devel@lists.sourceforge.net XDB-users@lists.sourceforge.net This program demonstrates using the xbTime class */ #include "xbase.h" using namespace xb; int main() { xbXBase x; /* initial date static variables */ xbString sTime; xbString sFmt; xbTime t1( 10, 11, 12 ); sFmt = "%H:%M"; t1.GetTime( sTime, sFmt ); std::cout << "Time is [" << t1.GetHour() << "]\n"; std::cout << "Hour is [" << t1.GetHour() << "]\n"; std::cout << "Minute is [" << t1.GetMinute() << "]\n"; std::cout << "Second is [" << t1.GetSecond() << "]\n"; #ifdef XB_DEBUG_SUPPORT t1.Dump( "Dump of T1" ); #endif // XB_DEBUG_SUPPORT // various constructor examples xbString s = "08:15:45"; xbTime t2( s ); s = "9:2:5"; xbTime t3( s ); s = "3:2"; xbTime t4( s ); xbTime t5; t5.SetToNow(); // add one second to t2 t2.Add( 'S', 1 ); // add 10 minutes to t2 t2.Add( 'M', 10 ); // add 1 hour to t2 t2.Add( 'H', 1 ); s = "%S|%M|%H"; t2.SetTimeFormat( s ); std::cout << "Time format is [" << t2.GetTimeFormat().Str() << "]\n"; s = "%H%M%S"; x.SetDefaultTimeFormat( s ); std::cout << "Default time format is [" << x.GetDefaultTimeFormat().Str() << "]\n"; // set t3 to what in t2 t3 = t2; if( t2 == t3 ) std::cout << "Times match\n"; else std::cout << "Times don't match\n"; if( t4 != t3 ) std::cout << "Times don't match\n"; else std::cout << "Times match\n"; t1++; // add one second to t1 t1+=6; // add six seconds to t1 t1--; // subtract one second from t1 t1-=15; // subtract 15 seconds from t1 if( t3 > t4 ) // greater than compare std::cout << "true\n"; else std::cout << "false\n"; if( t3 < t4 ) // less than compare std::cout << "true\n"; else std::cout << "false\n"; if( t3 >= t4 ) // greater than or equal compare std::cout << "true\n"; else std::cout << "false\n"; if( t3 <= t4 ) // less than or equal compare std::cout << "true\n"; else std::cout << "false\n"; return 0; }