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!