I wrote a class Instant
managing date and time with respect to timezones and some daylight saving time algorithms (Europe
and USA
).
Until now, I let the user of the class specify the DST algorithm with Europe
as default value. But now I want to detect it automatically for the default value.
This is my first implementation. It seems working on my Windows 7 workstation (compiler: intel 14.0) (I juste have to clarify the list), but it doesn't work on Linux openSUSE (compiler: gcc 4.8.3) because tz.tz_dsttime
is always 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
}
What is the good way to do that ?