April 20, 2021

Software Mile.com

Latest Software News and Hacks

Manual time_t Values and Time Zones

The 16-hour puzzle presented in last week’s Lesson has been resolved: It was actually a 24-hour puzzle, minus 8 hours for my time zone, which equals 16 hours. But what can be done about the time zone difference?

Time zone information is part of the computer system’s locale details. The locale defines aspects about the system based on your location, language, monetary and numeric representations, and other localized details. One of these details is the time zone.

Computer clocks are based on Greenwich Mean Time (GMT), also known as Coordinated Universal Time (UTC). The local time is set relative to GMT based on your time zone. I’m in the Pacific time zone, which -8 hours from GMT and referred to as GMT-8. Rome is GMT+1, the Central European time zone.

To calculate a proper time_t value, it helps to either take into consideration your time zone or adjust the time zone to GMT while the code runs.

In the C language, the locale() function deals with reporting or changing locale details, such as the currency symbol or date format. Alas, time zone information isn’t accessible by using the locale() function.

In Unix and Linux, time zone details are kept in the /etc/localtime file, which is often an alias to the actual time zone file. For example, on my computers, the alias is:

/etc/localtime -> /var/db/timezone/zoneinfo/America/Los_Angeles

I’m sure you could devise code that examines the /etc/localtime file and perform your time zone calculations based on the results. An easier way, however, is to use the tzset() function.

The tzset() function, defined in the time.h header file, takes no arguments and returns no values. Instead, it relies upon the TZ environment variable to set the time zone. You can use the putenv() function to set TZ‘s value as the program runs. The format is:


Where zone is a time zone string such as GMT-8 or GMT+1 or just GMT for Greenwich Mean time.

To calculate a time_t value appropriate to GMT, losing the -8 hour difference from my previous, manual time_t calculations, code such as the following is used:


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

int main()
    time_t epoch = 0;

    printf("Time: %s",ctime(&epoch));


Line 8 sets the value TZ=GMT into the environment. This change holds only as long as the program runs; modifications to the environment aren’t retained. With the TZ variable established, the tzset() function at Line 10 recalibrates the current time zone based on the value of TZ.

When the ctime() function reports the value of time_t variable epoch at Line 11, the value is relative to GMT, not the local time zone. Here is the output:

Time: Thu Jan  1 00:00:00 1970

From last week’s Lesson, the output from similar code without the time zone adjustment was:

Time: Wed Dec 31 16:00:00 1969

The 8 hour difference, which I misinterpreted as 16 hours in an earlier Lesson, is now accounted for.

Go to Source of this post
Author Of this post: dgookin
Title Of post: Manual time_t Values and Time Zones
Author Link: {authorlink}