mercredi 29 juin 2016

C++ std::localtime daylight saving time rule (dst) european vs american

I've problem with use of the std::localtime function. When I transform a std::time_t to a local struct tm, it always use the american daylight saving time whereas I want to use the european one (France).

UTC : 03/19/16 16:56 is transformed in Local : 03/19/16 18:56.

At this date, normally, local is 17:56 (UTC+1). The DST happens on the 27th in France. After several tests, it seems that the used DST is based on the american rule : DST happens on second sunday in march.

I've also changed the TZ environement variable, but it also fails.


`

if( putenv( "TZ=CET-1CEST-2,M3.5.0/2,M10.5.0/3" ) != 0 ) { 
     std::cout << "Unable to set TZ" << std::endl; 
} else { 
   tz = getenv("TZ"); 
   if (tz) std::cout << tz << std::endl; 
   else std::cout << "TZ not defined" << std::endl; tzset(); 
}

struct std::tm t;
t.tm_sec = 0;
t.tm_min = 56;
t.tm_hour = 18;
t.tm_mday = 19;
t.tm_mon = 3-1;
t.tm_year = 2016-1900;
t.tm_isdst = -1;
std::time_t tt = std::mktime(&t);
std::cout << "UTC: " << std::put_time(std::gmtime(&tt), "%c %Z") << '\n'; // UTC : 03/19/16 16:56
std::cout << "local: " << std::put_time(std::localtime(&tt), "%c %Z") << '\n'; // Local : 03/19/16 18:56 (not waited result)

`

As a precision, I use the bcc32c compiler (the embarcadero C++ clang based computer).

I hope I'm clear enough and you'll be able to help me.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire