Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / Objective C

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 101.8K   1.6K   29   12
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 
If it is not thread safe, which part of the function is not thread safe?

thanks

Sonork 100.41263:Anthony_Yio

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 

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.