asp.net C#如何实现当地时区转换的代码实例 对于处理日期和时间的任何应用程序而言,正确处理不同时区之间的差异愈发重要。 应用程序不能再假定所有时间都可以表示为本地时间(datetime 结构中的时间)。例如,显示美国东部当前时间的网页对于东亚地区的客户来说便缺乏可信度。本主题将说明如何在不同时区之间转换时间,以及如何转换可提供有 限时区识别能力的 datetimeoffset 值。
static void main(string[] args)
{
//将本地时间转换为美国标准中部时间
datetime dt=timezoneinfo.converttimetoutc(datetime.now, timezoneinfo.local);
console.writeline(dt.tostring());
dt = timezoneinfo.converttimefromutc(dt, timezoneinfo.findsystemtimezonebyid("central america standard time"));
console.writeline(dt.tostring());
//列举所有支持的时区列表
readonlycollection<timezoneinfo> lst=timezoneinfo.getsystemtimezones();
foreach(timezoneinfo tzi in lst)
{
console.writeline(tzi.id);
}
}
代码二
using system;
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;//www.3ppt.com
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 = @ "softwaremicrosoftwindows ntcurrentversiontime zones ";
const string c_win9xkey = @ "softwaremicrosoftwindowscurrentversiontime 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()
{
}
}
}
注意
还可以序列化 datetimeoffset 结构,以便明确地表示单个时间点。 由于 datetimeoffset 对象存储了日期和时间值及其 utc 偏移量,因此它们表示的特定时间点始终与 utc 有关。
若要将时间转换为 utc,最简单的方法是调用 static(在 visual basic 中为 shared)timezoneinfo.converttimetoutc(datetime) 方法。 该方法所执行的具体转换取决于 datetime 参数的 kind 属性值