Click here to Skip to main content
Click here to Skip to main content

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

By , 9 Oct 2002
Rate this:
Please Sign up or sign in to vote.

Introduction

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_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 have no name attributes

_tzlist_t

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;

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

About the Author

Alex Mol
Web Developer
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
GeneralRe: ISO 8601 PinmemberAxman22-Nov-04 6:28 
GeneralRe: ISO 8601 PinmemberAlbert van Peppen23-Nov-04 5:53 

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

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

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