Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


The Art of Daylight Savings Time-3 : Page 3




How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017

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 <ctime> #include <cstdlib> #include <iostream> using namespace std; int main() { _tzset(); //set all timezone global variables cout<< "Difference between UTC and the\ local standard time: "<<_timezone<< " seconds."<<endl; cout<<boolalpha; //switch to literal bools: 'true' and 'false' cout<<"Is DST in effect? "<<(bool) _daylight<<endl; cout<<"Local standard timezone code is: "<<_tzname[0]<<endl; cout<<"Local alternate timezone code is: "<<_tzname[1]<<endl; }

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

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

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

Danny Kalev is a certified system analyst and software engineer specializing in C++. He was a member of the C++ standards committee between 1997 and 2000 and has since been involved informally in the C++0x standardization process. He is the author of "The ANSI/ISO Professional C++ Programmer's Handbook" and "The Informit C++ Reference Guide: Techniques, Insight, and Practical Advice on C++."
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date