So the other day, there I was minding my own business, when the request for a web part that showed the current Share Price for the US and GMT arrives on my desk.
No problem I think we have the source feed and it provides the information with the last updated date in GMT. Unfortunately they want the FTSE price in GMT and the NYSE in EST. Converting times accurately between time zones on a server with a differing time zone to the required 1 has been a notorious headache especially when you factor in daylight saving time.
This has been made easier with .NET 3X TimeZoneInformation class but I am not coding in .NET 3.x for Sharepoint were using 2.0, but I did find a Sharepoint equivalent.
The final solution was to store the time in UTC and then use the SPRegionalSettings.GlobalTimeZoneSettings[].UTCToLocalTime to convert the time to the appropriate time.
So for a date object like 26-FEB-08 16:56 GMT
Simply myDT=DateTime.Parse(“26-FEB-08 16:56 GMT”);
myUniversalDT=myDT.ToUniversalTime();
string estTime=SPRegionalSettings.GlobalTimeZones[14].UTCToLocalTime(myUniversalDT).ToString();
string gmtTime=SPRegionalSettings.GlobalTimeZones[28].UTCToLocalTime(myUniversalDT).ToString();
So there you go, this uses out of the box Sharepoint 3.0 functionality to convert UTC times to EST(14) and GMT(28) obviously this could be expanded to for loop through the objects and then you wouldn’t need to hard code the indexes.