Skip to content

Latest commit

 

History

History
48 lines (30 loc) · 3.27 KB

let-users-resolve-ambiguous-times.md

File metadata and controls

48 lines (30 loc) · 3.27 KB
description title ms.date helpviewer_keywords ms.topic
Learn more about: How to: Let users resolve ambiguous times
How to: Let users resolve ambiguous times
04/10/2017
time zones [.NET], ambiguous time
ambiguous time [.NET]
how-to

How to: Let users 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:

  • If the ambiguous time is an item of data entered by the user, you can leave it to the user to resolve the ambiguity.

  • 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.

This topic shows how to let a user resolve an ambiguous time.

To let a user resolve an ambiguous time

  1. Get the date and time input by the user.

  2. Call the xref:System.TimeZoneInfo.IsAmbiguousTime%2A method to determine whether the time is ambiguous.

  3. If the time is ambiguous, call the xref:System.TimeZoneInfo.GetAmbiguousTimeOffsets%2A method to retrieve an array of xref:System.TimeSpan objects. Each element in the array contains a UTC offset that the ambiguous time can map to.

  4. Let the user select the desired offset.

  5. Get the UTC date and time by subtracting the offset selected by the user from the local time.

  6. Call the static (Shared in Visual Basic .NET) xref:System.DateTime.SpecifyKind%2A method to set the UTC date and time value's xref:System.DateTime.Kind%2A property to xref:System.DateTimeKind.Utc?displayProperty=nameWithType.

Example

The following example prompts the user to enter a date and time and, if it is ambiguous, lets the user select the UTC time that the ambiguous time maps to.

[!code-csharpSystem.TimeZone2.Concepts#11] [!code-vbSystem.TimeZone2.Concepts#11]

The core of the example code uses an array of xref:System.TimeSpan objects to indicate possible offsets of the ambiguous time from UTC. However, these offsets are unlikely to be meaningful to the user. To clarify the meaning of the offsets, the code also notes whether an offset represents the local time zone's standard time or its daylight saving time. The code determines which time is standard and which time is daylight by comparing the offset with the value of the xref:System.TimeZoneInfo.BaseUtcOffset%2A property. This property indicates the difference between the UTC and the time zone's standard time.

In this example, all references to the local time zone are made through the xref:System.TimeZoneInfo.Local%2A?displayProperty=nameWithType property; the local time zone is never assigned to an object variable. This is a recommended practice because a call to the xref:System.TimeZoneInfo.ClearCachedData%2A?displayProperty=nameWithType method invalidates any objects that the local time zone is assigned to.

See also