tags:

views:

78

answers:

4

Hi, I want to calculate the current time differences between US/Central timezone and British Summer Time. I mean, currently these both timezones have daylight savings going on, so they have a 6 hours time difference. But after Sunday October 31 2010, daylight savings will be off for British summer time, at which moment there will be a 5 hours time differences between these two timezones.

Is there any way I can calculate these varying time differences?

+2  A: 

You can create two datetime object from different timezones, a good example: http://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-fx-3-5

And calculate the Delta between them.

Am
Though I haven't used that exact method, but from your answers I got the basic idea. I have implemented that solution and it is almost 90% similar to Jon Skeet's answer. But since your answer gave me the idea, I am selecting it as the chosen answer. Thank you very much :-)
Night Shade
+2  A: 

Am has correct answer which will mostly work if you limit yourself to current date. In general depending on how correct you want to be it can be quite hard. This is because the DST in a timezone as of today can be different than what it was in history. Maybe before 01/04/1950 there was no British Summer Time. Or during the Sydney Olympics the DST dates were temporarily changed in year 2000. To take all these things into account you will need a historical DST/TimeZone database.

Pratik
No I don't need historical DST/TimeZone database, I have computed the time differences in these two areas and then used it for the necessary conversion, but thanks for your answer.
Night Shade
+2  A: 

The documentation for TimeZoneInfo.GetUtcOffset has an example that shows the calculation of the UTC offset for various time zones at different times. You just need to get the UTC Offset at the specific time of interest for each of your two time zones and compute the difference. See:

http://msdn.microsoft.com/en-us/library/bb396378.aspx

Andrew
Thank you very much....... :-) .
Night Shade
+3  A: 

Just to provide some concrete code for the answers given, here's some code to work out the current difference between me (in London) and my colleagues in Mountain View:

using System;

class Test
{
    static void Main()
    {
        var london = TimeZoneInfo.FindSystemTimeZoneById
            ("GMT Standard Time");
        var googleplex = TimeZoneInfo.FindSystemTimeZoneById
            ("Pacific Standard Time");

        var now = DateTimeOffset.UtcNow;
        TimeSpan londonOffset = london.GetUtcOffset(now);
        TimeSpan googleplexOffset = googleplex.GetUtcOffset(now);
        TimeSpan difference = londonOffset - googleplexOffset;        
        Console.WriteLine(difference);
    }
}
Jon Skeet
except Mountain View isn't in US Mountain Standard Time :-)
Andrew
@Andrew: Is it not? This is the problem with Windows IDs instead of zoneinfo names :( What's the right ID in this case? Just "Mountain Standard Time"? Or "Pacific Standard Time"? Editing to the latter...
Jon Skeet
@Jon, sorry, I should have been more explicit in my comment. I believe it should be Pacific Standard Time.
Andrew
@Andrew: No problem. Glad my third guess was correct... not sure why I didn't plump for Pacific to start with.
Jon Skeet
Thank you for your answer :-), +1
Night Shade