public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Max Nikulin <manikulin@gmail.com>
To: libc-alpha@sourceware.org
Subject: Re: mktime result may depend on previous calls
Date: Thu, 19 Jan 2023 14:10:10 +0700	[thread overview]
Message-ID: <tqaqci$tb2$1@ciao.gmane.io> (raw)
In-Reply-To: <040c6e3a-e65d-972e-d644-f363c0536cc5@cs.ucla.edu>

On 19/01/2023 11:34, Paul Eggert wrote:
> On 1/18/23 19:00, Max Nikulin wrote:
> 
>> Notice that in the case of ambiguous local time (around a transition 
>> with backward step) multiple mktime calls with the same argument may 
>> return both values. Such kind of uncertainty appears if tm_isdst is 
>> set to -1 or if local time changes keeping the same DST state.
> 
> The above wording isn't quite right, as it ignores the role of tm_gmtoff 
> in mktime. (Did I say it was complicated? :-)
...
> POSIX does allow that sort of thing as an extension, and glibc mktime 
> does so: it disambiguates via tm_gmtoff.

Has it been fixed recently?

https://www.gnu.org/software/libc/manual/html_node/Broken_002ddown-Time.html#index-mktime

> The mktime function *ignores* the specified contents of the tm_wday,
> tm_yday, *tm_gmtoff*, and tm_zone members of the broken-down time structure

 From the example below I would expect 1612128600 +0200 CAT gmtoff=7200 
on third line, but my glibc is dated

      1612119600 2021-01-31 22:00:00 +0300 EAT gmtoff=10800
      1612125000 2021-01-31 23:30:00 +0300 EAT gmtoff=10800
      1612125000 2021-01-31 23:30:00 +0300 EAT gmtoff=10800


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void bt_init(struct tm *bt) {
	memset(bt, 0, sizeof(*bt));
	bt->tm_isdst = -1;
	bt->tm_year = 2021 - 1900;
	bt->tm_mon = 1 - 1;
	bt->tm_mday = 31;
	bt->tm_hour = 23;
	bt->tm_min = 30;
	bt->tm_sec = 0;
}

void bt_print(struct tm *bt) {
	char buf[64];
	time_t ts = mktime(bt);
	strftime(buf, sizeof(buf) - 1, "%F %T %z %Z", bt);
	printf("%15zd %s gmtoff=%ld\n", ts, buf, bt->tm_gmtoff);
}

int main() {
	struct tm bt;
	putenv("TZ=Africa/Juba");
	// Fix cached offset to the value before time jump
	bt_init(&bt);
	bt.tm_hour = 22;
	bt.tm_min = 0;
	// Default conversion
	bt_print(&bt);
	bt_init(&bt);
	bt_print(&bt);
	// Attempt to choose +0200 CAT instead of +0300 EAT
	bt_init(&bt);
	bt.tm_gmtoff = 2*3600;
	bt_print(&bt);
	return 0;
}


  reply	other threads:[~2023-01-19  7:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-18 15:39 Max Nikulin
2023-01-18 22:12 ` Paul Eggert
2023-01-19  3:00   ` Max Nikulin
2023-01-19  4:34     ` Paul Eggert
2023-01-19  7:10       ` Max Nikulin [this message]
2023-01-20  9:21         ` Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='tqaqci$tb2$1@ciao.gmane.io' \
    --to=manikulin@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).