18

According to multiple postings, Microsoft enabled the ability to use an Application setting - WEBSITE_TIME_ZONE - to control the timezone of the web server.

To try this, I set this value to "Eastern Standard Time" which is my local time zone.

On an ASP.NET MVC Razor page, I added the following code:

DateTime.Now: @DateTime.Now
DateTimeOffset.Now: @DateTimeOffset.Now
DateTime.UtcNow: @DateTimeOffset.UtcNow

when I ran this last night at 5:10:07pm Eastern Standard Time, it gave the following output:

DateTime.Now: 6/18/2015 5:10:07 PM
DateTimeOffset.Now: 6/18/2015 5:10:07 PM +00:00
DateTime.UtcNow: 6/18/2015 9:10:07 PM

As you can see, the setting correctly allowed DateTime.Now to return the correct value in my timezone rather than UTC like Azure Websites/Web Apps usually do. DateTime.UtcNow has always returned the correct value for obvious reasons.

However, DateTimeOffset.Now returns the local time, but with an offset of +00:00 - almost as if the clock was changed rather than the timezone. This occurs even though the documentation says (emphasis mine):

Gets a DateTimeOffset object that is set to the current date and time on the current computer, with the offset set to the local time's offset from Coordinated Universal Time (UTC).

So what is happening that the WEBSITE_TIME_ZONE setting impacts DateTime.Now but it does not impact DateTimeOffset.Now? And is there any way I can get around that?

As a point of clarification, I don't really want to change the time zone on the server. We are working on a proper timezone independent solution. But I'm still curious why this happens the way it does.

6
  • 3
    Do you really really need to change the server's time zone? I would try as hard as possible to avoid that requirement. I agree it's weird behaviour of DateTimeOffset.Now, mind you... Commented Jun 19, 2015 at 14:00
  • I was debating explaining that in the OP. I would much prefer not to change the server's time zone. Just while exploring the options, I ran across this one and didn't understand how it worked (or why it doesn't work the way I thought). So I figured I would try to get clarification. Commented Jun 19, 2015 at 14:00
  • 1
    Makes sense - and I'm all for experimenting with things you wouldn't actually want to do :) Commented Jun 19, 2015 at 14:21
  • You might not be the first person to notice some weirdness there: blogs.msdn.com/b/tomholl/archive/2015/04/07/… i.sstatic.net/9WyvQ.png stackoverflow.com/a/29386241/397817 Commented Jun 19, 2015 at 17:28
  • WEBSITE_TIME_ZONE doesn't seem to be changing the time zone at all I've set the time zone to GMT, but I am still seeing UTC with adjusted time by an hour to match what the time would be in GMT: {"Id":"UTC","DisplayName":"(UTC) Coordinated Universal Time","StandardName":"Coordinated Universal Time","DaylightName":"Coordinated Universal Time","BaseUtcOffset":"00:00:00","AdjustmentRules":null,"SupportsDaylightSavingTime":false} Commented Jul 21, 2015 at 9:17

3 Answers 3

5

Well for me it works right now. If it is not working make sure you set right value

  1. Open regedit
  2. Search for "time zones"
  3. Check how your zone is written. For instance my zone is "Eastern EST (+2)" and in registry its name is "E. Europe Standard Time"

So I added to app settings in azure app "WEBSITE_TIME_ZONE: E. Europe Standard Time" and it works.

Sign up to request clarification or add additional context in comments.

3 Comments

This answer worked, but the regedit search is kind of silly (and the value I was looking for wasn't even in my registry). The values should be published somewhere instead of having to do that. Anyway, FYI for those in Japan, the value is "Tokyo Standard Time". There is a great blog post on this here (Jp only) blog.shibayan.jp/entry/20150909/1441725327
@starmandeluxe the best is to start using UTC, now i ended up that i must migrate to UTC :( it will require a bit of efford to do that.
thanks for the suggestion! Unfortunately I'm in the same boat as you are, working on a legacy codebase that can't be migrated instantly since releases need to keep happening. At this point I just need it to work.
4

For those who stumble upon this question. This has been fixed for a long time now.

Comments

0

I am running into the same problem. Unfortunately for me, I inherited a lot of legacy code that was written specifically for Eastern Standard Time, so updating it to work with UTC is essentially not an option.

After digging into the CLR code, it seems to be due to the fact that the local time zone is looked up against the system registry. Don't see how they can properly support this without a change to the CLR code since Azure Web Apps run on shared VMs.

For the time being, I will be getting around this with the following reflection hack at site startup to trick .Net into thinking it is in the Eastern Standard Time. It's not a great solution, and most likely will break if they change the implementation of the TimeZoneInfo class, but hopefully the reason for them changing it will be to address this issue.

// rewrite local timezone
var tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

var fInfo = typeof(TimeZoneInfo).GetField("s_cachedData", BindingFlags.Static|BindingFlags.NonPublic);
var cachedData = fInfo.GetValue(null);

fInfo = cachedData.GetType().GetField("m_localTimeZone", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
fInfo.SetValue(cachedData, tz);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.