C# 2.0 特定时区

发布于 2024-10-03 08:57:18 字数 5144 浏览 0 评论 0原文

我正在 .NET 2.0 中开发一个遗留应用程序,我想将时间从本地时间(恰好是 UTC+1)转换为巴西时间(即 Windows 所说的“南美洲东部标准时间”)然后回来。

我将我想出的代码放在一起进行转换:

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();
  }
 }
}

但是我得到了这个输出:

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

所以我将当地时间转换为巴西时间并返回,并减少一个小时。 任何想法有什么问题吗?

I am working on a legacy application in .NET 2.0 and I want to convert time from local time (which happens to be UTC+1) to the time in Brazil (that is "E. South America Standard Time" as Windows calls it) and back.

I put together this code that I came up with to do the conversion:

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();
  }
 }
}

but I get this output:

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

So I take local time convert it to brazilian time and back and get an hour less.
Any ideas what's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伤痕我心 2024-10-10 08:57:18

我认为您期望输出的第一行与最后一行匹配。这种情况没有发生,因为第一行写的是当地时间。然后,您调用 GetSystemTime 并将该值转换为巴西时间,然后再转换回来。 GetSystemTime 返回 UTC,因此您返回并在最后一行输出的值也应该是 UTC。换句话说,你不是在比较同类。

如果您在调用 GetSystemTime 之后立即输出本地时间的值,您应该会看到该值与转换后的输出值匹配。

如果您想从本地时间转换为巴西时间,那么您可能需要将本地时间转换为 UTC,然后使用每个步骤的相应时区信息将 UTC 转换为巴西时间。

I think you are expecting the first line of your output to match the last line. That is not happening because the first line writes the localtime. You then call GetSystemTime and convert that value to Brazil time and back again. GetSystemTime returns UTC so the value you get back and then output on the last line should be UTC too. In other words you are not comparing like with like.

If you output the value of localtime immediately after the call to GetSystemTime you should see that match the value output after the conversion.

If you want to convert from your local time to Brazil time then you probably need to convert your local time to UTC and then convert UTC to Brazil time using the appropriate time zone info for each step.

楠木可依 2024-10-10 08:57:18

请记住,DateTime 只是一个用于存储日期时间的结构。

您应该在应用程序中的所有位置使用 UTC,并且仅使用 Locale 进行输出。为了干净,我更喜欢大部分时间使用 utc 并从中进行转换。

Remember than DateTime is just a struct in order to store datetime.

You should use everywhere in your app UTC, and just use Locale for output. In order to be clean, i prefer use most of the time utc and doing my convert from it.

从来不烧饼 2024-10-10 08:57:18

如果是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"));
        }
    }
}

If it was 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"));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文