TimeZoneInfo.GetAmbiguousTimeOffsets 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回不明确的日期和时间可能映射到的日期和时间的相关信息。
重载
GetAmbiguousTimeOffsets(DateTime) |
返回不明确的日期和时间可能映射到的日期和时间的相关信息。 |
GetAmbiguousTimeOffsets(DateTimeOffset) |
返回不明确的日期和时间可能映射到的日期和时间的相关信息。 |
GetAmbiguousTimeOffsets(DateTime)
- Source:
- TimeZoneInfo.cs
- Source:
- TimeZoneInfo.cs
- Source:
- TimeZoneInfo.cs
返回不明确的日期和时间可能映射到的日期和时间的相关信息。
public:
cli::array <TimeSpan> ^ GetAmbiguousTimeOffsets(DateTime dateTime);
public TimeSpan[] GetAmbiguousTimeOffsets (DateTime dateTime);
member this.GetAmbiguousTimeOffsets : DateTime -> TimeSpan[]
Public Function GetAmbiguousTimeOffsets (dateTime As DateTime) As TimeSpan()
参数
- dateTime
- DateTime
日期和时间。
返回
对象的数组,它表示特定日期和时间可以映射到的可能的协调世界时 (UTC) 偏移量。
例外
dateTime
是明确的时间。
示例
以下示例定义了一个名为 ShowPossibleUtcTimes
的方法,该方法使用 GetAmbiguousTimeOffsets(DateTime) 方法将不明确时间映射到其可能的相应协调世界时 (UTC) 时间。
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);
}
}
}
let showPossibleUtcTimes (ambiguousTime: DateTime) (timeZone: TimeZoneInfo) =
// Determine if time is ambiguous in target time zone
if not (timeZone.IsAmbiguousTime ambiguousTime) then
printfn $"{ambiguousTime} is not ambiguous in time zone {timeZone.DisplayName}."
else
// Display time and its time zone (local, UTC, or indicated by timeZone argument)
let originalTimeZoneName =
match ambiguousTime.Kind with
| DateTimeKind.Utc -> "UTC"
| DateTimeKind.Local -> "local time"
| _ -> timeZone.DisplayName
printfn $"{ambiguousTime} {originalTimeZoneName} maps to the following possible times:"
// Get ambiguous offsets
let offsets = timeZone.GetAmbiguousTimeOffsets ambiguousTime
// Handle times not in time zone of timeZone argument
// Local time where timeZone is not local zone
let ambiguousTime =
if (ambiguousTime.Kind = DateTimeKind.Local) && not (timeZone.Equals TimeZoneInfo.Local) then
TimeZoneInfo.ConvertTime(ambiguousTime, TimeZoneInfo.Local, timeZone)
// UTC time where timeZone is not UTC zone
elif (ambiguousTime.Kind = DateTimeKind.Utc) && not (timeZone.Equals TimeZoneInfo.Utc) then
TimeZoneInfo.ConvertTime(ambiguousTime, TimeZoneInfo.Utc, timeZone)
else
ambiguousTime
// Display each offset and its mapping to UTC
for offset in offsets do
if offset.Equals timeZone.BaseUtcOffset then
printfn $"If {ambiguousTime} is {timeZone.StandardName}, {ambiguousTime - offset} UTC"
else
printfn $"If {ambiguousTime} is {timeZone.DaylightName}, {ambiguousTime - offset} UTC"
Private Sub ShowPossibleUtcTimes(ambiguousTime As Date, timeZone As TimeZoneInfo)
' Determine if time is ambiguous in target time zone
If Not timeZone.IsAmbiguousTime(ambiguousTime) Then
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)
Dim originalTimeZoneName As String
If ambiguousTime.Kind = DateTimeKind.Utc Then
originalTimeZoneName = "UTC"
ElseIf ambiguousTime.Kind = DateTimeKind.Local Then
originalTimeZoneName = "local time"
Else
originalTimeZoneName = timeZone.DisplayName
End If
Console.WriteLine("{0} {1} maps to the following possible times:", _
ambiguousTime, originalTimeZoneName)
' Get ambiguous offsets
Dim offsets() As TimeSpan = 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) And Not timeZone.Equals(TimeZoneInfo.Local) Then
ambiguousTime = TimeZoneInfo.ConvertTime(ambiguousTime, TimeZoneInfo.Local, timeZone)
' UTC time where timeZone is not UTC zone
ElseIf (ambiguousTime.Kind = DateTimeKind.Utc) And Not timeZone.Equals(TimeZoneInfo.Utc) Then
ambiguousTime = TimeZoneInfo.ConvertTime(ambiguousTime, TimeZoneInfo.Utc, timeZone)
End If
' Display each offset and its mapping to UTC
For Each offset As TimeSpan In offsets
If offset.Equals(timeZone.BaseUtcOffset) Then
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)
End If
Next
End If
End Sub
然后,可以使用如下代码调用 方法:
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
//
printfn ""
showPossibleUtcTimes(DateTime(2007, 11, 4, 1, 0, 0)) (TimeZoneInfo.FindSystemTimeZoneById "Central Standard Time")
printfn ""
showPossibleUtcTimes (DateTime(2007, 11, 4, 01, 00, 00, DateTimeKind.Local)) (TimeZoneInfo.FindSystemTimeZoneById "Central Standard Time")
printfn ""
showPossibleUtcTimes (DateTime(2007, 11, 4, 00, 00, 00, DateTimeKind.Local)) (TimeZoneInfo.FindSystemTimeZoneById "Central Standard Time")
printfn ""
showPossibleUtcTimes (DateTime(2007, 11, 4, 01, 00, 00, DateTimeKind.Unspecified)) (TimeZoneInfo.FindSystemTimeZoneById "Central Standard Time")
printfn ""
showPossibleUtcTimes (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
//
ShowPossibleUtcTimes(#11/4/2007 1:00:00#, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))
Console.WriteLine()
ShowPossibleUtcTimes(New Date(2007, 11, 4, 01, 00, 00, DateTimeKind.Local), _
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))
Console.WriteLine()
ShowPossibleUtcTimes(New Date(2007, 11, 4, 00, 00, 00, DateTimeKind.Local), _
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))
Console.WriteLine()
ShowPossibleUtcTimes(New Date(2007, 11, 4, 01, 00, 00, DateTimeKind.Unspecified), _
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))
Console.WriteLine()
ShowPossibleUtcTimes(New Date(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
注解
此方法的精确行为取决于 属性和 TimeZoneInfo 对象之间的关系Kind,如下表所示。
TimeZoneInfo 对象类型 | Kind 属性值 | 行为 |
---|---|---|
TimeZoneInfo.Local | DateTimeKind.Local 或 DateTimeKind.Unspecified | 返回 的 dateTime 不明确时间偏移量。 |
TimeZoneInfo.Local | DateTimeKind.Utc |
dateTime 转换为本地时间,然后返回该时间的不明确时间偏移量。 |
TimeZoneInfo.Utc | 任意值。 | 引发 ArgumentException。 |
任何其他时区。 | Local 或 DateTimeKind.Utc |
dateTime 转换为指定的时区,然后确定该时间是否不明确。 |
任何其他时区。 | DateTimeKind.Unspecified | 确定在指定的时区中是否 dateTime 不明确。 |
此方法返回的 TimeSpan 数组中对象的顺序未定义。 但是,可以通过比较哪个元素的值与时区的 属性来确定哪个元素表示与时区标准时间的 BaseUtcOffset 偏移量。 若要将不明确时间映射到时区的标准时间,请参阅 如何:解决不明确时间。
另请参阅
适用于
GetAmbiguousTimeOffsets(DateTimeOffset)
- Source:
- TimeZoneInfo.cs
- Source:
- TimeZoneInfo.cs
- Source:
- TimeZoneInfo.cs
返回不明确的日期和时间可能映射到的日期和时间的相关信息。
public:
cli::array <TimeSpan> ^ GetAmbiguousTimeOffsets(DateTimeOffset dateTimeOffset);
public TimeSpan[] GetAmbiguousTimeOffsets (DateTimeOffset dateTimeOffset);
member this.GetAmbiguousTimeOffsets : DateTimeOffset -> TimeSpan[]
Public Function GetAmbiguousTimeOffsets (dateTimeOffset As DateTimeOffset) As TimeSpan()
参数
- dateTimeOffset
- DateTimeOffset
日期和时间。
返回
对象的数组,它表示特定日期和时间可以映射到的可能的协调世界时 (UTC) 偏移量。
例外
dateTimeOffset
是明确的时间。
注解
此方法的精确行为取决于 参数的 dateTimeOffset
属性与 TimeZoneInfo 对象之间的关系Offset。 如果 属性的值 Offset 对应于当前时区与该日期和时间的协调世界时 (UTC) 的可能偏移量,该方法将返回可能的偏移量。 否则,它将转换为 dateTimeOffset
当前时区中的时间,然后返回该日期和时间的可能偏移量。
此方法返回的 TimeSpan 数组中对象的顺序未定义。 但是,可以通过比较哪个元素的值与时区的 属性来确定哪个元素表示与时区标准时间的 BaseUtcOffset 偏移量。 若要将不明确时间映射到时区的标准时间,请参阅 如何:解决不明确时间。