The Art of Daylight Savings Time

A month ago, I discussed the strfmt() function. A timely announcement from Microsoft has inspired me to write a column about a related topic, namely accessing the system’s Daylight Saving Time information programmatically. This article shows how to detect whether DST is in effect in the current local time zone.


How can you detect whether DST is in effect in your current local time zone? How do you detect the difference between the local standard time and the local alternate time?


Use the _tzset(), _daylight, _timezone, and _tzname facilities of the standard library.

At a Perfect Time
Before I delve into the C/C++ constructs for handling DST and other timezone related info, a few questions must be answered.

Q: Microsoft’s announcement was sent to Microsoft’s users. Is the said announcement relevant only for Windows users?

A: No, it isn’t. The reported change in the DST scheme affects users of other operating systems and programming languages as well. Furthermore, while the 2007 DST period extension is currently effective in the United States, it’s reasonable that other countries will adopt this scheme to save energy, and to ensure that DST periods in various countries remain in sync. In other words, the new DST scheme doesn’t affect only C/C++ programmers using the Windows operating system?it has more far reaching consequences and scope.

Q: What is the major difference between 2007 and previous years with respect to DST?

A: Beginning in 2007, DST will be extended in the United States. DST started on March 11, 2007, which is three weeks earlier than usual. It will end on November 4, 2007, which is one week later than usual. This results in a new DST period that is four weeks longer than in previous years.

Q: What should I do then?

A: Unless certain updates are applied to your computer, the time zone settings for your computer’s system clock may be incorrect during this four-week period. In particular, you must make sure that both your Windows operating system and your calendar programs are updated. As far as C/C++ developers are concerned, you will have to test existing applications and in some cases, relink them against the new CRT. In addition, hard-coded DST periods must be revised and replaced with dynamic DST detection facilities which I’m about to discuss here.

Time Zones and Daylight Saving
C/C++ implementations store time as Coordinated Universal Time (UTC) by default (UTC replaces the obsolescent term GMT). To access the local timezone information, applications and library functions rely on the TZ environment variable. The information encoded in TZ is then used for triggering a chain of updates that ultimately fill certain data structures and objects with the correct data.

To access DST and other timezone related information, you must call the _tzset() function first:

void _tzset(void); //declared in 

_tzset() initializes the f the following global variables:

extern time_t _timezone;extern time_t _altzone;extern int _daylight;extern char* _tzname[2];
Author’s Note: These variables may have slightly different names on certain implementations. For instance, _timezone is called timezone on older implementations. In essence, the main difference is that older implementations don’t have an underscore at the beginning of functions and global variables. Please consult your IDE’s online documentation for further information.

The Meaning of Time
What does each of these global variables stand for?

  • _timezone contains the difference in seconds between UTC and the local standard time, where standard time means the local time excluding the DST changes.
  • _altzone contains the difference in seconds between UTC and the local alternate time. In other words, it contains the difference between UTC and the local Daylight Saving Time. Note that this variable isn’t defined by standard C/C++. It’s only available on certain POSIX systems (Solaris for instance) so it has to be used cautiously in cross-platform code.
  • _daylight is a Boolean flag. Its value is zero if DST is currently not in effect. A positive value indicates that DST is currently in effect. A negative value indicates an error of some sort so you should always check for three cases here, not just zero and non-zero values.
  • _tzname is a two element array of nul-terminated char* strings. _tzname[0] contains a textual timezone codename which represents the local standard time, “PST” for example. _tzname[1] is the name of the local alternate timezone, “PDT” for example.

Putting everything together, let’s look at a program that uses _tzset() and these global variables to retrieve the difference between UTC and the local standard time, and check whether DST is currently in effect:

#include #include #include using namespace std;int main(){_tzset(); //set all timezone global variablescout<< "Difference between UTC and the local standard time: "<<_timezone<< " seconds."<

When I run it on my computer, the output is:

Difference between UTC and the local standard time: -7200 seconds.Is DST in effect? falseLocal standard timezone code is:  Jerusalem Standard TimeLocal alternate timezone code is: Jerusalem Standard Time

Test it on your machine and see how your local time zone settings affect the results!

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: