Bug 29035 - Output of mktime with latest gcc and glibc differs with their older version when is_dst flag set
Summary: Output of mktime with latest gcc and glibc differs with their older version w...
Status: UNCONFIRMED
Alias: None
Product: glibc
Classification: Unclassified
Component: time (show other bugs)
Version: 2.31
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-04-07 09:56 UTC by Shubham Jangam
Modified: 2022-04-20 04:36 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:


Attachments
Screenshot for different outputs (44.18 KB, image/png)
2022-04-11 06:44 UTC, Shubham Jangam
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Shubham Jangam 2022-04-07 09:56:30 UTC
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
Comment 1 Andreas Schwab 2022-04-07 10:13:23 UTC
Which timezone?
Comment 2 Shubham Jangam 2022-04-07 14:39:33 UTC
IST +0530

But, Mostly this will be same even with UTC +0000.
Comment 3 Andreas Schwab 2022-04-07 14:53:39 UTC
I cannot reproduce that with 2.35.
Comment 4 Shubham Jangam 2022-04-11 06:44:02 UTC
Created attachment 14056 [details]
Screenshot for different outputs
Comment 5 Shubham Jangam 2022-04-11 06:45:50 UTC
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,
Comment 6 Shubham Jangam 2022-04-11 06:50:39 UTC
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,
Comment 7 Shubham Jangam 2022-04-13 07:48:40 UTC
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;
}
Comment 8 Shubham Jangam 2022-04-20 04:36:37 UTC
Hi Andreas,
Was you able to reproduce the issue and confirm?

Thank you for your time and efforts,