Christofer Eliasson
Christofer Eliasson

Reputation: 33865

Incorrect TimeZone offset in Azure

Update - the problem didn't lie within the extension method

As it turned out, there was no actual problem with my extension method. When I feed it with a DateTime straight from the webserver like so DateTime.UtcNow.ToUserLocalTime() I realized that the method was actually producing the correct result. The problem was instead that I accidentally called it twice on the DateTimes that were displayed incorrectly - first in the controller and then again in the view. A really stupid mistake, realized after beating my head against the wall for hours. From now on it is called from the controller only.

I have a problem with a time zone conversion in an ASP.Net MVC3 application running in Azure. When I run the application locally, everything is working just fine, however when I run the exact same code in Azure, it ends up +1 hour off.

I have created an extension method for DateTime that I use to convert the DateTime according to the time zone chosen by the user. All DateTimes are stored in UTC.

public static DateTime ToUserLocalTime(this DateTime date)
{
    date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
    if (!string.IsNullOrEmpty(SecurityContext.CurrentUser.TimeZone))
    {
        var timezone = SecurityContext.CurrentUser.TimeZone;
        var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezone);
        return TimeZoneInfo.ConvertTime(date, timeZoneInfo);
    }

    return date;
}

SecurityContext.CurrentUser.TimeZone is a string-representation of the time zone, in my case W. Europe Standard Time.

When I get the DateTime 2011-12-02 13:46:22 from my local SQL-database and run it through my ToUserLocalTime method converting it to W. Europe Standard Time in my local environment I get 2011-12-02 14:46:22 which is correct. However when I run the exact same code in Azure, reading from my SQL-Azure db, which contains the exact same DateTime and chosen time zone I get 2011-12-02 15:46:22. This baffles me, why does it end up one hour off?

I really don't know how to debug this, since I cannot reproduce it locally. Is there a way to debug this in the Azure role, so that I can see what is really going on in the code? Something like the debugger in Visual Studio?

I tried to enable IntelliTrace when I publish the application, but when I did the application wouldn't start, complaining about a connection-error when connecting to the Azure-SQL db. I have no experience of IntelliTrace, would the generated logs be able to tell me anything about what is going on here? Would it be worth spending time getting InteliTrace to work in this case?

Any ideas on what might be wrong here, or how to debug it would be greatly appreciated.

Upvotes: 3

Views: 5912

Answers (1)

Jeow Li Huan
Jeow Li Huan

Reputation: 3796

What about using

return TimeZoneInfo.ConvertTimeFromUTCTime(date, timeZoneInfo);

instead of

return TimeZoneInfo.ConvertTime(date, timeZoneInfo);

Reference: http://social.msdn.microsoft.com/Forums/en-AU/windowsazuredevelopment/thread/04be9d07-fcdb-4bf2-b380-57c062382409

You could then remove the line date = DateTime.SpecifyKind(date, DateTimeKind.Utc);, assuming that your date always has the Unspecified kind

OR

Or could it be Daylight Saving messing things up? See http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/92696af4-5169-4015-90eb-79da3c5136c2/

To test this, try going the page that changes your timezone, by clicking on your computer clock and "Change date and time settings. Then uncheck/check the Automatically adjust time for Daylight Saving.

Upvotes: 4

Related Questions