How to: Resolve Ambiguous Times
An ambiguous time is a time that maps to more than one Coordinated Universal Time (UTC). It occurs when the clock time is adjusted back in time, such as during the transition from a time zone's daylight saving time to its standard time. When handling an ambiguous time, you can do one of the following:
Make an assumption about how the time maps to UTC. For example, you can assume that an ambiguous time is always expressed in the time zone's standard time.
If the ambiguous time is an item of data entered by the user, you can leave it to the user to resolve the ambiguity.
This topic shows how to resolve an ambiguous time by assuming that it represents the time zone's standard time.
To map an ambiguous time to a time zone's standard time
Call the IsAmbiguousTime method to determine whether the time is ambiguous.
If the time is ambiguous, subtract the time from the TimeSpan object returned by the time zone's BaseUtcOffset property.
Call the static (Shared in Visual Basic .NET) SpecifyKind method to set the UTC date and time value's Kind property to DateTimeKind.Utc.
Example
The following example illustrates how to convert an ambiguous time to UTC by assuming that it represents the local time zone's standard time.
Private Function ResolveAmbiguousTime(ambiguousTime As Date) As Date
' Time is not ambiguous
If Not TimeZoneInfo.Local.IsAmbiguousTime(ambiguousTime) Then
Return TimeZoneInfo.ConvertTimeToUtc(ambiguousTime)
' Time is ambiguous
Else
Dim utcTime As Date = DateTime.SpecifyKind(ambiguousTime - TimeZoneInfo.Local.BaseUtcOffset, DateTimeKind.Utc)
Console.WriteLine("{0} local time corresponds to {1} {2}.", ambiguousTime, utcTime, utcTime.Kind.ToString())
Return utcTime
End If
End Function
private DateTime ResolveAmbiguousTime(DateTime ambiguousTime)
{
// Time is not ambiguous
if (! TimeZoneInfo.Local.IsAmbiguousTime(ambiguousTime))
{
return ambiguousTime;
}
// Time is ambiguous
else
{
DateTime utcTime = DateTime.SpecifyKind(ambiguousTime - TimeZoneInfo.Local.BaseUtcOffset,
DateTimeKind.Utc);
Console.WriteLine("{0} local time corresponds to {1} {2}.",
ambiguousTime, utcTime, utcTime.Kind.ToString());
return utcTime;
}
}
The example consists of a method named ResolveAmbiguousTime that determines whether the DateTime value passed to it is ambiguous. If the value is ambiguous, the method returns a DateTime value that represents the corresponding UTC time. The method handles this conversion by subtracting the value of the local time zone's BaseUtcOffset property from the local time.
Ordinarily, an ambiguous time is handled by calling the GetAmbiguousTimeOffsets method to retrieve an array of TimeSpan objects that contain the ambiguous time's possible UTC offsets. However, this example makes the arbitrary assumption that an ambiguous time should always be mapped to the time zone's standard time. The BaseUtcOffset property returns the offset between UTC and a time zone's standard time.
In this example, all references to the local time zone are made through the TimeZoneInfo.Local property; the local time zone is never assigned to an object variable. This is a recommended practice because a call to the TimeZoneInfo.ClearCachedData method invalidates any objects that the local time zone is assigned to.
Compiling the Code
This example requires:
That a reference to System.Core.dll be added to the project.
That the System namespace be imported with the using statement (required in C# code).
See Also
Tasks
How to: Let Users Resolve Ambiguous Times