Click here to Skip to main content
13,347,462 members (51,256 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 6 Sep 2002

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

, 9 Oct 2002
Rate this:
Please Sign up or sign in to vote.
Classes to make working with time and date data in different timezones easier.


A few days ago this became necessary because of hard-working 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 is stored as long value in human readable form HHMMSSCC, where H = hours, m = minutes, s = seconds, c = centiseconds


date stored as CCYYMMDD, where C = century, Y = year, M = month, D = day


multiple inherit from _date_t and _time_t


timezone information, just as TIME_ZONE_INFORMATION, but have no name attributes


the main goal of this class is to obtain timezone info 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...

//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;

if ( dt.time() > _time_t(12,0,0))
    //before 12:00:00

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 << 

//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 <<

//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 << 
_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"));
if (os_version().is_win9x())
    tziAfganistan = tzlist.get(_T("Afghanistan"));
    tziRussian = tzlist.get(_T("Russian"));
    tziChina = tzlist.get(_T("China"));
    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.


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


About the Author

Alex Mol
Web Developer
Russian Federation Russian Federation
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralLicense question Pin
msvenx17-Aug-07 8:26
membermsvenx17-Aug-07 8:26 
GeneralRe: License question Pin
Alex Mol17-Aug-07 22:01
memberAlex Mol17-Aug-07 22:01 
GeneralThread safe Pin
Anthony_Yio15-Oct-04 2:31
memberAnthony_Yio15-Oct-04 2:31 
GeneralRe: Thread safe Pin
Anthony_Yio15-Oct-04 2:35
memberAnthony_Yio15-Oct-04 2:35 
GeneralRe: Thread safe Pin
Alex Mol16-Oct-04 3:23
memberAlex Mol16-Oct-04 3:23 
Questionhow to get the name of the current time zone? Pin
Patrick Hoffmann17-Oct-03 7:32
memberPatrick Hoffmann17-Oct-03 7:32 
GeneralNot closing Registry handles...Fix Pin
Joel Rowles25-Feb-03 7:14
sussJoel Rowles25-Feb-03 7:14 
GeneralRe: Not closing Registry handles...Fix Pin
Alex Mol25-Feb-03 10:04
memberAlex Mol25-Feb-03 10:04 
GeneralA small improvement Pin
Wout Louwers7-Jan-03 22:50
memberWout Louwers7-Jan-03 22:50 
GeneralISO 8601 Pin
Albert van Peppen9-Sep-02 6:09
memberAlbert van Peppen9-Sep-02 6:09 
GeneralRe: ISO 8601 Pin
Axman22-Nov-04 7:28
memberAxman22-Nov-04 7:28 
GeneralRe: ISO 8601 Pin
Albert van Peppen23-Nov-04 6:53
memberAlbert van Peppen23-Nov-04 6: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 10 Oct 2002
Article Copyright 2002 by Alex Mol
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid