I think that no matter what any particular implementation might do, we are
relying on undefined behavior which is at best unsafe.  I wonder if maybe
we shouldn't consider initializing the entire structure (and all others?)
after it is allocated.  The performance price feels small and the safety
guarantee feels large.

On Tue, Apr 20, 2021 at 1:24 PM Lars Munch <l...@segv.dk> wrote:

> Sorry for the noise. In my previous example I had by accident pasted a
> huge number into the example. New example:
>
> #include <stdio.h>
> #include <time.h>
> #include <stdlib.h>
>
> int main()
> {
>     struct tm t;
>     time_t now = time((void*)0);
>     localtime_r(&now, &t);
>     setenv("TZ", "UTC", 1);
>     t.tm_isdst = 1;
>     printf("%ld\n", mktime(&t));
>     t.tm_isdst = 0;
>     printf("%ld\n", mktime(&t));
>     t.tm_isdst = -1;
>     printf("%ld\n", mktime(&t));
> }
>
> Gives:
> -1
> 1618956512
> 1618956512
>
> This is on my up-to-date Arch Linux and Ubuntu 20.04.
> On my Ubuntu 18.04 it's fine.
>
>
> On Tue, Apr 20, 2021 at 5:55 PM Lars Munch <l...@segv.dk> wrote:
>
>> Ok, so mktime is forced to return in time UTC time scale earlier in the
>> code:
>>
>> #include <stdio.h>
>> #include <time.h>
>> #include <stdlib.h>
>>
>> int main()
>> {
>>     struct tm t;
>>     time_t now = time((void*)0);
>>     localtime_r(&now, &t);
>>     setenv("TZ", "UTC", 1);
>>     t.tm_isdst = 4000000001;
>>     printf("%ld\n", mktime(&t));
>>     t.tm_isdst = 0;
>>     printf("%ld\n", mktime(&t));
>>     t.tm_isdst = -1;
>>     printf("%ld\n", mktime(&t));
>> }
>>
>> 1618941220
>> 1618941220
>> 1618941220
>>
>> So my problem might come from the fact that I do not have any timezone
>> database on my system.
>>
>>
>>
>>
>> On Tue, Apr 20, 2021 at 5:30 PM Lars Munch <l...@segv.dk> wrote:
>>
>>> Indeed, any positive random number in tm.tm_isdst will set DST in effect
>>> it seems.
>>>
>>> On my stripped down ARM system with no timezone information, it will
>>> simply return -1.
>>>
>>> On Tue, Apr 20, 2021 at 4:26 PM Michael Galassi <mich...@galassi.us>
>>> wrote:
>>>
>>>> Somewhere lies a bug.  The following code snippet:
>>>> #include <stdio.h>
>>>> #include <time.h>
>>>> int main()
>>>> {
>>>>     struct tm t;
>>>>     time_t now = time((void*)0);
>>>>     localtime_r(&now, &t);
>>>>     t.tm_isdst = 1;
>>>>     printf("%ld\n", mktime(&t));
>>>>     t.tm_isdst = 0;
>>>>     printf("%ld\n", mktime(&t));
>>>>     t.tm_isdst = -1;
>>>>     printf("%ld\n", mktime(&t));
>>>> }
>>>> Yields:
>>>> 1618928503
>>>> 1618932103
>>>> 1618932103
>>>> This is true on FreeBSD 13 and Ubuntu 18.04, both fully patched.
>>>>
>>>> -michael
>>>>
>>>> On Tue, Apr 20, 2021 at 7:04 AM Richard Cochran <
>>>> richardcoch...@gmail.com> wrote:
>>>>
>>>>> On Tue, Apr 20, 2021 at 11:44:06AM +0200, Lars Munch wrote:
>>>>> > tm_isdst needs to be initialized to make sure mktime does not fail
>>>>> > or calculates the wrong time.
>>>>>
>>>>> No, take a look at the mktime(3) man page.  There you will read the
>>>>> following.
>>>>>
>>>>>        The  mktime() function modifies the fields of the tm structure
>>>>> as fol‐
>>>>>        lows: tm_wday and tm_yday are set to values determined from
>>>>> the  con‐
>>>>>        tents  of  the  other  fields;  if structure members are
>>>>> outside their
>>>>>        valid interval, they will be normalized (so that, for example,
>>>>> 40  Oc‐
>>>>>        tober  is changed into 9 November); tm_isdst is set (regardless
>>>>> of its
>>>>>        initial value) to a positive value or to 0, respectively, to
>>>>> indicate
>>>>>        whether DST is or is not in effect at the specified time.
>>>>>
>>>>> Thanks,
>>>>> Richard
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Linuxptp-devel mailing list
>>>>> Linuxptp-devel@lists.sourceforge.net
>>>>> https://lists.sourceforge.net/lists/listinfo/linuxptp-devel
>>>>>
>>>>
_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to