TimeZone.ToLocalTime(DateTime) Método
Definição
Retorna a hora local que corresponde a um valor de data e hora especificado.Returns the local time that corresponds to a specified date and time value.
public:
virtual DateTime ToLocalTime(DateTime time);
public virtual DateTime ToLocalTime (DateTime time);
abstract member ToLocalTime : DateTime -> DateTime
override this.ToLocalTime : DateTime -> DateTime
Public Overridable Function ToLocalTime (time As DateTime) As DateTime
Parâmetros
- time
- DateTime
Um horário UTC (Tempo Universal Coordenado).A Coordinated Universal Time (UTC) time.
Retornos
Um objeto DateTime cujo valor é a hora local que corresponde a time.A DateTime object whose value is the local time that corresponds to time.
Comentários
A tabela a seguir mostra a relação entre o time parâmetro e o DateTime valor retornado por esse método.The following table shows the relationship between the time parameter and the DateTime value returned by this method.
Parâmetro timetime parameter |
ComportamentoBehavior | Valor retornadoReturn value |
|---|---|---|
| Um tempo universal coordenado (UTC) ( DateTimeKind.Utc ).A Coordinated Universal Time (UTC) time (DateTimeKind.Utc). | Converte a hora de UTC para a hora local.Converts the time from UTC to the local time. | Um objeto DateTime cujo valor é a hora local que corresponde a time.A DateTime object whose value is the local time that corresponds to time. |
| Uma hora local ( DateTimeKind.Local ).A local time (DateTimeKind.Local). | Nenhuma conversão necessária.No conversion necessary. | O mesmo DateTime valor representado pelo time parâmetro.The same DateTime value represented by the time parameter. |
| Uma hora não especificada ( DateTimeKind.Unspecified ).An unspecified time (DateTimeKind.Unspecified). | Pressupõe que o horário é UTC e o converte de UTC para a hora local.Assumes that the time is UTC and converts it from UTC to the local time. | Um objeto DateTime cujo valor é a hora local que corresponde a time.A DateTime object whose value is the local time that corresponds to time. |
Se o fuso horário local observar o horário de verão, ToLocalTime o aplicará a regra de ajuste atual ao time ao executar a conversão.If the local time zone observes daylight saving time, ToLocalTime applies the current adjustment rule to time when performing the conversion.
Observação
O ToLocalTime método reconhece apenas a regra de ajuste de horário de verão atual para o fuso horário local.The ToLocalTime method recognizes only the current daylight saving time adjustment rule for the local time zone. Como resultado, é garantido retornar com precisão a hora local correspondente a uma hora UTC específica somente durante o período em que a regra de ajuste mais recente está em vigor.As a result, it is guaranteed to accurately return the local time corresponding to a particular UTC time only during the period in which the latest adjustment rule is in effect. Ele pode retornar resultados imprecisos se time for um valor de data e hora histórico que estava sujeito a uma regra de ajuste anterior.It may return inaccurate results if time is a historic date and time value that was subject to a previous adjustment rule.
O ToLocalTime método corresponde ao TimeZoneInfo.ConvertTimeFromUtc método com seu destinationTimeZone parâmetro definido como TimeZoneInfo.Local .The ToLocalTime method corresponds to the TimeZoneInfo.ConvertTimeFromUtc method with its destinationTimeZone parameter set to TimeZoneInfo.Local. Sempre que possível, use o TimeZoneInfo.ConvertTimeFromUtc método.Whenever possible, use the TimeZoneInfo.ConvertTimeFromUtc method.
Notas aos Herdeiros
Embora não seja necessário, na maioria dos casos, as classes derivadas em execução no .NET Framework versão 2,0 devem substituir a implementação padrão desse método.Although it is not required, in most cases derived classes running under the .NET Framework version 2.0 should override the default implementation of this method. No .NET Framework versões 1,0 e 1,1, o ToLocalTime método chamou o GetUtcOffset(DateTime) método e ajustado para o horário de verão ao retornar a hora local.In the .NET Framework versions 1.0 and 1.1, the ToLocalTime method called the GetUtcOffset(DateTime) method and adjusted for daylight saving time when returning the local time. No entanto, começando com o .NET Framework 2,0, o comportamento da implementação padrão depende da Kind Propriedade do time parâmetro.However, starting with the .NET Framework 2.0, the behavior of the default implementation depends on the Kind property of the time parameter. Se seu valor for Local , esse método retornará time inalterado.If its value is Local, this method returns time unchanged. Se seu valor for Utc ou Unspecified , esse método assumirá time o UTC e o converterá na hora do sistema local sem chamar o GetUtcOffset(DateTime) método.If its value is either Utc or Unspecified, this method assumes time is UTC and converts it to the local system time without calling the GetUtcOffset(DateTime) method.
O código a seguir fornece uma substituição simples da implementação padrão do ToLocalTime método.The following code provides a simple override of the default implementation of the ToLocalTime method. Nesse código, a internalTimeZone variável representa uma instância privada da TimeZone classe:In this code, the internalTimeZone variable represents a private instance of the TimeZone class:
[! Code-CsharpSystem. TimeZone. Paralocaltime # 1] [! Code-VBSystem. TimeZone. ToLocalTime # 1][!code-csharpSystem.TimeZone.ToLocalTime#1] [!code-vbSystem.TimeZone.ToLocalTime#1]