C# 2.0 fuso horário específico
-
27-09-2019 - |
Pergunta
Estou trabalhando em um aplicativo herdado no .NET 2.0 e quero converter o tempo desde o horário local (que por acaso é UTC+1) para o tempo no Brasil (ou seja, "E. A América do Sul Time Standard", como o Windows chama) e volta.
Reuni esse código que criei para fazer a conversão:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace timezone
{
class Program
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
private static extern int SystemTimeToTzSpecificLocalTime(ref
TIME_ZONE_INFORMATION lpTimeZone, ref SYSTEMTIME lpUniversalTIme, out
SYSTEMTIME lpLocalTime);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
private static extern int TzSpecificLocalTimeToSystemTime(ref
TIME_ZONE_INFORMATION lpTimeZone, ref SYSTEMTIME lpLocalTime, out SYSTEMTIME
lpUniversalTIme);
[DllImport("kernel32.dll")]
private static extern void GetSystemTime(out SYSTEMTIME lpSystemTime);
[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
//Registry time zone format. See KB article Q115231
[StructLayout(LayoutKind.Sequential)]
private struct REG_TIME_ZONE_INFORMATION
{
public int Bias;
public int StandardBias;
public int DaylightBias;
public SYSTEMTIME StandardDate;
public SYSTEMTIME DaylightDate;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct TIME_ZONE_INFORMATION
{
public int Bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string StandardName;
public SYSTEMTIME StandardDate;
public int StandardBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DaylightName;
public SYSTEMTIME DaylightDate;
public int DaylightBias;
}
private static List<TIME_ZONE_INFORMATION> GetTimeZones()
{
List<TIME_ZONE_INFORMATION> list = new List<TIME_ZONE_INFORMATION>();
RegistryKey key =
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones");
if (key == null)
return list;
string[] subKeyNames = key.GetSubKeyNames();
foreach (string subKeyName in subKeyNames)
{
RegistryKey subKey = key.OpenSubKey(subKeyName);
if (subKey != null)
{
object value = subKey.GetValue("TZI");
if (value != null)
{
int length =
Marshal.SizeOf(typeof(REG_TIME_ZONE_INFORMATION));
IntPtr p = Marshal.AllocHGlobal(length);
Marshal.Copy((byte[])value, 0, p, length);
REG_TIME_ZONE_INFORMATION rtzi =
(REG_TIME_ZONE_INFORMATION)Marshal.PtrToStructure(p,
typeof(REG_TIME_ZONE_INFORMATION));
Marshal.FreeHGlobal(p);
TIME_ZONE_INFORMATION tzi = new TIME_ZONE_INFORMATION();
tzi.Bias = rtzi.Bias;
tzi.DaylightBias = rtzi.DaylightBias;
tzi.StandardBias = rtzi.StandardBias;
tzi.DaylightDate = rtzi.DaylightDate;
tzi.StandardDate = rtzi.StandardDate;
tzi.DaylightName = (string)subKey.GetValue("Dlt", "");
tzi.StandardName = (string)subKey.GetValue("Std", "");
list.Add(tzi);
}
subKey.Close();
}
}
key.Close();
return list;
}
static void Main(string[] args)
{
foreach (TIME_ZONE_INFORMATION tzi in GetTimeZones())
{
if ("E. South America Standard Time" == tzi.StandardName)
{
Console.WriteLine("local time: " + DateTime.Now);
Console.WriteLine("name: {0} st bias:{1} daylight date:{2}-{3}-{4}, bias:{5}", tzi.StandardName, tzi.DaylightBias, tzi.DaylightDate.wDay, tzi.DaylightDate.wMonth, tzi.DaylightDate.wYear, tzi.Bias);
TIME_ZONE_INFORMATION tzi2 = tzi; //local copy so that i can pass it as a ref
SYSTEMTIME braziltime = new SYSTEMTIME();
SYSTEMTIME localtime = new SYSTEMTIME();
GetSystemTime(out localtime);
SystemTimeToTzSpecificLocalTime(ref tzi2, ref localtime, out braziltime);
Console.WriteLine("{0}:{1}", braziltime.wHour, braziltime.wMinute);
Console.WriteLine("{0}-{1}-{2} {3}:{4}:{5}", braziltime.wYear, braziltime.wMonth, braziltime.wDay, braziltime.wHour, braziltime.wMinute, braziltime.wSecond);
DateTime dt = DateTime.Now;
braziltime.wYear = (ushort)dt.Year;
braziltime.wMonth = (ushort)dt.Month;
braziltime.wDay = (ushort)dt.Day;
braziltime.wHour = (ushort)(dt.Hour - 3); //today the timezone difference is 3 hours
braziltime.wMinute = (ushort)dt.Minute;
braziltime.wSecond = (ushort)dt.Second;
TzSpecificLocalTimeToSystemTime(ref tzi2, ref braziltime, out localtime);
Console.WriteLine("{0}-{1}-{2} {3}:{4}:{5}", localtime.wYear, localtime.wMonth, localtime.wDay, localtime.wHour, localtime.wMinute, localtime.wSecond);
break;
}
}
Console.ReadLine();
}
}
}
Mas eu recebo esta saída:
local time: 11/22/2010 11:55:15 AM
name: E. South America Standard Time st bias:-60 daylight date:3-10-0, bias:180 8:55
2010-11-22 8:55:15
2010-11-22 10:55:15
Por isso, tomo o tempo local, converti -o para o brasileiro e volto e recebo uma hora a menos. Alguma idéia do que há de errado?
Solução
Eu acho que você está esperando a primeira linha da sua saída para corresponder à última linha. Isso não está acontecendo porque a primeira linha escreve a hora local. Você liga para getsystemtime e converte esse valor para o Brasil e volte novamente. O GetSystemTime retorna UTC para que o valor que você retorne e, em seguida, a saída na última linha também deve ser UTC. Em outras palavras, você não está comparando como.
Se você gerar o valor do local local imediatamente após a chamada para o GetSystemTime, você verá que corresponde à saída do valor após a conversão.
Se você deseja converter do horário local para o Brasil, provavelmente precisará converter o horário local para o UTC e converter o UTC para o Brasil, usando as informações apropriadas do fuso horário para cada etapa.
Outras dicas
Lembre -se do que o DateTime é apenas uma estrutura para armazenar DateTime.
Você deve usar em todos os lugares do seu aplicativo UTC e apenas usar a localidade para saída. Para estar limpo, prefiro usar a maioria das vezes UTC e fazer o meu convertido dele.
Se fosse 3,5 ...
using System;
// ReSharper disable once CheckNamespace
public static class BrazilTime
{
public static DateTime Now
{
get
{
return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("E. South America Standard Time"));
}
}
}