[tz] Timezone change detection

Guy Harris gharris at sonic.net
Sat Sep 4 02:20:18 UTC 2021


On Sep 3, 2021, at 6:11 PM, Paul Eggert <eggert at cs.ucla.edu> wrote:

> (Does the same thing happen with localtime_r on Darwin? localtime_r is documented by POSIX to not necessarily do a tzset, for all the usual thread-safety and performance reasons. If localtime responds to these notifications but localtime_r does not, this could lead to mysterious behavior.)

Same behavior from

	#include <stdio.h>
	#include <time.h>
	#include <unistd.h>

	int
	main(void)
	{
		time_t now;
		struct tm tmstr, *tm;

		for (;;) {
			now = time(NULL);
			tm = localtime_r(&now, &tmstr);
			printf("%04d-%02d-%02d %02d:%02d:%02d\n",
			    tm->tm_year + 1900,
			    tm->tm_mon + 1,
			    tm->tm_mday,
			    tm->tm_hour,
			    tm->tm_min,
			    tm->tm_sec);
			sleep(5);
		}
		return 0;
	}

Performance shouldn't be an issue, as it doesn't reload - or even check for a change - on every call; it reloads /etc/localtime only if it's notified that the tzdb region changed.

This program does *not* show a tzdb region change if I tweak the time zone:

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

	int
	main(void)
	{
		time_t now;
		struct tm tmstr, *tm;

		setenv("TZ", "America/Los_Angeles", 1);
		for (;;) {
			now = time(NULL);
			tm = localtime_r(&now, &tmstr);
			printf("%04d-%02d-%02d %02d:%02d:%02d\n",
			    tm->tm_year + 1900,
			    tm->tm_mon + 1,
			    tm->tm_mday,
			    tm->tm_hour,
			    tm->tm_min,
			    tm->tm_sec);
			sleep(5);
		}
		return 0;
	}

I suspect the same will be the case if ctime() is used.



More information about the tz mailing list