tags:

views:

2958

answers:

3

I need to have a common function to convert UTC time to EDT. I have a server in India. An application in it needs to use EDT time for all time purposes.

I am using .NET 3.5.

I found this on some other forum.

DateTime eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(
        DateTime.UtcNow, "Eastern Standard Time");

When i tried with "Easten Daylight Time" I got an error.

"The time zone ID 'Eastern Daylight Time' was not found on the local computer".

Please help with this or any other solution.

+3  A: 

Eastern Daylight Time isn't the name of a "full" time zone - it's "half" a time zone, effectively, always 4 hours behind UTC. (There may be proper terminology for this, but I'm not aware of it.)

Why would you want to use EDT for times which don't have daylight savings applied? If you want a custom time zone that always has the same offset to UTC, use TimeZoneInfo.CreateCustomTimeZone.

Note that if you use get the Eastern Standard timezone (TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")) then that will still have daylight saving time applied appropriately (i.e. during summer).

For example:

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

// Prints True
Console.WriteLine(tzi.IsDaylightSavingTime(new DateTime(2009, 6, 1)));
// Prints False
Console.WriteLine(tzi.IsDaylightSavingTime(new DateTime(2009, 1, 1)));
Jon Skeet
Hi Jon, Thanks for ur reply. What would you suggest as best solution out of these 2? shall i create a custom timezone or use TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); considoring that it will take care of daylight saving.And how do i take care of this issue in Stored procedures which are using getdate() method and taking IST time?
Shetty
I don't know about stored procedures, to be honest - if possible, pass UTC into them and make them understand UTC. As for whether you should use a custom time zone of Eastern Standard Time, it depends on whether you want a time zone which is *always* 4 hours behind UTC, or one which matches Eastern Standard Time. You originally stated that you needed to use EDT "for all time purposes" - I suggest you check whether you *really* need EDT, or whether you need Eastern Standard Time.
Jon Skeet
Ok Jon. I was asked to use *Boston* city time as standard time for all time purposes,which falls under EDT. I was not much aware of day light saving.I wanted my application to take this time although it is running in IST server. Previously it was in a different server which was running in EDT time zone and i had no such issues.
Shetty
Boston is in Eastern Time - which uses EDT during summer time. So yes, using FindSystemTimeZoneById("Eastern Standard Time") is the way to go. I agree the whole thing is pretty confusing :(
Jon Skeet
Thanks. I will do it :)
Shetty
+1  A: 

I would have said that you should use UTC for calculations of time periods, so that you avoid issues of daylight saving time and then use LocalTime for display only.

DateTime.ToLocalTime for UTC to whatever the local time zone is and then DateTime.ToUniversalTime to convert from local time to UTC.

Edit after comment 1

Do I take it then that you're after displaying a different timezone to that of the server?

If you're using web pages to access your server then use HttpRequest.UserLanguages to help create a CultureInfo object and use that to parse your DateTime object. Look here for a full explanation:Microsoft link on displaying local user time for web pages.

If you're using client-server architecture then if the LocalTime call is on the client side it will display the LocalTime for the client. You then convert it to UTC to send back to your server.

Either way your server doesn't need to know where the client is so if you have multiple clients in multiple timezones then all calculations will match. It will also allow you to show the times in any timezone that you wish by use of different Culture objects.

Edit 2 copied my second comment

You can get time data in UTC format from the server. Then you can convert it using DateTime.ToLocalTime or DateTime.ToUniversalTime as requried. If you're including dates as well and need to cope with say US MM/dd/yyyy and european dd/MM/yyyy formats the you can use CultureInfo class to parse the DateTime value accordingly. It sounds like more work than what you have at the moment, but it would mean that if you move your server again then you don't need to recode the DateTime handling.

A new point

Another point to look at is clock synchronisation between the server and the clients using NTP (Network Time Protocol) or SNTP (Simple Network Time Protocol) if it is accurate enough. I don't know what OS you are using but this is used by Windows Server time services to synchronise networks.

ChrisBD
Hi Chris,Thanks for your reply.If i am not wrong,if the server is running in IST time zone, Datetime.ToLoalTime from UTC will give time in IST.isnt it?
Shetty
True. I've edited my answer. To explain how LocalTime wrt Client-server code and also pointers to displaying local time for web users.
ChrisBD
Hi Chris,You got it right. I am displaying a different timezone to that of the server. It is a client server architecture.But i do not want to rely on client system time coz all the client machine times may not be exactly same. where as if it uses server timing , it would be uniform for all clients.
Shetty
In which case obtain your time data in UTC format from the server. Then you can convert it using DateTime.ToLocalTime or DateTime.ToUniversalTime as requried. If you're including dates as well and need to cope with say US MM/dd/yyyy and european dd/MM/yyyy formats the you can use CultureInfo class to parse the DateTime value accordingly.It sounds like more work than what you have at the moment, but it would mean that if you move your server again then you don't need to recode the DateTime handling.
ChrisBD
Ok.thanks. I will try
Shetty
A: 

The cowboy method is to take the UTC time, subtract four hours' worth of seconds from it (the timezone offset), format it using a UTC formatting function, and slap a "EDT" label on it.

If you need to use Daylight Time sometimes and Standard Time other times, either make a lookup table of switchover dates, or use some calendar function.

Robert L