How to set time zone for Visual Studio and browser

2019-08-20 17:35发布

问题:

This question already has an answer here:

  • How to use a custom time in browser to test for client vs server time difference 7 answers

I am using visual studio 2015 Community edition. Is there any way to run visual studio in one timezone and browser in other time zone? Lets say i want to run browser in CST time zone and Visual Studio in UTC time zone

Here is what i have tried so far ( unsuccessfully )

Lets say visual studio executes under current user

Method 1> Set current user's time zone to CST. Start the browser. Then Change current user TimeZone to UTC and Start VS in debugging mode. I was hoping browser will cache the time zone as CST when it was started, but that is not the case.

Method 2> Create a new user on local machine. Set its time zone to CST and set current user's time zone to UTC. Start the browser using different user as new user. And start VS in debugging mode as current user

Both methods did not work. The browsers i am using are IE and Chrome

回答1:

Changing the time zone on Windows changes it system-wide. You cannot have app A run in one time zone and app B run in another.

There are a few exceptions to this:

  • Some programs are written using POSIX C APIs, which look for the TZ environment variable. While this works reasonably well on Linux/OSX, unfortunately this approach is buggy on Windows and should not be relied upon.

  • Some programs will read the system time zone once on startup and then hold on to it for the lifetime of the application. Many web browsers used to do this, but then people reported bugs when they moved from one time zone to another and the browser didn't update correctly, and such behavior was subsequently fixed. So most web browser on Windows now listen for WM_TIMECHANGE messages, which fire whenever the time or time zone are updated.

So in general, no - there's no way to accomplish what you asked for. If you are trying to test how your web site behaves in different time zones, you will need to use more than one computer. One approach is to run the web browser in a virtual machine, as you can change the time zone of the VM without it affecting the host computer's time zone.

Alternatively, consider running your tests on Linux. You can set the TZ environment variable to any valid IANA time zone ID, and you can set environment variables differently per process.