How to handle dates and times that include DST
Time storage and manipulationTimestamps are values that specify a date and time combination. Applications that must handle timestamps typically store those timestamps in Universal Coordinated Time (UTC). The advantage of UTC is that UTC is universal. UTC is not subject to local time zones or to DST. However, UTC is neither user-friendly nor relevant to most users. Although UTC is the obvious choice for storage, it is not a good choice for display. Therefore, most applications convert UTC time to local time before they display the timestamp to the user. For example, Windows Explorer applies the time zone and the DST setting to the UTC timestamp before it displays dates and times for files in a Windows NT File System (NTFS) directory.
Conversion from UTC time to local time can be thought of as applying two offsets. The first is the time zone offset, and the second is the DST offset. Therefore, local time is effectively UTC time plus a time zone offset, plus any applicable DST offset. The time zone offset is fairly straightforward. The computer is configured for a particular time zone, and that time zone has an offset from UTC. To determine whether a DST offset should be applied is much more complex. This activity relies on many rules that are complex and dynamic.
Those complex DST rules have recently changed with DST 2007. Starting in 2007, the United States has adopted new start dates and new end dates for DST. In addition, it is common for other countries and governments to routinely change the start dates and the end dates for DST in time zones that are under their control. The following section describes the effects of the DST 2007 change on developer-related products.
For more information about DST 2007, visit the following Microsoft Web site:
DST 2007 effects on developer-related technologies
WindowsOn Windows Update and on Microsoft Update, updates are available that enable Windows to correctly apply the changes for DST 2007 and for the following years. After these updates are applied, Windows correctly calculates the current offsets from UTC time to local time as the computer passes through DST. The offsets include the offsets for the base APIs and for the networking time-related APIs.
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
C runtime (CRT)The CRT also performs date translations and time translations. Therefore, the CRT must also be updated to include the new rules for DST 2007. The CRT performs its own time handling only when the TZ environment variable is set or when an underlying operating system API time call fails. Updates are available for the CRTs that are included with each version of Microsoft Visual Studio and also for the CRTs that are included with Windows. These updates enable the CRT to continue to correctly handle DST conversions in United States time zones.
The .NET FrameworkThe .NET Framework relies on the underlying operating system calls. Therefore, the behavior of the .NET Framework reflects the state of the underlying operating system. No separate update is required.
Visual Studio .NET Integrated Development Environments (IDEs)The Visual Studio .NET IDEs include versions 2002, 2003, and 2005 of Microsoft Visual C++, Microsoft Visual C#, and Microsoft Visual Basic. These products are affected only because they include the CRT. No IDE-specific update is required.
Visual Studio 2005 Team Foundation ServerVisual Studio 2005 Team Foundation Server relies on the underlying operating system for date and time conversions. Therefore, Visual Studio 2005 Team Foundation Server exhibits the same behavior as the operating system. Visual Studio 2005 Team Foundation Server also relies on Microsoft SQL Server, SQL Server Reporting Services, and Windows SharePoint Services. Computers should be updated with the relevant updates for the operating system, for SQL Server, and for Windows SharePoint Services. All relevant updates should be applied on all affected computers at the same time. No separate Visual Studio 2005 Team Foundation Server update is required.
Visual Studio 2005 Team SystemVisual Studio 2005 Team System is affected through the operating system, through Visual Studio 2005 Team Foundation Server, and through the CRT. No separate Visual Studio 2005 Team System update is required.
Visual SourceSafeFor more information about Visual SourceSafe DST issues in 2007, click the following article number to view the article in the Microsoft Knowledge Base:
Visual Basic 6.0 runtimeThe Visual Basic 6.0 runtime is not affected.
Visual C++ 6.0Visual C++ 6.0 is no longer supported.
For more information, visit the following Microsoft Web site:
Windows Software Development Kit (SDK) for Windows VistaThis SDK includes a version of the CRT that is affected by the DST 2007 changes. As part of the installation of this SDK, you can install the Visual Studio 2005 CRT on computers that do not already have that version of the CRT installed. If a newer version of the CRT is already installed, the SDK installation does not overwrite that newer version. When the SDK is uninstalled, the latest version of CRT is left on the computer. You may either install the Visual Studio 2005 CRT update before or after you install the SDK.
The Windows SDK for Windows Vista also installs a set of merge modules (.msm files) for the Visual Studio 2005 CRT for redistribution of the CRT as part of custom C++ applications. An application that deploys the redistributable CRT to the installation folder of the application must deploy the updated CRT from the Visual Studio 2005 CRT update instead of the CRT .msm files from the Windows SDK for Windows Vista. An application that deploys the redistributable Visual Studio 2005 CRT update to the Windows installation folder must apply the Visual Studio 2005 CRT redistributable update to those computers.
Platform SDK for Microsoft Windows Server 2003 R2This SDK includes a version of the CRT that is affected by the DST 2007 changes. Customers must follow the release notes for this SDK and use the Visual Studio 2005 CRT updates if they are necessary.
The .NET Framework 2.0 SDKThis SDK includes a version of the CRT that is affected by the DST 2007 changes. As part of the installation of this SDK, you can install the Visual Studio 2005 CRT on computers that do not already have that version of the CRT installed. If a newer version of the CRT is already installed, the SDK installation does not overwrite that newer version. When the SDK is uninstalled, the latest version of CRT is left on the computer. You may either install the Visual Studio 2005 CRT update before or after you install the SDK.
Local time conversion in WindowsApplications typically convert UTC times to local times before they display time information and date information to the user. Windows provides several APIs for applications to use for timestamp manipulation.
- The GetSystemTime() function and the GetSystemTimeAsFileTime() function obtain the current UTC time in a SYSTEMTIME structure or in a FILETIME structure.
- The GetLocalTime() function obtains the current local time in a SYSTEMTIME structure.
- The GetTimeZoneInformation() function obtains a TIME_ZONE_INFORMATION structure that describes the current time zone and the DST setting for the computer.
- The SystemTimeToFileTime() function and the FileTimeToSystemTime() function marshal between SYSTEMTIME structures and FILETIME structures.
- The FileTimeToLocalFileTime() function and the LocalFileTimeToFileTime() function convert and translate a FILETIME structure between UTC and local time by using the current time zone and the DST setting on the computer.
- The SystemTimeToTzSpecificLocalTime() function and the TzSpecificTimeToSystemTime() function convert a UTC timestamp in a SYSTEMTIME structure to a local SYSTEMTIME structure. These functions use a TIME_ZONE_INFORMATION structure that specifies the start date and the end date for DST. By default, the current DST rules are used when no such structure is provided.
- The NetRemoteTOD() function obtains the time from a remote server by using the server’s information and settings.
To see an example of this behavior in Windows Explorer, follow these steps on a computer that resides in a time zone that uses DST.
Be aware that these steps require that you change the system clock. Therefore, you must exit any applications, such as calendar applications, that might react to these time changes before you follow these steps.
- Change the date on the computer to a DST day. For example, set the date to July 1, 2006.
- In an NTFS directory on the same computer, create a new text file that is named Test.txt.
- Notice that the timestamp on the file is displayed as follows in Windows Explorer:7/1/2006 3:37pm
- Change the date on the computer to a non-DST day. For example, set the date to February 1, 2007.
- Refresh the Windows Explorer window.
- Notice that the timestamp on the file is displayed as follows in Windows Explorer:7/1/2006 2:37pm
For more information, see this blog: SystemTimeToTzSpecificLocalTime() method and the TzSpecificTimeToSystemTime() method convert between UTC time and the local time by using the provided TIME_ZONE_INFORMATION structure. If no time zone information is provided, these functions use the current time zone rules and the DST rules to determine whether a DST offset must be applied to the timestamp. This is functionally equivalent to calling the GetTimeZoneInformation() method to obtain the TIME_ZONE_INFORMATION structure that is currently in effect.
The TIME_ZONE_INFORMATION structure includes the start date and the stop date for DST. Therefore, when the TIME_ZONE_INFORMATION structure uses the current time zone information, the TIME_ZONE_INFORMATION structure may introduce a historical inaccuracy. This behavior may occur if the current time zone information and the DST information do not reflect the timestamp that is being converted. This behavior is affected by DST 2007 only because the rules that govern the dates when DST starts and stops have been changed.
To obtain historically accurate conversions from these functions, an application must provide a historically accurate TIME_ZONE_INFORMATION structure when the application calls these functions.
Dynamic time zones in WindowsWindows Vista introduces dynamic DST time zones. Dynamic DST provides support for time zones whose boundaries for DST change from year to year. These rules are stored in the registry. Applications can query the rules by using the GetDynamicTimeZoneInformation() function.
Dynamic time zones enable easier updating of computers, especially for locales where the yearly DST boundaries are known in advance. For more information about the DYNAMIC_TIME_ZONE_INFORMATION structure in the Windows SDK for Vista, visit the following Microsoft Developer Network (MSDN) Web site:
Local time conversion in the C runtime (CRT)The CRT basically has three modes in which it can translate timestamps:
- If the TZ environment variable is not set, the CRT calls the Windows APIs and exhibits Windows behavior as described in this article.
- If the TZ environment variable is set, the CRT performs its own conversions that are based on that setting. The CRT is being updated so that it respects the new DST 2007 rules when it performs conversions in this scenario.
- If the TZ environment variable is not set, but the underlying Windows APIs fail, the CRT falls back to its own conversions by using a value of PST8PDT for the TZ environment variable.
Note This conversion follows rules specific to the United States.
Before you apply the DST 2007 update, the CRT correctly handles current timestamps in United States time zones. After you apply the DST 2007 update, the CRT also handles both past and future United States dates. Updates for the CRT are listed in the “References” section.
Local time conversion in the .NET FrameworkThe .NET Framework contains classes that store and convert timestamps. These classes include the DateTime class, the TimeZone class, the TimeSpan class, and the DateTimeKind class. As previously noted, these classes depend primarily on the underlying platform implementation. These classes exhibit the same behavior as the underlying operating system APIs.
One interesting behavior exhibited by the .NET Framework date classes and time classes relates to the functions that offset the timestamp by a requested amount. For example, consider the AddHours() function, the AddMinutes() function, and the AddSeconds() function in the DateTime class. These functions, and similarly named functions, merely increment the timestamp by the requested amount without regard for DST settings. This behavior might be considered simple arithmetic on the underlying UTC timestamp. However, this behavior could lead to unexpected outcomes when the addition causes the timestamp to pass into or out of DST. This behavior is unrelated to DST 2007 changes.
RecommendationsThe following recommendations can help developers minimize the effect of DST 2007 and improve general date and time handling.
- You should plan for a near-atomic installation of the DST 2007 updates. All planned DST 2007 updates should be applied as close in time to one another as possible. If a computer that has been updated tries to use methods such as SQL queries or Web services to communicate with a computer that has not been updated, translation errors may occur. Similarly, if a single computer requires two or more updates, such as the Windows update and the CRT update, the updates should be applied at the same time.
- UTC timestamps are historically accurate. It is typically the conversion to local time that concerns most applications. Applications should always store UTC timestamps. Conversions to local time for display purposes require time zone information and DST information. This information can come from several sources:
- The application may use the current time zone and the DST setting for the conversion. This may introduce an inaccuracy in the conversion if the current time zone and the DST setting were not in effect at the time of the timestamp.
- The application may store the previously accurate time zone information and DST information in addition to the UTC timestamp.
- When dynamic time zones are available, the application may use dynamic time zones to determine which time zone information should be applied to a particular UTC timestamp. This option is available only when the dynamic time zone information is available for a specific timestamp and for a specific time zone.
- The application may store a local timestamp and the UTC timestamp. This method prevents the need for future conversion.
- Any communication between computers that deal with timestamps should use UTC timestamps. This implicitly gives both computers the same context information for UTC.
- If an application handles dates, you should be careful with the way that the dates are handled in testing. Dates that are displayed without time information are typically stored as a timestamp of 12:00 AM on the relevant date. Therefore, an off-by-one error in the hour part of the timestamp could result in an off-by-one result in the effective date if the time is shifted to 11:00 PM the previous day.
Id. de artículo: 932955 - Última revisión: 01/15/2008 14:38:06 - Revisión: 5.2
- kbinfo kbhowto KB932955