Skip to content

Instantly share code, notes, and snippets.

@fmfdias
Created March 28, 2015 00:35
Show glibc mktime unstable results when timezone changed on Europe/Lisbon and Europe/Istanbul. Europe/Amsterdam is an example where this unstable behaviour is not happening
#include <stdio.h>
#include <time.h>
#include <errno.h>
int main(int argc, const char * argv[]) {
time_t ret, ret2, ret3;
struct tm info, info2, info3;
info.tm_year = 40;
info.tm_mon = 5 - 1;
info.tm_mday = 16;
info.tm_hour = 0;
info.tm_min = 0;
info.tm_sec = 0;
info.tm_isdst = -1;
info.tm_wday = -1;
info.tm_gmtoff = 0;
info.tm_zone = 0;
info2.tm_year = 40;
info2.tm_mon = 1 - 1;
info2.tm_mday = 1;
info2.tm_hour = 0;
info2.tm_min = 0;
info2.tm_sec = 0;
info2.tm_isdst = -1;
info2.tm_wday = -1;
info2.tm_gmtoff = 0;
info2.tm_zone = 0;
info3.tm_year = 40;
info3.tm_mon = 5 - 1;
info3.tm_mday = 16;
info3.tm_hour = 0;
info3.tm_min = 0;
info3.tm_sec = 0;
info3.tm_isdst = -1;
info3.tm_wday = -1;
info3.tm_gmtoff = 0;
info3.tm_zone = 0;
ret = mktime(&info);
ret2 = mktime(&info2);
ret3 = mktime(&info3);
if( ret == -1 )
{
printf("Error: unable to make time using mktime\n");
}
else
{
char buff[70];
printf("-------\n");
printf("%s", asctime(&info));
strftime(buff, sizeof buff, "%A %c %z", &info);
puts(buff);
printf("-------\n");
printf("%s", asctime(&info2));
strftime(buff, sizeof buff, "%A %c %z", &info2);
puts(buff);
printf("-------\n");
printf("%s", asctime(&info3));
strftime(buff, sizeof buff, "%A %c %z", &info3);
puts(buff);
printf("-------\n");
}
return(0);
}
#include <stdio.h>
#include <time.h>
#include <errno.h>
int main(int argc, const char * argv[]) {
time_t ret, ret2, ret3;
struct tm info, info2, info3;
info.tm_year = 10;
info.tm_mon = 10 - 1;
info.tm_mday = 1;
info.tm_hour = 0;
info.tm_min = 0;
info.tm_sec = 0;
info.tm_isdst = -1;
info.tm_wday = -1;
info.tm_gmtoff = 0;
info.tm_zone = 0;
info2.tm_year = 10;
info2.tm_mon = 1 - 1;
info2.tm_mday = 1;
info2.tm_hour = 0;
info2.tm_min = 0;
info2.tm_sec = 0;
info2.tm_isdst = -1;
info2.tm_wday = -1;
info2.tm_gmtoff = 0;
info2.tm_zone = 0;
info3.tm_year = 10;
info3.tm_mon = 10 - 1;
info3.tm_mday = 1;
info3.tm_hour = 0;
info3.tm_min = 0;
info3.tm_sec = 0;
info3.tm_isdst = -1;
info3.tm_wday = -1;
info3.tm_gmtoff = 0;
info3.tm_zone = 0;
ret = mktime(&info);
ret2 = mktime(&info2);
ret3 = mktime(&info3);
if( ret == -1 )
{
printf("Error: unable to make time using mktime\n");
}
else
{
char buff[70];
printf("-------\n");
printf("%s", asctime(&info));
strftime(buff, sizeof buff, "%A %c %z", &info);
puts(buff);
printf("-------\n");
printf("%s", asctime(&info2));
strftime(buff, sizeof buff, "%A %c %z", &info2);
puts(buff);
printf("-------\n");
printf("%s", asctime(&info3));
strftime(buff, sizeof buff, "%A %c %z", &info3);
puts(buff);
printf("-------\n");
}
return(0);
}
#include <stdio.h>
#include <time.h>
#include <errno.h>
int main(int argc, const char * argv[]) {
time_t ret, ret2, ret3;
struct tm info, info2, info3;
info.tm_year = 12;
info.tm_mon = 1 - 1;
info.tm_mday = 1;
info.tm_hour = 0;
info.tm_min = 0;
info.tm_sec = 0;
info.tm_isdst = -1;
info.tm_wday = -1;
info.tm_gmtoff = 0;
info.tm_zone = 0;
info2.tm_year = 11;
info2.tm_mon = 1 - 1;
info2.tm_mday = 1;
info2.tm_hour = 0;
info2.tm_min = 0;
info2.tm_sec = 0;
info2.tm_isdst = -1;
info2.tm_wday = -1;
info2.tm_gmtoff = 0;
info2.tm_zone = 0;
info3.tm_year = 12;
info3.tm_mon = 1 - 1;
info3.tm_mday = 1;
info3.tm_hour = 0;
info3.tm_min = 0;
info3.tm_sec = 0;
info3.tm_isdst = -1;
info3.tm_wday = -1;
info3.tm_gmtoff = 0;
info3.tm_zone = 0;
ret = mktime(&info);
ret2 = mktime(&info2);
ret3 = mktime(&info3);
if( ret == -1 )
{
printf("Error: unable to make time using mktime\n");
}
else
{
char buff[70];
printf("-------\n");
printf("%s", asctime(&info));
strftime(buff, sizeof buff, "%A %c %z", &info);
puts(buff);
printf("-------\n");
printf("%s", asctime(&info2));
strftime(buff, sizeof buff, "%A %c %z", &info2);
puts(buff);
printf("-------\n");
printf("%s", asctime(&info3));
strftime(buff, sizeof buff, "%A %c %z", &info3);
puts(buff);
printf("-------\n");
}
return(0);
}
Copy link

ghost commented Jan 4, 2018

to avoid this unstability problem, first init the tm structure with zeroes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment