Programming with time zones

Updated: April 19, 2023

Inside a program, you can set the TZ environment variable by calling setenv() or putenv().

For example:

setenv( "TZ", "PST08PDT07,M3.2.0/2,M11.1.0/2", 1 );
putenv( "TZ=PST08PDT07,M3.2.0/2,M11.1.0/2" );

To obtain the value of the variable, use the getenv() function:

char *tzvalue;
…
tzvalue = getenv( "TZ" );

You can get the value of _CS_TIMEZONE by calling confstr(), like this:

confstr( _CS_TIMEZONE, buff, BUFF_SIZE );

or set it like this:

confstr( _CS_SET | _CS_TIMEZONE, "JST-9", 0 );

The tzset() function gets the current value of TZ—or _CS_TIMEZONE if TZ isn't set—and sets the following global variables:

daylight
Indicates if daylight saving time is supported in the locale.
timezone
The number of seconds of time difference between the local time zone and Coordinated Universal Time (UTC).
tzname
A vector of two pointers to character strings containing the standard and daylight time zone names.

Whenever you call ctime(), ctime_r(), localtime(), or mktime(), the library sets tzname, as if you had called tzset(). The same is true if you use the %Z directive when you call strftime().

For more information about these functions and variables, see the QNX Neutrino C Library Reference.