我现需做个时区转换的功能,如:北京时间2005-06-14 12:14:35 转换成纽约时间,东京时间等。请问怎样计算?或者哪位知道算法,能否帮我下?
解决方案 »
- 如何动态的用js给lable赋ID值
- 二个正则求解...
- [C#][WinForm]DataGridView disabled会按到背后的按钮
- C# 下拉
- 大家来看看,顶者有分,在线等!
- 如何保证某列数据不重复,c# winform开发,与数据库无关 ...
- 这种文件该怎么读写?
- 有没有用Odp.net做开发的,知道如何返回在oracle中自定义的类型,然后在c#中接收呢?
- datagridview绑定数据之后如何把某一列的样式设置成datagridviewlinkcolumn类型
- 求助,DLL字符串返回问题
- 为DataTable设置主键受挫,能否帮个忙啊?
- new + virtual + override 是不是等于垃圾???
TzSpecificLocalTimeToSystemTime()
TIME_ZONE_INFORMATION
构造信息
For Windows NT and Windows 2000
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
For Windows 9x
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones
BCL中TimeZone类只能获取本机设定,只能用api了
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.Win32;namespace ConsoleApplication26
{
[StructLayout(LayoutKind.Sequential)]
struct SYSTEMTIME
{
public short Year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds; public static implicit operator SYSTEMTIME(DateTime dt)
{
SYSTEMTIME st = new SYSTEMTIME(); st.Year = (short)dt.Year;
st.Month = (short)dt.Month;
st.DayOfWeek = (short)dt.DayOfWeek;
st.Day = (short)dt.Day;
st.Hour = (short)dt.Hour;
st.Minute = (short)dt.Minute;
st.Second = (short)dt.Second;
st.Milliseconds = (short)dt.Millisecond;
return st;
} public static implicit operator DateTime(SYSTEMTIME st)
{
return new DateTime(st.Year, st.Month, st.Day, st.Hour, st.Minute, st.Second, st.Milliseconds);
}
} [StructLayout(LayoutKind.Sequential)]
struct TIME_ZONE_INFORMATION
{
public int Bias;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public char[] StandardName;
public SYSTEMTIME StandardDate;
public int StandardBias;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public char[] DaylightName;
public SYSTEMTIME DaylightDate;
public int DaylightBias; public TIME_ZONE_INFORMATION(TIME_ZONE_INFORMATION tzi)
{
this = tzi;
DaylightName = new char[32];
tzi.DaylightName.CopyTo(DaylightName, 0);
StandardName = new char[32];
tzi.StandardName.CopyTo(StandardName, 0);
}
} struct REG_TIME_ZONE_INFORMATION
{
public readonly int Bias;
public readonly int DaylightBias;
public readonly SYSTEMTIME DaylightDate;
public readonly int StandardBias;
public readonly SYSTEMTIME StandardDate; public REG_TIME_ZONE_INFORMATION(byte[] stream)
{
//Bias
// StandardBias
//DaylightBias
//StandardDate
//DaylightDatetry
try
{
Bias = stream[0] + (stream[1] << 8) + (stream[2] << 16) + (stream[3] << 24);
StandardBias = stream[4] + (stream[5] << 8) + (stream[6] << 16) + (stream[7] << 24);
DaylightBias = stream[8] + (stream[9] << 8) + (stream[10] << 16) + (stream[11] << 24);
StandardDate.Year = (short)(stream[12] + (stream[13] << 8));
StandardDate.Month = (short)(stream[14] + (stream[15] << 8));
StandardDate.DayOfWeek = (short)(stream[16] + (stream[17] << 8));
StandardDate.Day = (short)(stream[18] + (stream[19] << 8));
StandardDate.Hour = (short)(stream[20] + (stream[21] << 8));
StandardDate.Minute = (short)(stream[22] + (stream[23] << 8));
StandardDate.Second = (short)(stream[24] + (stream[25] << 8));
StandardDate.Milliseconds = (short)(stream[26]+ (stream[27] << 8));
DaylightDate.Year = (short)(stream[28] + (stream[29] << 8));
DaylightDate.Month = (short)(stream[30] + (stream[31] << 8));
DaylightDate.DayOfWeek = (short)(stream[32] + (stream[33] << 8));
DaylightDate.Day = (short)(stream[34] + (stream[35] << 8));
DaylightDate.Hour = (short)(stream[36] + (stream[37] << 8));
DaylightDate.Minute = (short)(stream[38] + (stream[39] << 8));
DaylightDate.Second = (short)(stream[40] + (stream[41] << 8));
DaylightDate.Milliseconds = (short)(stream[42] + (stream[43] << 8));
}
catch(Exception e)
{
throw new ArgumentException("REG_TIME_ZONE_INFORMATION initialization failed", e);
}
}
} struct TimeZoneInfo
{
public readonly string DaylightName;
public readonly string Display;
public readonly string Name;
public readonly string StandardName;
TIME_ZONE_INFORMATION m_TZI; public TimeZoneInfo(string name, string display, string daylightName, string standardName, REG_TIME_ZONE_INFORMATION rtzi)
{
DaylightName = daylightName;
Display = display;
Name = name;
StandardName = standardName;
m_TZI = new TIME_ZONE_INFORMATION();
m_TZI.Bias = rtzi.Bias;
m_TZI.DaylightBias = rtzi.DaylightBias;
m_TZI.DaylightDate = rtzi.DaylightDate;
m_TZI.DaylightName = new char[32];
DaylightName.ToCharArray().CopyTo(m_TZI.DaylightName, 0);
m_TZI.StandardBias = rtzi.StandardBias;
m_TZI.StandardDate = rtzi.StandardDate;
m_TZI.StandardName = new char[32];
StandardName.ToCharArray().CopyTo(m_TZI.StandardName, 0);
} public TIME_ZONE_INFORMATION TZI
{
get
{
return new TIME_ZONE_INFORMATION(m_TZI);
}
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
const string c_WinNTKey = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones";
const string c_Win9xKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones";
const string c_KeyDisplay = "Display";
const string c_KeyDayLightName = "Dlt";
const string c_KeyStandardName = "Std";
const string c_KeyTZI = "TZI";
static ArrayList s_TimeZones = new ArrayList();
/// <summary>
/// The main entry point for the application.
/// </summary>
[DllImport("Kernel32.dll")]
static extern bool TzSpecificLocalTimeToSystemTime(ref TIME_ZONE_INFORMATION lpTimeZoneInformation, ref SYSTEMTIME lpLocalTime, ref SYSTEMTIME lpUniversalTime); [DllImport("Kernel32.dll")]
static extern bool SystemTimeToTzSpecificLocalTime(ref TIME_ZONE_INFORMATION lpTimeZone, ref SYSTEMTIME lpUniversalTime, ref SYSTEMTIME lpLocalTime); [STAThread]
static void Main(string[] args)
{
switch(Environment.OSVersion.Platform)
{
case PlatformID.Win32NT:
RetrieveWinNTTimeZones();
break;
case PlatformID.Win32Windows:
break;
default:
break;
}
//Test
DateTime dtUTC = DateTime.Now.ToUniversalTime();
SYSTEMTIME stUTC = dtUTC;
SYSTEMTIME st = new SYSTEMTIME();
TIME_ZONE_INFORMATION tzi; Console.WriteLine("{0,-50}{1}", "UTC", dtUTC.ToString());
foreach(TimeZoneInfo dzi in s_TimeZones)
{
tzi = dzi.TZI;
SystemTimeToTzSpecificLocalTime(ref tzi, ref stUTC, ref st);
Console.WriteLine("{0, -50}{1}", dzi.Name, ((DateTime)st).ToString());
}
} static void RetrieveWinNTTimeZones()
{
using(RegistryKey baseKey = Registry.LocalMachine.OpenSubKey(c_WinNTKey))
{
if(baseKey == null)
{
throw new InvalidOperationException(string.Format("Registry key \"{0}\" open failed", c_WinNTKey));
}
string[] tzNames = baseKey.GetSubKeyNames();
string tzDisplay;
string tzDaylightName;
string tzStandardName;
byte[] tzTZI; foreach(string tzName in tzNames)
{
using(RegistryKey tzKey = baseKey.OpenSubKey(tzName))
{
tzDisplay = tzKey.GetValue(c_KeyDisplay, string.Empty) as string;
tzDaylightName = tzKey.GetValue(c_KeyDayLightName, string.Empty) as string;
tzStandardName = tzKey.GetValue(c_KeyStandardName, string.Empty) as string;
tzTZI = tzKey.GetValue(c_KeyTZI, string.Empty) as byte[];
s_TimeZones.Add(new TimeZoneInfo(tzName, tzDisplay, tzDaylightName, tzStandardName, new REG_TIME_ZONE_INFORMATION(tzTZI)));
}
}
}
} static void RetrieveWin9xTimeZones()
{
}
}
}
未处理的“System.ArgumentException”类型的异常出现在 ConsoleApplication26.exe 中。其他信息: 未能封送类型,因为嵌入的字符串未能从 Unicode 转换为 ANSI。这个是什么问题呀?请psn(psn)再帮我看下好不?谢谢!
系统:Microsoft Windows Server 2003
Enterprise Edition
开发工具:Microsoft Visual Studio .NET 2003