2

Im working on a project at the moment which has a lot of time scheduling and executing tasks based on those schedules.

Im just wondering if there is an easier way to test my app in a different timezone from my server when they're both running on my pc.

Id prefer not to be changing the timezones in the regional settings but rather some way to change the context of that one application.

The database stores everything as UTC but the application shows this to the user in their local time. But im in a UTC timezone so theyre the same for me.

CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • 2
    You should set up a virtual machine to test this behaviour. It is not possible to have two time zones on one computer. – etalon11 Feb 09 '16 at 11:43
  • 1) There's no UTC timezone. You may be at UTC+0 today, but during summer you'll be at UTC+1 2) *Don't* use DateTime, use DateTimeOffset so that at least your code doesn't have to guess about offsets. And if you really care about timezones, use NodaTime – Panagiotis Kanavos Feb 09 '16 at 16:47

1 Answers1

9

A couple of things:

  • A server-side application should never depend on the time zone of the local machine. That means you should never call DateTime.Now, TimeZoneInfo.Local, DateTime.ToLocalTime, DateTime.ToUniversalTime, or any other method or property that uses the machine-local time zone. If you notice changes in your application's behavior when the time zone is different, then your application code is incorrect, and should be fixed.

  • Scheduling properly cannot be done just in terms of UTC. You need to consider the time zone of the event being scheduled (not the machine's time zone). (More on this here) This is best done in the application layer, and thus you can create unit tests for different time zones in your code. You don't test by changing the machine's time zone, or some other master time zone setting.

    As an example, consider if I have a meeting every day at 10:00 AM in US Pacific time. I cannot schedule this in terms of UTC, because the equivalent UTC time will fluctuate between 18:00 and 17:00, depending on whether daylight saving time is in effect in the Pacific time zone on the specific occurrence of the meeting. Likewise, I cannot use the time zone of the server, because it might be UTC, or it might be set for Australia, Europe, South America, or any number of other places where the time zone and daylight saving time rules are completely different. The event is in Pacific time, the clock is in UTC, the translation between the two must be done at the application layer, and the event must be scheduled in Pacific time.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575