views:

1493

answers:

4

Hi

I am storing all my dates in UTC format in my database. I ask the user for their timezone and I want to use their time zone plus what I am guessing is the server time to figure out the UTC for them.

Once I have that I want to do a search to see what is range in the database using their newly converted UTC date.

but I always get this exception.

System.ArgumentException was unhandled by user code  
Message="The conversion could not be completed because the   
supplied DateTime did not have the Kind property set correctly.  
For example, when the Kind property is DateTimeKind.Local,   
the source time zone must be TimeZoneInfo.Local.  
Parameter name: sourceTimeZone"

I don't know why I am getting this.

I tried 2 ways

 TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById(id);
 // I also tried DateTime.UtcNow
 DateTime now = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local); 
 var utc = TimeZoneInfo.ConvertTimeToUtc(now , zone );

This failed so I tired

 DateTime now = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local); 
 var utc = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(now, 
                                           ZoneId, TimeZoneInfo.Utc.Id);

This also failed both with the same error. What am I doing wrong?

Edit would this work?

 DateTime localServerTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local);
 TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(id);

 var usersTime = TimeZoneInfo.ConvertTime(localServerTime, info);

 var utc = TimeZoneInfo.ConvertTimeToUtc(usersTime, userInfo);

Edit 2 @ Jon Skeet

Ya I was just thinking about that I might not even need to do all this. Time stuff confuses me right now so thats why the post may not be as clear as it should be. I never know what the heck DateTime.Now is getting (I tried to change my Timezone to another timezone and it kept getting my local time).

This is what I wanted my stuff to do. User comes to site adds some alert and it gets now saved as utc (before it was DateTime.Now then someone suggested to store everything UTC).

So before a user would come to my site and depending where my hosting server was it could be like on the next day. So if the alert was said to be shown on August 30th (their time) but with the time difference of the server they could come on August 29th and the alert would be shown.

So I wanted to combat that. So now I am not sure should I just store their local time then use this offset stuff? Or just store UTC time. With just storing UTC time still might be wrong since the user still probably would be thinking in local time and I am not sure how UTC really works it still could end up a difference of time.

Edit3

 var info = TimeZoneInfo.FindSystemTimeZoneById(id)

 DateTimeOffset usersTime = TimeZoneInfo.ConvertTime(DataBaseUTCDate,
                                             TimeZoneInfo.Utc, info);
+3  A: 

The DateTime structure supports only two timezones:

  • The local timezone the machine is running in.
  • and UTC.

Have a look at the DateTimeOffset structure.

var info = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");

DateTimeOffset localServerTime = DateTimeOffset.Now;

DateTimeOffset usersTime = TimeZoneInfo.ConvertTime(localServerTime, info);

DateTimeOffset utc = localServerTime.ToUniversalTime();

Console.WriteLine("Local Time:  {0}", localServerTime);
Console.WriteLine("User's Time: {0}", usersTime);
Console.WriteLine("UTC:         {0}", utc);

Output:

Local Time:  30.08.2009 20:48:17 +02:00
User's Time: 31.08.2009 03:48:17 +09:00
UTC:         30.08.2009 18:48:17 +00:00
dtb
That seems to work. Time stuff confuses I have to read up on that DAteTimeOffSet and why it works and that converting stuff does not. But I am wondering can't I just DateTime.UtcNow? like when I do it it converts my server time to utc date what is the same if I did your dateoffSet stuff(using West Africa Time). Or would they be different?
chobo2
As Jon Skeet says, if you just want the current time in UTC, just use `DateTime.UtcNow` or `DateTimeOffset.UtcNow`.
dtb
+3  A: 

You need to set the Kind to Unspecified, like this:

DateTime now = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
var utc = TimeZoneInfo.ConvertTimeToUtc(now , zone);

DateTimeKind.Local means the in local time zone, and not any other time zone. That's why you were getting the error.

SLaks
Ok I will try this. Would what I have just edited work? It seems to be off(West Africa TimeZone was off by about 10mins) when you go from one time zone to another but the UTC was the correct time.
chobo2
Hmm so it does not work because it does not know the right Timezone? Then I am still mssing something because I think I am getting the wrong utc times.
chobo2
I misread your code; it actually will work.
SLaks
Hmm ok but I guess it is alot of dancing around for nothing? Like it seems DateTime.UtcNow would get the same result. and I think if I just go with my code from one time zone to another it is off.
chobo2
+2  A: 

As dtb says, you should use DateTimeOffset if you want to store a date/time with a specific time zone.

However, it's not at all clear from your post that you really need to. You only give examples using DateTime.Now and you say you're guessing that you're using the server time. What time do you actually want? If you just want the current time in UTC, use DateTime.UtcNow or DateTimeOffset.UtcNow. You don't need to know the time zone to know the current UTC time, precisely because it's universal.

If you're getting a date/time from the user in some other way, please give more information - that way we'll be able to work out what you need to do. Otherwise we're just guessing.

Jon Skeet
see my original post Edit2
chobo2
A: 

UTC is just a time zone that everyone agreed on as the standard time zone. Specifically, it's a time zone that contains London, England. EDIT: Note that it's not the exact same time zone; for example, UTC has no DST. (Thanks, Jon Skeet)

The only special thing about UTC is that it's much easier to use in .Net than any other time zone (DateTime.UtcNow, DateTime.ToUniversalTime, and other members).

Therefore, as others have mentioned, the best thing for you to do is store all dates in UTC within your database, then convert to the user's local time (by writing TimeZoneInfo.ConvertTime(time, usersTimeZone) before displaying.


If you want to be fancier, you can geolocate your users' IP addresses to automatically guess their time zones.

SLaks
That will be future versions lol. Got to get the basics down first.
chobo2
Then what do you want to know now?
SLaks
So this would not give any time different then. Like first getting all alerts by UTC then changing them to to local time once found won't cause any alerts to be missed? Like their won't be some that where just some how where off by a couple mins or something?
chobo2
I am trying to figure out how to give all the alerts the user needs to see at the right time based on their timezone. Thats what I need.
chobo2
1: Yes.
SLaks
2: If so, this (or your edit3) is exactly what you need.
SLaks
UTC is *not* the same as the time zone in London. Most importantly, London has daylight savings applied in summer - so currently, despite being in the Europe/London time zone, I'm offset by an hour from UTC.
Jon Skeet
Fixed; thanks. Are there any other differences? (Of more than one second)
SLaks