我写了一个类Instant
管理与时区和一些夏令时算法(Europe
和USA
)有关的日期和时间。 到目前为止,我让这个类的用户指定DST算法作为默认值Europe
。但现在我想自动检测它的默认值。检测夏令时算法
这是我第一次实施。它似乎在我的Windows 7工作站(编译器:英特尔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
}
这样做的好方法是什么?
“这样做的好方法是什么?”使用图书馆!!!!!严重的时区不是你想要做的事情。 – Mat
是的,我想要。这个问题呢? – Caduchon
给一个理智的理由。 –