[tz] [PROPOSED] Draft next POSIX has tm_gmtoff, tm_zone
Paul Gilmartin
PaulGBoulder at AIM.com
Sun Jan 14 16:16:34 UTC 2024
On 1/13/24 16:01:23, Paul Eggert via tz wrote:
> * private.h (TM_GMTOFF, TM_ZONE): Treat draft next POSIX like
> glibc etc., since it requires tm_gmtoff and tm_zone.
> ---
I wrote an exercise testing the effect of changing struct tm
and TZ after local time() and before strftime() and mktime(().
It works as I intended on MacOS and Linux. I'll try to
attach it. Output is:
2024 1705248193 1705248193
2424 14328028993 14328028993
Average days in year = 365.24250
America/New_York is ahead of America/Los_Angeles by 3.00 hours.
I hope the PROPOSED changes don't modify this.
--
gil
-------------- next part --------------
/* A recherché example
o Modifying a field in struct tm after calling localtime()
o Changing TZ before calling mktime().
Both yield the result I expect.
Would these be facilitated or perhaps hindered by
tm_gmtoff and tm_zone?
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void)
{
char buf[256];
time_t t1, t2, t3;
struct tm result;
/* Use localtime_r() to load struct tm. */
#define Z1 "America/Los_Angeles"
putenv( "TZ="Z1 );
time(&t1);
localtime_r( &t1, &result );
strftime(buf, sizeof(buf), "%Y %s", &result );
printf( "%s %12ld\n", buf, mktime( &result ) );
/* The Grtegorian calendar cycle is 499 years, so add 400
years; take the difference in seconds and divide by
seconds/day then by 400 to get average days in year. */
result.tm_year += 400;
strftime(buf, sizeof(buf), "%Y %s", &result );
printf( "%s %12ld\n", buf, mktime( &result ) );
t2 = mktime( &result );
printf( "Average days in year =%10.5f\n",
( t2 - t1 ) / 86400 /400.0 );
/* time difference in seconds between two timezones; convert to hours. */
#define Z3 "America/New_York"
putenv( "TZ="Z3 );
t3 = mktime( &result );
printf( "\n%s is ahead of %s by %5.2f hours.\n",
Z3, Z1, ( t2 - t3 ) /3600.0 );
}
More information about the tz
mailing list