Click here to Skip to main content
15,666,974 members
Articles / Programming Languages / Objective C
Article
Posted 6 Sep 2002

Stats

100.4K views
1.6K downloads
29 bookmarked

A Few Classes to Work With Date, Time, Datetime and Timezone Data

Rate me:
Please Sign up or sign in to vote.
4.07/5 (12 votes)
9 Oct 20021 min read
Classes to make working with time and date data in different timezones easier.

Introduction

A few days ago, this became necessary because of working hard with date, time and timezone information. The main task is to convert date from one local time to another. Trying to find something useful on the net failed, except for finding a small part of an SFL library. After some cutting and pasting their code, I created a few small classes where everything I need can be found. So, that's the history.

There are only five classes, fully describing the topic. They are:

_time_t time is stored as long value in human readable form HHMMSSCC, where H = hours, m = minutes, s = seconds, c = centiseconds
_date_t date stored as CCYYMMDD, where C = century, Y = year, M = month, D = day
_datetime_t multiple inherit from _date_t and _time_t
_tzinfo timezone information, just as TIME_ZONE_INFORMATION, but has no name attributes
_tzlist_t the main goal of this class is to obtain timezone information from registry

Selecting the internal format of the _time_t and _date_t is the most successful route I can imagine. Small memory requirements, human readability (under debugger), simple comparison, wide range, high resolution (1 cs) made it an ideal solution for business applications.

All classes have a set of overloaded operators which made working with it more comfortable. So, take a look...

C++
//local time
_datetime_t dt = _datetime_t::now(); 
cout << _T("now: ") << dt.fmt() << endl;

//add two hour, seven minutes
cout << _T("two hours and seven minutes later: ") << (
    dt + _time_t(2,7,0)).fmt() 
     << endl << endl;

//comparing
if ( dt.time() > _time_t(12,0,0))
{
    //before 12:00:00
}
else
{
    //after 
}

//assigning
dt.set_date(1,2,2001); //2/1/2001
dt.set_time(12,0,0); //12:00:00
cout << _T("create datetime at 4/1/2001, 14:00:00") << endl;
cout << dt.set(14,0,0,1,4,2001).fmt() << endl;

//few static function to get current time, date info
cout << _T("local time : ") << _time_t::now().fmt() << endl;
cout << _T("gmt time   : ") << _time_t::gmt_now().fmt() << endl;

cout << _T("local date : ") << _date_t::now().fmt() << endl;
cout << _T("gmt date   : ") << _date_t::now().fmt() << endl;

cout << _T("local dateime : ") << _datetime_t::now().fmt() << endl;
cout << _T("gmt datetime  : ") << (_datetime_t::gmt_now()).fmt() << endl << 
    endl;

//create timezone information object
_tzinfo_t tzi = _tzinfo_t::current();

cout << _T("convert local datetime to gmt using tzi object") << endl;
cout << _T("gmt dt   : ") << tzi.to_gmt(_datetime_t::now()).fmt() << endl <<
    endl;

//now let's do what this classes is created for - converting from one local
//time to another for example convert local afganistan time to local russian 
//time and china

cout << endl << _T("Converting from one local time to another") << endl << 
    endl;
_tzlist_t tzlist;

_tzinfo_t tziAfganistan;
_tzinfo_t tziRussian;
_tzinfo_t tziChina;

if (os_version().is_winNT())
{
    tziAfganistan = tzlist.get(_T("Afghanistan Standard Time"));
    tziRussian = tzlist.get(_T("Russian Standard Time"));
    tziChina = tzlist.get(_T("China Standard Time"));
}
else
if (os_version().is_win9x())
{
    tziAfganistan = tzlist.get(_T("Afghanistan"));
    tziRussian = tzlist.get(_T("Russian"));
    tziChina = tzlist.get(_T("China"));
}
else
{
    cout << _T("this is Win 3.1, fail to get timezone info") << endl;
    return 1;
}
//set local afganistan time - 12:00:00, 2/1/2001
_datetime_t dtAfganistan(22,0,0,28,2,2004);
    
//convert to gmt
_datetime_t dtGmt = tziAfganistan.to_gmt(dtAfganistan);
    
//convert it to other local times 
_datetime_t dtRussian = tziRussian.to_local(dtGmt);
_datetime_t dtChina = tziChina.to_local(dtGmt);

cout << _T("afganistan time  : ") << dtAfganistan.fmt() << endl;
cout << _T("gmt time         : ") << dtGmt.fmt() << endl;
cout << _T("russian time     : ") << dtRussian.fmt() << endl;
cout << _T("china time       : ") << dtChina.fmt() << endl;

Hope that somebody finds it useful for their projects.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralLicense question Pin
msvenx17-Aug-07 7:26
msvenx17-Aug-07 7:26 
GeneralRe: License question Pin
Alex Mol17-Aug-07 21:01
Alex Mol17-Aug-07 21:01 
GeneralThread safe Pin
Anthony_Yio15-Oct-04 1:31
Anthony_Yio15-Oct-04 1:31 
GeneralRe: Thread safe Pin
Anthony_Yio15-Oct-04 1:35
Anthony_Yio15-Oct-04 1:35 
GeneralRe: Thread safe Pin
Alex Mol16-Oct-04 2:23
Alex Mol16-Oct-04 2:23 
Questionhow to get the name of the current time zone? Pin
Patrick Hoffmann17-Oct-03 6:32
Patrick Hoffmann17-Oct-03 6:32 
GeneralNot closing Registry handles...Fix Pin
yughfdjfg25-Feb-03 6:14
yughfdjfg25-Feb-03 6:14 
GeneralRe: Not closing Registry handles...Fix Pin
Alex Mol25-Feb-03 9:04
Alex Mol25-Feb-03 9:04 
GeneralA small improvement Pin
WoutL7-Jan-03 21:50
WoutL7-Jan-03 21:50 
GeneralISO 8601 Pin
Albert van Peppen9-Sep-02 5:09
professionalAlbert van Peppen9-Sep-02 5:09 
GeneralRe: ISO 8601 Pin
Axman22-Nov-04 6:28
Axman22-Nov-04 6:28 
GeneralRe: ISO 8601 Pin
Albert van Peppen23-Nov-04 5:53
professionalAlbert van Peppen23-Nov-04 5:53 
Cool | :cool:

Yeah, did it rather simple.. Good enough for me that is Blush | :O

Here you have the main code for it; converting the other way around is as easy; you figer that out yourself.. Good practice for some.. I think Smile | :)

<br />
<br />
typedef enum {<br />
	D4WDATE_DD_MM_CCYY = 0,		// Default<br />
	D4WDATE_CCYYMMDD,<br />
	D4WDATE_ISO8601,		// CCYY-DD-MMTHH:MM:SSZ+1:00<br />
	D4WDATE_ISO8601_SHORT		// CCYY-DD-MMTHH:MM  (Short version for M$ BizzTalk...)<br />
} D4WDATE_FORMAT;<br />
<br />
LPCTSTR D4WGetFormat(D4WDATE_FORMAT fmtFormat)<br />
{<br />
static char szTemp[128];  // I use a class and a member var of type CString.. This'll work however ;)<br />
	switch (fmtFormat) {<br />
		case D4WDATE_ISO8601:				// CCYY-DD-MMTHH:MM:SSZ+1:00<br />
			_stprintf(szTemp, _T("%%Y%c%%m%c%%dT%%H:%%M:%%SZ+1:00"), CH_DATE_SEPERATOR, CH_DATE_SEPERATOR);<br />
			break;<br />
		case D4WDATE_ISO8601_SHORT:		// CCYY-DD-MMTHH:MM<br />
			_stprintf(szTemp, _T("%%Y%c%%m%c%%dT%%H:%%M"), CH_DATE_SEPERATOR, CH_DATE_SEPERATOR);<br />
			break;<br />
		case D4WDATE_CCYYMMDD:				// CCYY-MM-DD<br />
			_stprintf(szTemp, _T("%%Y%c%%m%c%%d"), CH_DATE_SEPERATOR, CH_DATE_SEPERATOR);<br />
			break;<br />
		default:<br />
//		case D4WDATE_DD_MM_CCYY:			// Default: DD-MM-CCYY<br />
			_stprintf(szTemp, _T("%%d%c%%m%c%%Y"), CH_DATE_SEPERATOR, CH_DATE_SEPERATOR);<br />
			break;<br />
	}<br />
	return szTemp;<br />
}<br />
<br />
.... your code ....<br />
COleDateTime ctSourceDate;   // This one needs to be filled before usage ;)<br />
ctSourceDate = ...;<br />
CString strTargetDate;       // This one gets filled afterward<br />
D4WDATE_FORMAT fmtDate = D4WDATE_ISO8601;     // What format to use?<br />
strTargetDate = ctSrc.Format(D4WGetFormat(fmtDate));<br />
....<br />


Since the code is part of a large date-time handling class i've just extracted the important part Smile | :)

Greetings.

Albert van Peppen

Cool | :cool:

PS When i have time (should be around the next century Cry | :(( ) I might post more about this class Smile | :)

EDIT: Maybe as an extension for A Simple Class To Handle Dates - By Richard Stringer

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.