TimeZoneInfo.GetAmbiguousTimeOffsets Method

Definition

Returns information about the possible dates and times that an ambiguous date and time can be mapped to.

Overloads

GetAmbiguousTimeOffsets(DateTime)

Returns information about the possible dates and times that an ambiguous date and time can be mapped to.

GetAmbiguousTimeOffsets(DateTimeOffset)

Returns information about the possible dates and times that an ambiguous date and time can be mapped to.

GetAmbiguousTimeOffsets(DateTime)

Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs

Returns information about the possible dates and times that an ambiguous date and time can be mapped to.

public TimeSpan[] GetAmbiguousTimeOffsets (DateTime dateTime);

Parameters

dateTime
DateTime

A date and time.

Returns

An array of objects that represents possible Coordinated Universal Time (UTC) offsets that a particular date and time can be mapped to.

Exceptions

dateTime is not an ambiguous time.

Examples

The following example defines a method named ShowPossibleUtcTimes that uses the GetAmbiguousTimeOffsets(DateTime) method to map an ambiguous time to its possible corresponding Coordinated Universal Time (UTC) times.

private void ShowPossibleUtcTimes(DateTime ambiguousTime, TimeZoneInfo timeZone)
{
   // Determine if time is ambiguous in target time zone
   if (! timeZone.IsAmbiguousTime(ambiguousTime))
   {
      Console.WriteLine("{0} is not ambiguous in time zone {1}.", 
                        ambiguousTime, 
                        timeZone.DisplayName);
   }
   else
   {
      // Display time and its time zone (local, UTC, or indicated by timeZone argument)
      string originalTimeZoneName; 
      if (ambiguousTime.Kind == DateTimeKind.Utc)
         originalTimeZoneName = "UTC";
      else if (ambiguousTime.Kind == DateTimeKind.Local)
         originalTimeZoneName = "local time";
      else
         originalTimeZoneName = timeZone.DisplayName;

      Console.WriteLine("{0} {1} maps to the following possible times:", 
                        ambiguousTime, originalTimeZoneName);
      // Get ambiguous offsets 
      TimeSpan[] offsets = timeZone.GetAmbiguousTimeOffsets(ambiguousTime);
      // Handle times not in time zone of timeZone argument
      // Local time where timeZone is not local zone
      if ((ambiguousTime.Kind == DateTimeKind.Local) && ! timeZone.Equals(TimeZoneInfo.Local)) 
         ambiguousTime = TimeZoneInfo.ConvertTime(ambiguousTime, TimeZoneInfo.Local, timeZone);
      // UTC time where timeZone is not UTC zone   
      else if ((ambiguousTime.Kind == DateTimeKind.Utc) && ! timeZone.Equals(TimeZoneInfo.Utc))
         ambiguousTime = TimeZoneInfo.ConvertTime(ambiguousTime, TimeZoneInfo.Utc, timeZone);

      // Display each offset and its mapping to UTC
      foreach (TimeSpan offset in offsets)
      {
         if (offset.Equals(timeZone.BaseUtcOffset))
            Console.WriteLine("If {0} is {1}, {2} UTC", ambiguousTime, timeZone.StandardName, ambiguousTime - offset);
         else
            Console.WriteLine("If {0} is {1}, {2} UTC", ambiguousTime, timeZone.DaylightName, ambiguousTime - offset);
      }
   }            
}

The method can then be called using code such as the following:

Console.WriteLine();
ShowPossibleUtcTimes(new DateTime(2007, 11, 4, 1, 0, 0), 
                     TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));
Console.WriteLine();
ShowPossibleUtcTimes(new DateTime(2007, 11, 4, 01, 00, 00, DateTimeKind.Local), 
                     TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));
Console.WriteLine();
ShowPossibleUtcTimes(new DateTime(2007, 11, 4, 00, 00, 00, DateTimeKind.Local), 
                     TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));
Console.WriteLine();                     
ShowPossibleUtcTimes(new DateTime(2007, 11, 4, 01, 00, 00, DateTimeKind.Unspecified), 
                     TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));
Console.WriteLine();
ShowPossibleUtcTimes(new DateTime(2007, 11, 4, 07, 00, 00, DateTimeKind.Utc), 
                     TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));
// 
// This example produces the following output if run in the Pacific time zone:
//
//    11/4/2007 1:00:00 AM (GMT-06:00) Central Time (US & Canada) maps to the following possible times:
//    If 11/4/2007 1:00:00 AM is Central Standard Time, 11/4/2007 7:00:00 AM UTC
//    If 11/4/2007 1:00:00 AM is Central Daylight Time, 11/4/2007 6:00:00 AM UTC
//       
//    11/4/2007 1:00:00 AM Pacific Standard Time is not ambiguous in time zone (GMT-06:00) Central Time (US & Canada).
//     
//    11/4/2007 12:00:00 AM local time maps to the following possible times:
//    If 11/4/2007 1:00:00 AM is Central Standard Time, 11/4/2007 7:00:00 AM UTC
//    If 11/4/2007 1:00:00 AM is Central Daylight Time, 11/4/2007 6:00:00 AM UTC
//    
//    11/4/2007 1:00:00 AM (GMT-06:00) Central Time (US & Canada) maps to the following possible times:
//    If 11/4/2007 1:00:00 AM is Central Standard Time, 11/4/2007 7:00:00 AM UTC
//    If 11/4/2007 1:00:00 AM is Central Daylight Time, 11/4/2007 6:00:00 AM UTC
//       
//    11/4/2007 7:00:00 AM UTC maps to the following possible times:
//    If 11/4/2007 1:00:00 AM is Central Standard Time, 11/4/2007 7:00:00 AM UTC
//    If 11/4/2007 1:00:00 AM is Central Daylight Time, 11/4/2007 6:00:00 AM UTC
//

Remarks

The precise behavior of this method depends on the relationship between the Kind property and the TimeZoneInfo object, as the following table shows.

TimeZoneInfo object type Kind property value Behavior
TimeZoneInfo.Local DateTimeKind.Local or DateTimeKind.Unspecified Returns ambiguous time offsets for dateTime.
TimeZoneInfo.Local DateTimeKind.Utc Converts dateTime to the local time, and then returns ambiguous time offsets for that time.
TimeZoneInfo.Utc Any value. Throws an ArgumentException.
Any other time zone. Local or DateTimeKind.Utc Converts dateTime to the specified time zone, and then determines whether that time is ambiguous.
Any other time zone. DateTimeKind.Unspecified Determines whether dateTime is ambiguous in the specified time zone.

The order of TimeSpan objects in the array returned by this method is undefined. However, you can determine which element represents an offset from the time zone's standard time by comparing its value with the time zone's BaseUtcOffset property. To map an ambiguous time to a time zone's standard time, see How to: Resolve Ambiguous Times.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

GetAmbiguousTimeOffsets(DateTimeOffset)

Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs

Returns information about the possible dates and times that an ambiguous date and time can be mapped to.

public TimeSpan[] GetAmbiguousTimeOffsets (DateTimeOffset dateTimeOffset);

Parameters

dateTimeOffset
DateTimeOffset

A date and time.

Returns

An array of objects that represents possible Coordinated Universal Time (UTC) offsets that a particular date and time can be mapped to.

Exceptions

dateTimeOffset is not an ambiguous time.

Remarks

The precise behavior of this method depends on the relationship between the Offset property of the dateTimeOffset parameter and the TimeZoneInfo object. If the value of the Offset property corresponds to the current time zone's possible offsets from Coordinated Universal Time (UTC) for that date and time, the method returns the possible offsets. Otherwise, it converts dateTimeOffset to the time in the current time zone, and then returns the possible offsets of that date and time.

The order of TimeSpan objects in the array returned by this method is undefined. However, you can determine which element represents an offset from the time zone's standard time by comparing its value with the time zone's BaseUtcOffset property. To map an ambiguous time to a time zone's standard time, see How to: Resolve Ambiguous Times.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0