Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
Gray::cTimeUnits Class Reference

#include <cTimeUnits.h>

Public Member Functions

void SetZeros ()
 
bool InitTimeNow (TZ_TYPE nTimeZoneOffset=TZ_LOCAL)
 
COMPARE_TYPE Compare (cTimeUnits &b) const
 
bool isTimeFuture () const
 
bool isTimePast () const
 
TIMEDOW_TYPE get_DOW () const
 
int get_DOY () const
 
TIMEMONTH_TYPE get_Month () const
 
bool IsValidUnit (TIMEUNIT_TYPE i) const
 
bool isValidTimeUnits () const
 
bool isReasonableTimeUnits () const
 
TIMEUNIT_t GetUnit (TIMEUNIT_TYPE i) const
 
TIMEUNIT_t GetUnit0 (TIMEUNIT_TYPE i) const
 
void SetUnit (TIMEUNIT_TYPE i, TIMEUNIT_t wVal)
 
bool operator== (const cTimeUnits &rTu) const
 
void put_DosDate (UINT32 ulDosDate)
 
UINT32 get_DosDate () const
 
void AddMonths (int iMonths)
 
void AddDays (int iDays)
 
void AddSeconds (TIMESECD_t nSeconds)
 
void AddTZ (TZ_TYPE nTimeZoneOffset)
 
bool isInDST () const
 
StrLen_t GetFormStr (GChar_t *pszOut, StrLen_t iOutSizeMax, const GChar_t *pszFormat) const
 
StrLen_t GetFormStr (GChar_t *pszOut, StrLen_t iOutSizeMax, TIME_FORMAT_TYPE eFormat=TIME_FORMAT_DEFAULT) const
 
HRESULT SetTimeStr (const GChar_t *pszDateTime, TZ_TYPE nTimeZoneOffset)
 
StrLen_t GetTimeSpanStr (GChar_t *pszOut, StrLen_t iOutSizeMax, TIMEUNIT_TYPE eUnitHigh=TIMEUNIT_Day, int iUnitsDesired=2, bool bShortText=false) const
 
TIMESECD_t get_SecondOfDay () const
 
bool isValidMonth () const
 
TIMEUNIT_t get_DaysInMonth () const
 
TIMEUNIT_t get_DayOfYear () const
 
TIMEUNIT_t get_DaysInYear () const
 
 cTimeUnits (void)
 
 cTimeUnits (TIMEUNIT_t wYear, TIMEUNIT_t wMonth, TIMEUNIT_t wDay, TIMEUNIT_t wHour=0, TIMEUNIT_t wMinute=0, TIMEUNIT_t wSecond=0, TIMEUNIT_t wMilliseconds=0, TIMEUNIT_t wMicroseconds=0, TZ_TYPE nTZ=TZ_UTC)
 

Static Public Member Functions

static int __stdcall IsLeapYear (TIMEUNIT_t wYear)
 
static int __stdcall GetLeapYearsSince2K (TIMEUNIT_t wYear)
 
static TIMEDOW_TYPE __stdcall GetDOW (TIMEUNIT_t wYear, TIMEUNIT_t wMonth, TIMEUNIT_t wDay)
 
static int __stdcall GetDOY (TIMEUNIT_t wYear, TIMEUNIT_t wMonth, TIMEUNIT_t wDay)
 

Public Attributes

TIMEUNIT_t m_wYear
 Year valid for 1980 to 2043 at least. TIMEUNIT_Year. More...
 
TIMEUNIT_t m_wMonth
 1 based month of year. Jan=1, to 12=Dec, NOT TIMEMONTH_TYPE. TIMEUNIT_Month More...
 
TIMEUNIT_t m_wDay
 1 based day of month. 1 to 31. TIMEUNIT_Day More...
 
TIMEUNIT_t m_wHour
 0 to 23 for hour of day. More...
 
TIMEUNIT_t m_wMinute
 0 to 59 More...
 
TIMEUNIT_t m_wSecond
 0 to 59 More...
 
TIMEUNIT_t m_wMillisecond
 1000th = thousandth. 0 to 1000 More...
 
TIMEUNIT_t m_wMicrosecond
 1000000th = millionth. 0 to 1000. TIMEUNIT_Microsecond More...
 
TIMEUNIT_t m_nTZ
 TZ_TYPE where m_wHour has NOT yet been adjusted. TIMEUNIT_TZ. More...
 

Static Public Attributes

static const TIMESECD_t k_nSecondsPerDay = (24 * 60 * 60)
 seconds in a day = 86400 More...
 
static const TIMESECD_t k_nSecondsPerHour = (60 * 60)
 seconds in a hour = 3600 More...
 
static const int k_nMinutesPerDay = (24 * 60)
 minutes in a day More...
 
static const int k_nMicroSeconds = 1000000
 millionth of a second. More...
 
static const CTimeUnit k_Units [TIMEUNIT_QTY]
 Metadata for time units. More...
 
static const GChar_tk_StrFormats [TIME_FORMAT_QTY+1]
 standard strftime() type formats. More...
 
static const BYTE k_MonthDays [2][TIMEMONTH_QTY]
 Jan=0. More...
 
static const WORD k_MonthDaySums [2][TIMEMONTH_QTY+1]
 Jan=0. More...
 
static const GChar_t *const k_MonthName [TIMEMONTH_QTY+1]
 January=0. More...
 
static const GChar_t *const k_MonthAbbrev [TIMEMONTH_QTY+1]
 Jan=0. More...
 
static const GChar_t *const k_DayName [TIMEDOW_QTY+1]
 Sunday=0. More...
 
static const GChar_t *const k_DayAbbrev [TIMEDOW_QTY+1]
 Sun=0. More...
 
static const GChar_t k_TimeSeparator = ':'
 this is the same for all formats. e.g. 09:00 AM. NOT USED ? More...
 
static const GChar_t k_Seps [3] = _GT("/:")
 
static const GChar_t k_SepsAll [8] = _GT("/ :T.,-")
 
static GChar_t sm_DateSeparator = '/'
 date separator to use for string creation = '\', '-', '.', but Time is always ':' More...
 
static bool sm_time24Mode = false
 Display time in 24 hour format. default = false. More...
 

Friends

class cTimeUnitsTests
 

Detailed Description

Decompose/Break time into units in order of size. like: struct tm for POSIX time_t. like: SYSTEMTIME for _WIN32 like: TIMESTAMP_STRUCT for SQL_TIMESTAMP, SQL_C_TIMESTAMP, SQL_DATE

Constructor & Destructor Documentation

◆ cTimeUnits() [1/2]

Gray::cTimeUnits::cTimeUnits ( void  )
inline

◆ cTimeUnits() [2/2]

Gray::cTimeUnits::cTimeUnits ( TIMEUNIT_t  wYear,
TIMEUNIT_t  wMonth,
TIMEUNIT_t  wDay,
TIMEUNIT_t  wHour = 0,
TIMEUNIT_t  wMinute = 0,
TIMEUNIT_t  wSecond = 0,
TIMEUNIT_t  wMilliseconds = 0,
TIMEUNIT_t  wMicroseconds = 0,
TZ_TYPE  nTZ = TZ_UTC 
)
inline

Member Function Documentation

◆ AddDays()

void Gray::cTimeUnits::AddDays ( int  iDays)

Add Days. Adjust for the fact that months and years are not all the same number of days.

  • iDays can be negative. Do we cross years ?

◆ AddMonths()

void Gray::cTimeUnits::AddMonths ( int  iMonths)

Add months to this structure. months are not exact time measures, but there are always 12 per year.

  • iMonths = 0 based. Can be <0

◆ AddSeconds()

void Gray::cTimeUnits::AddSeconds ( TIMESECD_t  nSeconds)

Add TimeUnits with seconds. handles very large values of seconds. Used to adjust for TZ and DST. nSeconds can be negative.

◆ AddTZ()

void Gray::cTimeUnits::AddTZ ( TZ_TYPE  nTimeZoneOffset)

add TZ Offset in minutes.

◆ Compare()

COMPARE_TYPE Gray::cTimeUnits::Compare ( cTimeUnits b) const

Compare relevant parts of 2 times.

Note
TODO DOES NOT FACTOR TIMEUNIT_TZ

◆ get_DayOfYear()

TIMEUNIT_t Gray::cTimeUnits::get_DayOfYear ( ) const
inline

◆ get_DaysInMonth()

TIMEUNIT_t Gray::cTimeUnits::get_DaysInMonth ( ) const
inline

◆ get_DaysInYear()

TIMEUNIT_t Gray::cTimeUnits::get_DaysInYear ( ) const
inline

◆ get_DosDate()

UINT32 Gray::cTimeUnits::get_DosDate ( ) const

get/pack a 32 bit DOS date format. for ZIP files. and old FAT file system. ASSUME isValidTimeUnits().

◆ get_DOW()

TIMEDOW_TYPE Gray::cTimeUnits::get_DOW ( ) const
inline

Calculated Day of Week

Returns
TIMEDOW_Sun = 0

◆ get_DOY()

int Gray::cTimeUnits::get_DOY ( ) const
inline

Day of year. 0 based

◆ get_Month()

TIMEMONTH_TYPE Gray::cTimeUnits::get_Month ( ) const
inline

◆ get_SecondOfDay()

TIMESECD_t Gray::cTimeUnits::get_SecondOfDay ( ) const
inline

◆ GetDOW()

TIMEDOW_TYPE GRAYCALL Gray::cTimeUnits::GetDOW ( TIMEUNIT_t  wYear,
TIMEUNIT_t  wMonth,
TIMEUNIT_t  wDay 
)
static
Returns
day of week for a particular date. TIMEDOW_TYPE wMonth = 1 based wDay = 1 based.

◆ GetDOY()

int GRAYCALL Gray::cTimeUnits::GetDOY ( TIMEUNIT_t  wYear,
TIMEUNIT_t  wMonth,
TIMEUNIT_t  wDay 
)
static

Day of the year. 0 to 365 wMonth = 1 based wDay = 1 based.

◆ GetFormStr() [1/2]

StrLen_t Gray::cTimeUnits::GetFormStr ( GChar_t pszOut,
StrLen_t  iOutSizeMax,
const GChar_t pszFormat 
) const

Get the time as a formatted string using "C" strftime() build formatted string from cTimeUnits. similar to C stdlib strftime() http://linux.die.net/man/3/strftime add TZ as postfix if desired?? used by cTimeDouble::GetTimeFormStr and cTimeInt::GetTimeFormStr

Returns
length of string in chars. <= 0 = failed.

◆ GetFormStr() [2/2]

StrLen_t Gray::cTimeUnits::GetFormStr ( GChar_t pszOut,
StrLen_t  iOutSizeMax,
TIME_FORMAT_TYPE  eFormat = TIME_FORMAT_DEFAULT 
) const
inline

◆ GetLeapYearsSince2K()

int GRAYCALL Gray::cTimeUnits::GetLeapYearsSince2K ( TIMEUNIT_t  wYear)
static

calculate the number of leap days/years since Jan 1 of a year to Jan 1 2000. (Jan 1 2000 = TIMEDOW_Sat) to Jan 1 wYear can be negative if wYear < 2000

◆ GetTimeSpanStr()

StrLen_t Gray::cTimeUnits::GetTimeSpanStr ( GChar_t pszOut,
StrLen_t  iOutSizeMax,
TIMEUNIT_TYPE  eUnitHigh = TIMEUNIT_Day,
int  iUnitsDesired = 2,
bool  bShortText = false 
) const

A delta/span time string. from years to milliseconds. Get a text description of amount of time span (delta)

  • eUnitHigh = the highest unit, TIMEUNIT_Day, TIMEUNIT_Minute iUnitsDesired = the number of units up the cTimeUnits::k_Units ladder to go. default=2
    Returns
    length of string in chars

◆ GetUnit()

TIMEUNIT_t Gray::cTimeUnits::GetUnit ( TIMEUNIT_TYPE  i) const
inline

enumerate the time units.

◆ GetUnit0()

TIMEUNIT_t Gray::cTimeUnits::GetUnit0 ( TIMEUNIT_TYPE  i) const
inline

◆ InitTimeNow()

bool Gray::cTimeUnits::InitTimeNow ( TZ_TYPE  nTimeZoneOffset = TZ_LOCAL)

Get the current time, and adjust units for timezone. nDST ?? like _WIN32 GetLocalTime(st), GetSystemTime(st)

◆ isInDST()

bool Gray::cTimeUnits::isInDST ( ) const

Is this Date DST? Assuming local time zone honors DST. http://www.worldtimezone.com/daylight.html like the C internal function _isindst(const struct tm *tb)

Note
rule for years < 1987: begin after 2 AM on the last Sunday in April end 1 AM on the last Sunday in October.

rule for years >= 1987: begin after 2 AM on the first Sunday in April end 1 AM on the last Sunday in October.

rule for years >= 2007: begin Second Sunday in March 2AM end First Sunday in November 2AM

◆ IsLeapYear()

int GRAYCALL Gray::cTimeUnits::IsLeapYear ( TIMEUNIT_t  wYear)
static

0 or 1 NOT Boolean - for array access. Every year divisible by 4 is a leap year. But every year divisible by 100 is NOT a leap year Unless the year is also divisible by 400, then it is still a leap year.

◆ isReasonableTimeUnits()

bool Gray::cTimeUnits::isReasonableTimeUnits ( ) const

Is this data reasonable for most purposes?

◆ isTimeFuture()

bool Gray::cTimeUnits::isTimeFuture ( ) const

◆ isTimePast()

bool Gray::cTimeUnits::isTimePast ( ) const
inline

AKA Expired ?

◆ isValidMonth()

bool Gray::cTimeUnits::isValidMonth ( ) const
inline

◆ isValidTimeUnits()

bool Gray::cTimeUnits::isValidTimeUnits ( ) const

Are the values in valid range ?

Note
If we are just using this for time math values may go out of range ?

◆ IsValidUnit()

bool Gray::cTimeUnits::IsValidUnit ( TIMEUNIT_TYPE  i) const

◆ operator==()

bool Gray::cTimeUnits::operator== ( const cTimeUnits rTu) const
inline

◆ put_DosDate()

void Gray::cTimeUnits::put_DosDate ( UINT32  ulDosDate)

unpack 32 bit DosDate format. for ZIP files and old FAT file system. we could use DosDateTimeToFileTime and LocalFileTimeToFileTime for _WIN32 0-4 =Day of the month (1-31) 5-8 =Month (1 = January, 2 = February, and so on) 9-15 =Year offset from 1980 (add 1980 to get actual year) . 6 bits = 63 years = 2043 failure.

◆ SetTimeStr()

HRESULT Gray::cTimeUnits::SetTimeStr ( const GChar_t pszDateTime,
TZ_TYPE  nTimeZoneOffset 
)

set cTimeUnits from a string.

  • pszDateTime = "2008/10/23 12:0:0 PM GMT" rnTimeZoneOffset = if found a time zone indicator in the string. do not set if nothing found.
    Note
    Must support all regular TIME_FORMAT_QTY types.
    Returns

    0 length = OK, <=0 = doesn't seem like a valid datetime.

    e.g. "Sat, 07 Aug 2004 01:20:20", "" toJSON method: "2012-04-23T18:25:43.511Z" is sortable.

◆ SetUnit()

void Gray::cTimeUnits::SetUnit ( TIMEUNIT_TYPE  i,
TIMEUNIT_t  wVal 
)
inline

◆ SetZeros()

void Gray::cTimeUnits::SetZeros ( )

Friends And Related Function Documentation

◆ cTimeUnitsTests

friend class cTimeUnitsTests
friend

Member Data Documentation

◆ k_DayAbbrev

const GChar_t *const Gray::cTimeUnits::k_DayAbbrev
static
Initial value:
=
{
_GT("Sun"), _GT("Mon"), _GT("Tue"), _GT("Wed"), _GT("Thu"), _GT("Fri"), _GT("Sat"),
nullptr
}
#define _GT(x)
like _T(x) macro for static text.
Definition: StrConst.h:27

Sun=0.

◆ k_DayName

const GChar_t *const Gray::cTimeUnits::k_DayName
static
Initial value:
=
{
_GT("Sunday"), _GT("Monday"), _GT("Tuesday"), _GT("Wednesday"),
_GT("Thursday"), _GT("Friday"), _GT("Saturday"),
nullptr
}

Sunday=0.

◆ k_MonthAbbrev

const GChar_t *const Gray::cTimeUnits::k_MonthAbbrev
static
Initial value:
=
{
_GT("Jan"), _GT("Feb"), _GT("Mar"), _GT("Apr"), _GT("May"), _GT("Jun"),
_GT("Jul"), _GT("Aug"), _GT("Sep"), _GT("Oct"), _GT("Nov"), _GT("Dec"),
nullptr,
}

Jan=0.

◆ k_MonthDays

const BYTE Gray::cTimeUnits::k_MonthDays
static
Initial value:
=
{
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
}

Jan=0.

◆ k_MonthDaySums

const WORD Gray::cTimeUnits::k_MonthDaySums
static
Initial value:
=
{
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
}

Jan=0.

◆ k_MonthName

const GChar_t *const Gray::cTimeUnits::k_MonthName
static
Initial value:
=
{
_GT("January"), _GT("February"), _GT("March"), _GT("April"), _GT("May"), _GT("June"),
_GT("July"), _GT("August"), _GT("September"), _GT("October"), _GT("November"), _GT("December"),
nullptr
}

January=0.

◆ k_nMicroSeconds

const int Gray::cTimeUnits::k_nMicroSeconds = 1000000
static

millionth of a second.

◆ k_nMinutesPerDay

const int Gray::cTimeUnits::k_nMinutesPerDay = (24 * 60)
static

minutes in a day

◆ k_nSecondsPerDay

const TIMESECD_t Gray::cTimeUnits::k_nSecondsPerDay = (24 * 60 * 60)
static

seconds in a day = 86400

◆ k_nSecondsPerHour

const TIMESECD_t Gray::cTimeUnits::k_nSecondsPerHour = (60 * 60)
static

seconds in a hour = 3600

◆ k_Seps

const GChar_t Gray::cTimeUnits::k_Seps = _GT("/:")
static

◆ k_SepsAll

const GChar_t Gray::cTimeUnits::k_SepsAll = _GT("/ :T.,-")
static

◆ k_StrFormats

const GChar_t * Gray::cTimeUnits::k_StrFormats
static
Initial value:
=
{
_GT("%Y/%m/%d %H:%M:%S"),
_GT("%Y-%m-%d %H:%M:%S"),
_GT("%Y-%m-%d %H:%M:%S %Z"),
_GT("%m/%d/%Y %H:%M:%S"),
_GT("%a, %d %b %Y %H:%M:%S %z"),
_GT("%d %b %Y %H:%M:%S %z"),
_GT("%Y/%m/%dT%H:%M:%S"),
_GT("%Y/%m/%dT%H:%M:%S%z"),
_GT("%Y%m%d%H%M%S%z"),
nullptr,
}

standard strftime() type formats.

◆ k_TimeSeparator

const GChar_t Gray::cTimeUnits::k_TimeSeparator = ':'
static

this is the same for all formats. e.g. 09:00 AM. NOT USED ?

◆ k_Units

const CTimeUnit Gray::cTimeUnits::k_Units
static
Initial value:
=
{
{ _GT("year"), _GT("Y"), 1, 3000, 12, 365 * 24 * 60 * 60, 365.25 },
{ _GT("month"), _GT("M"), 1, 12, 30, 30 * 24 * 60 * 60, 30.43 },
{ _GT("day"), _GT("d"), 1, 31, 24, 24 * 60 * 60, 1.0 },
{ _GT("hour"), _GT("h"), 0, 23, 60, 60 * 60, 1.0 / (24.0) },
{ _GT("minute"), _GT("m"), 0, 59, 60, 60, 1.0 / (24.0*60.0) },
{ _GT("second"), _GT("s"), 0, 59, 1000, 1, 1.0 / (24.0*60.0*60.0) },
{ _GT("millisec"), _GT("ms"), 0, 999, 1000, 0, 1.0 / (24.0*60.0*60.0*1000.0) },
{ _GT("microsec"), _GT("us"), 0, 999, 0, 0, 1.0 / (24.0*60.0*60.0*1000.0*1000.0) },
{ _GT("TZ"), _GT("TZ"), -24 * 60, 24 * 60, 0, 0, 1.0 },
}

Metadata for time units.

◆ m_nTZ

TIMEUNIT_t Gray::cTimeUnits::m_nTZ

TZ_TYPE where m_wHour has NOT yet been adjusted. TIMEUNIT_TZ.

◆ m_wDay

TIMEUNIT_t Gray::cTimeUnits::m_wDay

1 based day of month. 1 to 31. TIMEUNIT_Day

◆ m_wHour

TIMEUNIT_t Gray::cTimeUnits::m_wHour

0 to 23 for hour of day.

◆ m_wMicrosecond

TIMEUNIT_t Gray::cTimeUnits::m_wMicrosecond

1000000th = millionth. 0 to 1000. TIMEUNIT_Microsecond

◆ m_wMillisecond

TIMEUNIT_t Gray::cTimeUnits::m_wMillisecond

1000th = thousandth. 0 to 1000

◆ m_wMinute

TIMEUNIT_t Gray::cTimeUnits::m_wMinute

0 to 59

◆ m_wMonth

TIMEUNIT_t Gray::cTimeUnits::m_wMonth

1 based month of year. Jan=1, to 12=Dec, NOT TIMEMONTH_TYPE. TIMEUNIT_Month

◆ m_wSecond

TIMEUNIT_t Gray::cTimeUnits::m_wSecond

0 to 59

◆ m_wYear

TIMEUNIT_t Gray::cTimeUnits::m_wYear

Year valid for 1980 to 2043 at least. TIMEUNIT_Year.

◆ sm_DateSeparator

GChar_t Gray::cTimeUnits::sm_DateSeparator = '/'
static

date separator to use for string creation = '\', '-', '.', but Time is always ':'

might be . for Germans,

◆ sm_time24Mode

bool Gray::cTimeUnits::sm_time24Mode = false
static

Display time in 24 hour format. default = false.


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