[tz] Timezone change detection
Guy Harris
gharris at sonic.net
Fri Sep 3 23:05:31 UTC 2021
On Sep 3, 2021, at 3:43 PM, Tom Lane via tz <tz at iana.org> wrote:
> I think the odds of this breaking stuff are significantly larger
> than the odds of it doing useful stuff. I find it particularly
> frightening that the patch doesn't appear to allow the calling
> application to have any say in the matter.
Note that, in Darwin, and thus, in macOS/iOS/iPadOS/etc.:
1) there is a notification mechanism by which the code in the OS that adjusts the time zone if the machine (from an iPhone to a MacBook*) crosses tzdb region boundaries can tell all code that uses the time zone information that the time zone has changed;
2) the tzdb-reference-code-based code in libSystem registers for those notifications and reloads the time zone information when the time zone changes;
so that the following program, when compiled and run on a Mac:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int
main(void)
{
time_t now;
for (;;) {
now = time(NULL);
printf("%s", ctime(&now));
sleep(5);
}
return 0;
}
can produce output such as
Fri Sep 3 15:55:20 2021
Fri Sep 3 15:55:25 2021
Fri Sep 3 15:55:30 2021
Fri Sep 3 15:55:35 2021
Fri Sep 3 15:55:40 2021
Fri Sep 3 15:55:45 2021
Fri Sep 3 15:55:50 2021
Fri Sep 3 15:55:55 2021
Fri Sep 3 15:56:00 2021
Fri Sep 3 15:56:05 2021
Fri Sep 3 17:56:10 2021
Fri Sep 3 17:56:15 2021
Fri Sep 3 15:56:20 2021
Fri Sep 3 15:56:25 2021
if the time zone changes, and
3) the only opt-out mechanism I know of would be to somehow determine the tzdb region you want to stay in forever (note that TZ doesn't reflect it - it's not set by default, so /etc/localtime is used, so if you want to stay in the current tzdb region, you'd have to do a readlink() on /etc/localtime), do a setenv() to set TZ to that, and then call tzset().
(In that example, I turned off the automatic time zone picker, manually changed to a different time zone, and then turned the automatic time zone picker back on, as I'm not close enough to a time zone boundary to be able to test it using only the picker. But the same thing will happen if, for example, you get on a plane with your MacBook* or iPhone or iPad, cross a time zone boundary, and then look at the time in the airport where you arrive - I know this because it *did* happen to me several years ago.)
More information about the tz
mailing list