TZ and tzset Only Use Whole Hours: How to Work Around
Article ID: Q68383
Creation Date: 16-JAN-1991
Revision Date: 19-JAN-1996
The information in this article applies to:
Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, and 6.0ax
Microsoft C for OS/2, versions 5.1, 6.0, and 6.0a
Microsoft Visual C++ for Windows, versions 1.0 and 1.5
Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, and 2.1
In Microsoft C versions 5.1, 6.0, 6.0a, and 6.0ax, the TZ environment variable and the tzset() function can use only whole hours for time zone adjustments. These functions cannot be used, for example, to calculate the time in Sri Lanka, which is 330 minutes off of GMT.
The only workaround in C 6.x is to use a second environment variable (for example, TZ2) to add thirty minutes to the times passed by functions that return and use local times. Beginning with C/C++ version 7.0, tzset() and TZ have been enhanced to accept TZ values of the form tzn[+|-]hh[:mm[:ss]][dzn].
The information in this article applies to:
Microsoft Visual C++ for Windows, versions 1.50, 1.51, 1.52
Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2, 4.0, 4.1, 4.2
SUMMARY
For time zones that are ahead of Greenwich Mean Time such as Cairo's time zone (GMT + 2, which means the time in Cairo is two hours ahead of GMT), calling mktime with its argument set to correspond to January 1, 1970 00:00:00 (midnight), returns -1 (failure).
The Visual C++ 4.0 Books Online states:
mktime handles dates in any time zone from midnight, January 1, 1970, to midnight, February 5, 2036.
MORE INFORMATION
The problem is not that mktime is in error, it is the documentation's claim that this function will handle midnight January 1, 1970 in any time zone. Actually mktime returns the number of seconds that have elapsed since January 1, 1970 00:00:00, adjusted for the current time zone. Adjusted for current time zone means that the appropriate number of seconds will be added or subtracted such that mktime actually returns the number of seconds elapsed since midnight January 1, 1970 Greenwich Mean Time (GMT). This means that, for example, calling mktime for January 1, 1970 in the Pacific Time Zone (GMT - 8) returns 28800, which happens to be the number of seconds in 8 hours. The return value of 28800 in the Pacific Time Zone means that 28800 seconds have elapsed since January 1, 1970 00:00:00 GMT when it is the same time in the Pacific Time Zone. The problem occurs in time zones that are ahead of Greenwich Mean Time. Calling mktime in Cairo (GMT + 2), for example, would have to return -1 for January 1, 1970 00:00:00, as January 1, 1970 00:00:00 had not yet occured in GMT when the same time happened in Cairo.
Date.getTimezoneOffset
public int getTimezoneOffset()
Determines the local time zone offset. The time zone noffset is the number of minutes that must be added to Greenwich Mean Time to give the local time zone. This value includes the correction, if necessary, for daylight savings time.
Returns: the time zone offset, in minutes, for the current locale.
See Also Date Overview
public static long
UTC(int year, int month, int date, int hrs, int min, int sec)
Determines the date and time based on the arguments. The arguments are interpreted in UTC, not in the local time zone.
Parameters:
year - the year minus 1900
month - a month between 0-11
date - day of the month between 1-31
hrs - hours between 0-23
min - minutes between 0-59
sec - seconds between 0-59
Returns: the number of seconds since January 1, 1970, 00:00:00 GMT, for the date and time specified by the arguments.
| Language Reference |
| Start Page | Version 1 |
Description
Computes the number of milliseconds between midnight, January 1, 1970 UTC (or GMT) and the supplied date.
Syntax
Date.UTC(year, month, day, hours, minutes, seconds)
The UTC method syntax has these parts:
Part
Description
year (The year minus 1900. For example, specify 105 for the year 2005.)
month (The month as an integer between 0 and 11 (January to December).)
day (The day as an integer between 1 and 31.)
hours (Optional. Must be supplied if minutes is supplied. An integer from 0 to 23 (midnight to 11pm) that specifies the hour.)
minutes (Optional. Must be supplied if seconds is supplied. An integer from 0 to 59 that specifies the minutes.)
Seconds (Optional. An integer from 0 to 59 that specifies the seconds.)
Remarks
The UTC method returns the number of milliseconds between midnight, January 1, 1970 UTC (Universal Coordinated Time) and the supplied date. This return value can be used in the setTime method and in the Date object constructor.
[The difference between the UTC method and the Date object constructor that accepts a date, is that the UTC method assumes UTC, and the Date object constructor assumes local time.
The UTC method is a static method. A Date object does not have to be created before it can be used.