|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionTime-zone handling in .NET 2.0 is restricted to converting between UTC and local (usually server) time. However, in order to display BackgroundThe source code includes a library (basically a single class) and a sample application that shows how to use the library. The library reads the registry for time-zone names and basic information, and uses Interop to access the Windows system calls necessary to convert between the different time zones. Using the codeThe following lines are taken from the sample application: // list all time zones
TimeZoneInformation[] arrTzi = TimeZoneInformation.EnumZones();
foreach (TimeZoneInformation tzi in arrTzi)
{
Console.Write("name: " + tzi.Name);
Console.WriteLine(" --- now: " + tzi.FromUniversalTime(dt));
}
Each instance of the Converting between time-zones is as easy as this: TimeZoneInformation tziLocal = TimeZoneInformation.CurrentTimeZone;
TimeZoneInformation tziRemote = TimeZoneInformation.FromIndex(10);
// convert local time to UTC (could be abbreviated as DateTime.UtcNow)
DateTime dt = tziLocal.ToUniversalTime(DateTime.Now);
DateTime dt2 = tziRemote.FromUniversalTime(dt);
Points of interestI have noticed that the time-zone names change when there are Windows updates, so you do not want to rely on them. I am keeping my fingers crossed that the AcknowledgementsI found the basis for my code on the internet but cannot remember where (it has been about two years ago). If you are the one whose code snippets I borrowed, drop me an email and I'll name you here! HistoryNone yet.
|
|||||||||||||||||||||||||||||||||||||||||||||||