Edit

Share via


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

  1. Call the IsAmbiguousTime method to determine whether the time is ambiguous.

  2. If the time is ambiguous, subtract the time from the TimeSpan object returned by the time zone's BaseUtcOffset property.

  3. 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 static 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;
    }
}
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

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.

See also