ASP tutorial. NET C # time zone conversion two instances
For any application that processes dates and times, it is increasingly important to correctly handle differences between zones. An application cannot assume that all time can be represented as a local time (a time in a datetime structure). For example, a Web page showing the current time in the eastern United States lacks credibility for customers in the East Asian region. This topic explains how to convert time between different time zones and how to convert DateTimeOffset values that provide limited time zone recognition.
static void Main (string[] args)
{
Convert local time to U.S. standard Central Time
DateTime DT=TIMEZONEINFO.CONVERTTIMETOUTC (DateTime.Now, timezoneinfo.local);
Console.WriteLine (Dt.tostring ());
DT = TIMEZONEINFO.CONVERTTIMEFROMUTC (dt, Timezoneinfo.findsystemtimezonebyid ("America Standard Time"));
Console.WriteLine (Dt.tostring ());
Enumerate all supported time zone lists
Readonlycollection<timezoneinfo> Lst=timezoneinfo.getsystemtimezones ();
foreach (TimeZoneInfo tzi in LST)
{
Console.WriteLine (tzi.id);
}
}
Code two
using System;
Using System.Collections;
Using System.runtime.interops tutorial ervices;
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 = )]
public char[] StandardName;
public systemtime standarddate;
public int StandardBias;
[MarshalAs (unmanagedtype.byvalarray, sizeconst = )]
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] < <) + (Stream[3] < < 2 4);
StandardBias = stream[4] + (Stream[5] < < 8) + (Stream[6] < <) + (Stream[7] < < 24);
DaylightBias = stream[8] + (Stream[9] < < 8) + (Stream[10] < <) + (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 = @ "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 is for the application.
</summary>
[DllImport ("kernel32.dll")]
static extern bool Tzspecificlocaltimetosystemtime (ref time_zone_information lptimezoneinformation, ref Syst Emtime lplocaltime, ref systemtime lpuniversaltime);
[DllImport ("kernel32.dll")]
static extern bool Systemtimetotzspecificlocaltime (ref time_zone_information Lptimezone, ref systemtime LP Universaltime, 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 = & nbsp 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 == & nbsp 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 ()
{
}
}
}
Attention
You can also serialize the DateTimeOffset structure to explicitly represent a single point in time. Because the DateTimeOffset object stores date and time values and their UTC offsets, they represent a specific point in time that is always associated with UTC.
The simplest way to convert a time to UTC is to invoke the static (shared in Visual Basic) TIMEZONEINFO.CONVERTTIMETOUTC (DateTime) method. The specific conversion performed by the method depends on the kind property value of the DateTime parameter