我寫了一個類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
給一個理智的理由。 –