Hello, (A) There is one discrepancy with the behaviour of mktime() from version GCC 9.4.0 (glibc ver:2.31) and GCC 7.5.0 (glibc ver: 2.27) when used. I have referenced online docs as mentioned in point(C) below to understand this function. This sample(Mentioned in point(B)) tries to get time since the Epoch by giving some random but valid date as input using mktime. (Sample code taken from man page). I run this sample code on 2 different versions of gcc. and observed different outputs when flag for tm_isdst was set. Output(1) with gcc version: 9.4.0 and glibc version: 2.31 o/p: -1 -unknown- Output(2) with gcc version: 7.5.0 and glibc version: 2.27 o/p: 1649203201 Wednesday When this code run on older version of gcc (gcc ver: 7.5.0, glibc ver: 2.31), It outputs correctly. But when I run same code with newer version of gcc (gcc ver: ). It returns erroneous output which indicates some discrepancy in functions behaviour compared as docs states. As per man page, A positive or 0 value for tm_isdst shall cause mktime() to presume initially that Daylight Savings Time, respectively, is or is not in effect for the specified time. Buf if we provide positive value for tm_isdst, function fails with newer version of gcc and glibc. May be this whole behaviour is expected but seems not rightly mentioned on man page. (B) Sample Code used from docs present as per internet given point(C): ``` #include <stdio.h> #include <time.h> char daybuf[20]; int main(void) { time_t tt; struct tm time_str; time_str.tm_year = 2022 - 1900; time_str.tm_mon = 4 - 1; time_str.tm_mday = 6; time_str.tm_hour = 0; time_str.tm_min = 0; time_str.tm_sec = 1; time_str.tm_isdst = 0; tt = mktime(&time_str); printf("%ld ", tt); if(tt == -1) { (void)puts("-unknown-"); } else { (void)strftime(daybuf, sizeof(daybuf), "%A", &time_str); (void)puts(daybuf); } return 0; } ``` (C)Docs referred: https://pubs.opengroup.org/onlinepubs/7908799/xsh/mktime.html https://man7.org/linux/man-pages/man3/mktime.3p.html https://www.ibm.com/docs/de/zvm/7.1?topic=SSB27U_7.1.0/com.ibm.zvm.v710.edclv/mktime.htm
Which timezone?
IST +0530 But, Mostly this will be same even with UTC +0000.
I cannot reproduce that with 2.35.
Created attachment 14056 [details] Screenshot for different outputs
I tried to reproduce this on 2.35, I was successful. I have attached screenshot for run output. For version 2.31, I also checked on UTC timezone, it was able to reproduce there too. Please let me know if I am missing anything. Thanks,
For Info, Output(1) With GLIBC 2.35 and gcc 11.2.0 shubhamj-Standard-PC:~$ sudo timedatectl set-timezone GMT+0 shubhamj-Standard-PC:~$ ./a.out -1 -unknown- shubhamj-Standard-PC:~$ sudo timedatectl set-timezone Etc/UTC shubhamj-Standard-PC:~$ ./a.out -1 -unknown- shubhamj-Standard-PC:~$ sudo timedatectl set-timezone America/Denver shubhamj-Standard-PC:~$ ./a.out 1649224801 Wednesday _________________________________________________________________ Output(2) With GLIBC 2.27 and gcc 7.5.0 root@Shubham:~# set timedatectl set-timezone GMT+0 root@Shubham:~# ./a.out 1649203201 Wednesday root@Shubham:~# set timedatectl set-timezone Etc/UTC root@Shubham:~# ./a.out 1649203201 Wednesday root@Shubham:~# set timedatectl set-timezone America/Denver root@Shubham:~# ./a.out 1649203201 Wednesday *** I hope this makes things clear. Code used: #include <stdio.h> #include <time.h> char daybuf[20]; int main(void) { time_t tt; struct tm time_str; time_str.tm_year = 2022 - 1900; time_str.tm_mon = 4 - 1; time_str.tm_mday = 6; time_str.tm_hour = 0; time_str.tm_min = 0; time_str.tm_sec = 1; time_str.tm_isdst = 1; tt = mktime(&time_str); printf("%ld ", tt); if(tt == -1){ (void)puts("-unknown-"); } else{ (void)strftime(daybuf, sizeof(daybuf), "%A", &time_str); (void)puts(daybuf); } return 0; }
Hi Andreas, Was you able to reproduce the issue and confirm? Thank you for your time and efforts,