Objective CVisual Studio 6Visual Studio 2005Windows 2000Visual C++ 6.0Windows XP中级开发Visual StudioWindowsC++
处理日期、时间、日期时间以及时区数据的一些类






4.07/5 (10投票s)
2002年9月7日
1分钟阅读

103818

1671
用于更轻松地处理不同时区中的时间和日期数据的类。
引言
几天前,由于处理日期、时间和时区信息,这变得必要。主要任务是将日期从一个本地时间转换为另一个本地时间。试图在网上找到有用的东西失败了,除了找到一个SFL库的一小部分。在剪切和粘贴他们的代码后,我创建了一些小类,其中包含我需要的一切。这就是它的历史。
只有五个类,完全描述了主题。它们是
_time_t | 时间以人类可读的形式存储为长值 HHMMSSCC,其中 H = 小时,m = 分钟,s = 秒,c = 百分秒 |
_date_t | 日期存储为 CCYYMMDD,其中 C = 世纪,Y = 年,M = 月,D = 日 |
_datetime_t | 从 _date_t 和 _time_t 多重继承 |
_tzinfo | 时区信息,就像 TIME_ZONE_INFORMATION 一样,但没有名称属性 |
_tzlist_t | 这个类的主要目标是从注册表中获取 时区 信息 |
选择 _time_t
和 _date_t
的内部格式是我能想象到的最成功的途径。小的内存需求、人类可读性(在调试器下)、简单的比较、宽范围、高分辨率(1 毫秒)使其成为商业应用的理想解决方案。
所有类都有一组重载的运算符,这使得使用起来更加舒适。所以,请看一看...
//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;
希望有人在他们的项目中发现它有用。
许可证
本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。
作者可能使用的许可证列表可以在此处找到。