views:

91

answers:

3

Hi,

i'm a little lost in the timezone :)

I have data stored with the time in UTC. The server is in the Netherlands, so we live in utc+1 (now, with daylightsavingtime, in utc + 2)

Now a client says: give me the data from august 5th.

So i have to calculate the utc time from 'his time'. For that i have to know:

what is your utc offset (we stored that in his profile, let's say utc -6) are you in daylightsavingtime (because then we have to add +1 and make the utc offset -5)

Then my questions:

  1. Can i ask the .Net framework: does country XX have daylightsavingtime?

  2. Can i ask the .Net framework: is 08-05-2010T00:00:00 in country XXX daylightsavingtime at that moment?

i've been trying the .ToLocalTime(), but this only gives me the local time at the server, and that's not what i want, i want to calculate with the timezone of the user, and also with the fact that at that particular point in time, if he/she is in daylightsavingtime

I've also seen this VB example:

TimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time") 
Dim Dated As DateTime = TimeZoneInfo.ConvertTimeToUtc(TempDate, TimeZone) 

but imho this doesn't take into account that the user in this timezone is or is not in a daylightsavingtime (dst) country. For instance, one user in this timezone is in the netherlands having dst, and one other is in another country which has no dst.

+5  A: 

You can't ask the framework about a particular country - but you can ask about a particular time zone.

TimeZoneInfo does take DST into account. (Heck, it wouldn't have the IsDaylightSavingTime method otherwise.) If you've got two users, one of whom is currently observing DST and the other of whom isn't, then they aren't in the same time zone.

If you could specify which locations you're talking about, I could try to find out which time zones are involved. (It's generally easier to find out the Olson names, but it shouldn't be impossible to find out the Windows IDs.)

Jon Skeet
Ok, thanks. But are you saying that users have to change their timezone when the enter the DST? for example, normally i'm in GMT+1 Amsterdam, but then the the DST is entered, am i then in GMT + 2? Or am i in GMT + 1 in DST? If the former case, the user has to change his timezone in my app. (in that case it will work as you say, because the user which lives in a DST country does change his timezone to +2, and the user which lives in a non-DST country keeps his timezone at +1)
Michel
@Michel: No, the time zone describes when it enters and exits DST. For example, I'm in the time zone with the Olson name "Europe/London" - that's UTC for half the year, and UTC+1 for half the year. Your user in a DST country *doesn't* change time zone - their time zone changes offset from UTC. It's a very important distinction.
Jon Skeet
@Jon: ok, didn't know that the TimeZone has the knowledge of when the DST starts end ends. But: then also have to know where you live, because i can check the TimeZone if your zone is in DST right now, but i also have to know if your country (as there are multiple countries in your timezone) works with DST or not?
Michel
@Jon: There is also the problem that when you change DST to normal time, you have multiple 02.15 times. So when you say: it happened in my house at 02.15, i have to ask you: "Was it the first 02.15 or the second 02.15", to determine the right UTC time.
Michel
@Michel: No, you don't need to know the country. If your country doesn't observe DST, then you're not in the same time zone as a country which *does* observe DST. You just need to know the time zone. (And be aware that many countries operate in multiple time zones.) And yes, a given local time may have 0, 1 or 2 corresponding UTC times. It's just a natural consequence of the mess that humans have made of calendaring :( `TimeZoneInfo` has methods to handle this - `IsAmbiguousTime` and `GetAmbiguousTimeOffsets`.
Jon Skeet
Let's say that USA uses DST and Canada is not. Then those 2 counties could not be in the same time zone ? And yet they are one "above" (on the north) the other. Strange.
Petar Repac
@Jon: Thanks very much for your help. I've always seen the multiple entries for +1, but never thought it was for determing if you are in a DST zone or not. And the IsAmbiguousTime is a big helper.Thanks again.
Michel
@Petar: There are *several* time zones in the US, not just one. And yes, there can be different time zones for the same longitude.
Jon Skeet
+2  A: 

TimeZone class has a method IsDaylightSavingTime that take a date as parameter and return a boolean indicating if that date in in daylightsavingtime for that timezone.

Andrea Parodi
Thanks, but that only works for local times, in my case, in an asp.net server app, that will be the time of the location where the server is located. In that case i can check if the SERVER is in dst, but not if the user in a particular time zone is.
Michel
+1  A: 

I promote my comment to an answer. It appears to be a complicate problem. Look at http://www.timeanddate.com/time/dst2010.html, there is a table of DST in various country of the world. You cannot get this with static code because DST change not only from country to country, but also from year to year.

I guess you have to maintain a table with DST information and get information from it.

Or you can query a webservice to get such infos.

Look at http://www.earthtools.org/webservices.htm#timezone for a webservice that you can query to get time in various country that take in account DST (it work only for Western Europe time zones). Also look at http://www.geonames.org/export/web-services.html. You have to get your users geolocation to use these services.

Andrea Parodi
I've found this: 'timeZone.GetAdjustmentRules()[0].DaylightTransitionStart' which tells me when the dst starts in a particular zone, is that the same?
Michel
I look in MSDN. It's about the same, but according to info on http://www.timeanddate.com/time/dst2010.html DST information is a dynamic one, that change in time according to countries laws modification, so it cannot be "embedded" in a static framework. Anyway, I guess modification in laws doesn't happen so frequently, so if you don't bother on a mistake in the calculation once in a while, you can go with DaylightTransitionStart
Andrea Parodi