【发布时间】:2016-06-18 07:24:06
【问题描述】:
我写了一个类 Instant 管理与时区相关的日期和时间以及一些夏令时算法(Europe 和 USA)。
到现在为止,我让类的用户使用Europe 作为默认值来指定 DST 算法。但是现在我想自动检测它的默认值。
这是我的第一个实现。它似乎适用于我的 Windows 7 工作站(编译器:intel 14.0)(我必须澄清一下列表),但它不适用于 Linux openSUSE(编译器:gcc 4.8.3),因为tz.tz_dsttime 始终为 0。
typedef enum {
DST_ALGO_NONE = 0,
DST_ALGO_EUROPE = 1,
DST_ALGO_USA = 2
} TimeZoneType;
TimeZoneType auto_detect_dst_algorithm()
{
# ifdef WIN32
TIME_ZONE_INFORMATION tz;
GetTimeZoneInformation(&tz);
std::wstring tz_wstr = tz.DaylightName;
std::string tz_str(tz_wstr.begin(), tz_wstr.end());
if( tz_str.find("Romance") != std::string::npos
|| tz_str.find("RST") != std::string::npos
|| tz_str.find("Central Europe") != std::string::npos
|| tz_str.find("CEST") != std::string::npos
|| tz_str.find("Middle Europe") != std::string::npos
|| tz_str.find("MET") != std::string::npos
|| tz_str.find("Western Europe") != std::string::npos
|| tz_str.find("WET") != std::string::npos )
{
return DST_ALGO_EUROPE;
}
else if( tz_str.find("Pacific") != std::string::npos
|| tz_str.find("PDT") != std::string::npos )
{
return DST_ALGO_USA;
}
else
{
return DST_ALGO_NONE;
}
# else
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
if(tz.tz_dsttime == 1)
{
return DST_ALGO_USA;
}
else if(tz.tz_dsttime == 3 || tz.tz_dsttime == 4)
{
return DST_ALGO_EUROPE;
}
else
{
return DST_ALGO_NONE;
}
# endif
}
这样做的好方法是什么?
【问题讨论】:
-
“有什么好的方法吗?”使用图书馆!!!!!!说真的时区不是你想自己做的事情。
-
是的,我想要。那问题呢?
-
给出一个理智的理由。
-
既然已经存在很多库,为什么还要创建新库?
-
我写这个库是因为我需要一些在其他库中没有的特定打印功能。它正在工作。我只想找到如何正确初始化它。有人可以提供帮助,或者我需要就我的工作召开会议?...