Get information about the world's time zones and daylight savings Net2.0 under.

Source: Internet
Author: User
Tags time zones local time

Under. NET 3.5, there is a timezoneinfo class that makes it easy to transform time zones and time transitions. However, under. NET 2.0, it is inconvenient to only handle the current server time zone. Special systems are used worldwide and need to be considered for local time zones, especially daylight saving times. Otherwise, time will go wrong. How to solve this problem, it is necessary to manually handle the.

In fact, Windows time zone information is stored in the Windows Registry "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones" Just read the relevant information. You can achieve the same function as TimeZoneInfo. Below we will be through a small demo. Reads the world time zone information, calculates the time zone offset and the daylight saving time offset. and determines whether the time is in the daylight savings period of the timezone according to the user's selected timezone and input times.

First, we need to create two struct to record the relevant information, one is Sytemtime, one timezoneinformation. The code is as follows:

 [StructLayout (layoutkind.sequential)] 


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;


public void SetInfo (byte[] info)


{


if (info. Length!= marshal.sizeof (this))


{


throw new ArgumentException ("Information size is incorrect", "info");


}


Else


{


this.wyear = bitconverter.touint16 (info, 0);


This.wmonth = bitconverter.touint16 (info, 2);


This.wdayofweek = bitconverter.touint16 (info, 4);


This.wday = bitconverter.touint16 (info, 6);


This.whour = bitconverter.touint16 (info, 8);


This.wminute = bitconverter.touint16 (info, 10);


This.wsecond = bitconverter.touint16 (info, 12);


this.wmilliseconds = bitconverter.touint16 (info, 14);


}


}


public override bool Equals (object obj)


{


if (this. GetType () = = obj. GetType ())


{


Try


{


SYSTEMTIME objst = (SYSTEMTIME) obj;


if (this.wyear!= objst.wyear


|| This.wmonth!= Objst.wmonth


|| This.wdayofweek!= Objst.wdayofweek


|| This.wday!= Objst.wday


|| This.whour!= Objst.whour


|| This.wminute!= Objst.wminute


|| This.wsecond!= Objst.wsecond


|| This.wmilliseconds!= objst.wmilliseconds)


return false;


Else


return true;


}


Catch


{


return false;


}


}


Else


return false;


}


public override int GetHashCode ()


{


return base. GetHashCode ();


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.