Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sat, 23 Mar 2024 19:33:16 +0000
From: Alexander Weps <exander77@...me>
To: musl@...ts.openwall.com
Cc: Markus Wichmann <nullplan@....net>
Subject: Re: Broken mktime calculations when crossing DST boundary

What is the reliable way to get start of this day and similar?

Seems there is just no way if you say tm_isdst = -1 is not a proper way?

For cron calculations I have to move between seconds, minutes, hours, days, months etc. reliably.

AW

On Saturday, March 23rd, 2024 at 19:57, Alexander Weps <exander77@...me> wrote:

> So, in the meantime, I was debugging with not setting tm_isdst = -1;
>
> This causes pretty annoying behavior:
>
> before: 2010-10-31 14:00:00
>
> tm_sec: 0
> tm_min: 0
> tm_hour: 14
> tm_mday: 31
> tm_mon: 9
> tm_year: 110
> tm_wday: 0
> tm_yday: 0
> tm_isdst: 0
> tm_gmtoff: 3600
> tm_zone: CET
>
> tm->tm_hour = 0; <-- reset hour field
>
> mktime(&tm);
>
> after: 2010-10-31 01:00:00 CEST <-- 10:00:00 instead of 00:00:00
> tm_sec: 0
> tm_min: 0
> tm_hour: 1
> tm_mday: 31
> tm_mon: 9
> tm_year: 110
> tm_wday: 0
> tm_yday: 303
> tm_isdst: 1
> tm_gmtoff: 7200
> tm_zone: CEST
>
> tm->tm_hour = 0;
>
> mktime(&tm);
>
> after: 2010-10-31 00:00:00 CEST <-- second run gives a correct value
> tm_sec: 0
> tm_min: 0
> tm_hour: 0
> tm_mday: 31
> tm_mon: 9
> tm_year: 110
> tm_wday: 0
> tm_yday: 303
> tm_isdst: 1
> tm_gmtoff: 7200
> tm_zone: CEST
>
> This basically means that setting field twice produces different value each time:
>
> AW
>
>
>
>
> On Saturday, March 23rd, 2024 at 17:54, Alexander Weps exander77@...me wrote:
>
> > One of the main purposes of struct tm is to calculate date and time, by adding and substracting it's fields.
> >
> > > mktime cannot tell whether your non-normalized input was the result of
> > > you starting with 01:00:02 and adding 1 hour (in which case, our
> > > output does not reflect your intent) or of you starting with 3:00:02
> > > and subtracting 1 hour (in which case, our output does reflect your
> > > intent).
> >
> > We are not adding hours here, your example is completely unrelated.
> >
> > We are adding or subtracting minutes that changes hours.
> >
> > tm_sec: 2
> > tm_min: 60
> > tm_hour: 1
> >
> > vs
> >
> > tm_sec: 2
> > tm_min: 0
> > tm_hour: 2
> >
> > And:
> >
> > tm_sec: 2
> > tm_min: 59
> > tm_hour: 1
> >
> > vs
> >
> > tm_sec: 2
> > tm_min: -1
> > tm_hour: 2
> >
> > AW
> >
> > On Saturday, March 23rd, 2024 at 16:31, Rich Felker dalias@...c.org wrote:
> >
> > > On Sat, Mar 23, 2024 at 01:49:48PM +0000, Alexander Weps wrote:
> > >
> > > > I don't think time can go backwards by incrementing field under any conditions.
> > > >
> > > > Going from:
> > > > tm_sec: 2
> > > > tm_min: 60
> > > > tm_hour: 1
> > > > tm_mday: 31
> > > > tm_mon: 2
> > > > tm_year: 124
> > > > tm_wday: 0
> > > > tm_yday: 90
> > > > tm_isdst: -1
> > > >
> > > > To:
> >
> > tm_sec: 1
> > tm_min: 59
> > tm_hour: 2
> >
> > > > tm_sec: 2
> > > > tm_min: 0
> > > > tm_hour: 1
> > > > tm_mday: 31
> > > > tm_mon: 2
> > > > tm_year: 124
> > > > tm_wday: 0
> > > > tm_yday: 90
> > > > tm_isdst: 0
> > > >
> > > > Seems to be plain wrong. I cannot come up with any argument for this
> > > > being correct under any conditions.
> > >
> > > The above broke-down time is 2:00:02, which does not exist on that day
> > > as a normalized time. If interpreted as non-DST, it would be just a
> > > couple seconds past the end of non-DST (1:59:59.99999..). If
> > > interpreted as DST, it would be just under an hour before the start of
> > > DST (3:00:00), which, after normalization, is 1:00:02 non-DST.
> > >
> > > mktime cannot tell whether your non-normalized input was the result of
> > > you starting with 01:00:02 and adding 1 hour (in which case, our
> > > output does not reflect your intent) or of you starting with 3:00:02
> > > and subtracting 1 hour (in which case, our output does reflect your
> > > intent).
> > >
> > > > mktime was given a struct tm with uncertain STD/DST, it deduced it
> > > > is STD and then thrown away 60 minute information. The minutes got
> > > > reset from 60 to 0 and no other change was done.
> > >
> > > It did not deduce it was STD. It deduced it was non-normalized DST
> > > rather than non-normalized STD (this is an arbitrary choice), then
> > > normalized it and got STD.
> > >
> > > Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.