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 17:36:00 -0700
From: Eric Pruitt <eric.pruitt@...il.com>
To: musl@...ts.openwall.com
Subject: Re: Broken mktime calculations when crossing DST boundary

On Sat, Mar 23, 2024 at 08:40:50PM +0000, Alexander Weps wrote:
> Is there a way, how to reliable get beginning of day etc. without tm_isdst = -1.

I needed to do something similar in a program that involved sunset and
sunrise calculations. Depending on what you're doing and the information
you have, maybe the approach I used could work:

    static time_t round_down_to_midnight(time_t when)
    {
        char buf[20];
        struct tm *in;
        struct tm out;

        when -= when % 60;
        in = localtime(&when);

        if (in->tm_hour == 0 && in->tm_min == 0) {
            return when;
        }

        sprintf(buf, "%d-%02d-%02d 00:00:00", in->tm_year + 1900, in->tm_mon + 1,
            in->tm_mday);
        strptime(buf, "%Y-%m-%d %H:%M:%S", &out);
        return mktime(&out);
    }

The performance of this is not great, though.

Eric

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.