Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeZoneInfo.ConvertTime doesn't Convert anything

Running asp.net mvc 2 on win 7 with .net 4.0

I have a controller action method that receives 2 DateTime objects from a form. The UI on the form uses the jQueryUi datepicker (not sure if that maters).

The user who fills out that form will ALWAYS be entering the date/time in Hawaiian time zone.

I want to convert that to UTC time and store it in a database.

When I call TimeZoneInfo.ConverTime(DateTime,TimeZoneInfo,TimeZoneInfo) it returns the exact same datetime as i passed into it without doing any conversion. I checked the debugger and the only thing that changed was it changed the the DateTime.Kind property to DateTimeKind.Utc.

public ActionResult New(ScheduleNew data){         
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById( "Hawaiian Standard Time" );

    DateTime start = TimeZoneInfo.ConvertTime(data.StartDate, tz, TimeZoneInfo.Utc);
    DateTime end = TimeZoneInfo.ConvertTime(data.EndDate, tz, TimeZoneInfo.Utc);
}

I've Tried an alternate version as well with the same results.

public ActionResult New(ScheduleNew data){
    DateTime start = new DateTime( data.StartDate.Year, data.StartDate.Month, data.StartDate.Day, data.StartDate.Hour, data.StartDate.Minute, data.StartDate.Second, DateTimeKind.Unspecified );
    DateTime end = new DateTime( data.EndDate.Year, data.EndDate.Month, data.EndDate.Day, data.EndDate.Hour, data.EndDate.Minute, data.EndDate.Second, DateTimeKind.Unspecified );

    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById( "Hawaiian Standard Time" );

    StartDate = TimeZoneInfo.ConvertTime(start, tz, TimeZoneInfo.Utc);
    EndDate = TimeZoneInfo.ConvertTime(end, tz, TimeZoneInfo.Utc),
}

ScheduleData is just a simple ViewModel class with two date time Properties called StartDate and EndDate.

I want to emphasize, I don't care where the server is located, I don't care where the user is located. The user will always enter time in the Hawaiian time zone, and the server should always convert that datetime to to UTC.

Basically what I want, is to add 10 hours to the time the user enters (hawaiian to utc) and I could achieve that by just calling .AddHours(10) and it would be just fine. But later on down the road I will need this app to be more flexible.

like image 240
Erick Avatar asked Sep 18 '10 02:09

Erick


2 Answers

If you want more flexible approach, try something like this:

 DateTime now = DateTime.UtcNow;

 TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");
 TimeSpan utcOffset = timeZoneInfo.GetUtcOffset(now);
 DateTime hawaiianTime = new DateTime(now.Ticks + utcOffset.Ticks, DateTimeKind.Local);

This should work correctly no matter what time zone you are using. By the way: If I were you, I would create start and end dates as DateTimeKind.Utc (and store all schedule data as UTC as well). I am not sure how GetUtcOffset() will work with unspecified time kind...

Update on GetUtcOffset()

It seems that GetUtcOffset() works exactly the same for DateTimes created with different DateTimeKind values. However, I haven't test it with invalid input (i.e. with dates that does not exist due to daylight saving time switching). I believe DateTimeKind.Utc is fairly save here, but it might for Local or Unspecified.

like image 86
Paweł Dyda Avatar answered Sep 22 '22 10:09

Paweł Dyda


I can't find any problems with you code... It looks like it should work. Have you tried the alternate of:

DateTime start = TimeZoneInfo.ConvertTimeToUtc(data.StartDat, tz);
like image 20
colithium Avatar answered Sep 22 '22 10:09

colithium