TimeZoneInfo.GetAmbiguousTimeOffsets Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Devolve informações sobre as possíveis datas e horas para as quais uma data e hora ambíguas pode ser mapeada.
Sobrecargas
| Name | Description |
|---|---|
| GetAmbiguousTimeOffsets(DateTime) |
Devolve informações sobre as possíveis datas e horas para as quais uma data e hora ambíguas pode ser mapeada. |
| GetAmbiguousTimeOffsets(DateTimeOffset) |
Devolve informações sobre as possíveis datas e horas para as quais uma data e hora ambíguas pode ser mapeada. |
GetAmbiguousTimeOffsets(DateTime)
- Origem:
- TimeZoneInfo.cs
- Origem:
- TimeZoneInfo.cs
- Origem:
- TimeZoneInfo.cs
- Origem:
- TimeZoneInfo.cs
- Origem:
- TimeZoneInfo.cs
Devolve informações sobre as possíveis datas e horas para as quais uma data e hora ambíguas pode ser mapeada.
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()
Parâmetros
- dateTime
- DateTime
Uma data e hora.
Devoluções
Um array de objetos que representa possíveis deslocamentos de Tempo Universal Coordenado (UTC) para os quais uma data e hora específicas pode ser mapeada.
Exceções
dateTime não é uma época ambígua.
Exemplos
O exemplo seguinte define um método nomeado ShowPossibleUtcTimes que utiliza o GetAmbiguousTimeOffsets(DateTime) método para mapear um tempo ambíguo para os seus possíveis tempos correspondentes no Tempo Universal Coordenado (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
O método pode então ser chamado usando código como o seguinte:
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
Observações
O comportamento preciso deste método depende da relação entre a Kind propriedade e o TimeZoneInfo objeto, como mostra a tabela seguinte.
| Tipo de objeto TimeZoneInfo | Valor da propriedade | Comportamento |
|---|---|---|
| TimeZoneInfo.Local | DateTimeKind.Local ou DateTimeKind.Unspecified | Devolve deslocamentos temporais ambíguos para dateTime. |
| TimeZoneInfo.Local | DateTimeKind.Utc | Converte dateTime para a hora local e depois devolve deslocamentos de tempo ambíguos para esse tempo. |
| TimeZoneInfo.Utc | Qualquer valor. | Lança um ArgumentException. |
| Qualquer outro fuso horário. | Local ou DateTimeKind.Utc | Converte dateTime para o fuso horário especificado e depois determina se esse horário é ambíguo. |
| Qualquer outro fuso horário. | DateTimeKind.Unspecified | Determina se dateTime é ambíguo no fuso horário especificado. |
A ordem dos TimeSpan objetos no array devolvido por este método é indefinida. No entanto, pode determinar qual elemento representa um deslocamento em relação ao horário padrão do fuso horário comparando o seu valor com a propriedade do BaseUtcOffset fuso horário. Para mapear uma hora ambígua para a hora padrão de um fuso horário, veja Como: Resolver Horas Ambíguas.
Ver também
- IsAmbiguousTime
- Como: Resolver Tempos Ambíguos
- Como: Deixar os Utilizadores Resolver Tempos Ambíguos
Aplica-se a
GetAmbiguousTimeOffsets(DateTimeOffset)
- Origem:
- TimeZoneInfo.cs
- Origem:
- TimeZoneInfo.cs
- Origem:
- TimeZoneInfo.cs
- Origem:
- TimeZoneInfo.cs
- Origem:
- TimeZoneInfo.cs
Devolve informações sobre as possíveis datas e horas para as quais uma data e hora ambíguas pode ser mapeada.
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()
Parâmetros
- dateTimeOffset
- DateTimeOffset
Uma data e hora.
Devoluções
Um array de objetos que representa possíveis deslocamentos de Tempo Universal Coordenado (UTC) para os quais uma data e hora específicas pode ser mapeada.
Exceções
dateTimeOffset não é uma época ambígua.
Observações
O comportamento preciso deste método depende da relação entre a Offset propriedade do dateTimeOffset parâmetro e o TimeZoneInfo objeto. Se o valor da Offset propriedade corresponder aos possíveis deslocamentos do fuso horário atual em relação ao Tempo Universal Coordenado (UTC) para essa data e hora, o método devolve os possíveis deslocamentos. Caso contrário, converte dateTimeOffset para a hora no fuso horário atual e depois devolve os possíveis deslocamentos dessa data e hora.
A ordem dos TimeSpan objetos no array devolvido por este método é indefinida. No entanto, pode determinar qual elemento representa um deslocamento em relação ao horário padrão do fuso horário comparando o seu valor com a propriedade do BaseUtcOffset fuso horário. Para mapear uma hora ambígua para a hora padrão de um fuso horário, veja Como: Resolver Horas Ambíguas.
Ver também
- IsAmbiguousTime(DateTime)
- Como: Resolver Tempos Ambíguos
- Como: Deixar os Utilizadores Resolver Tempos Ambíguos