4

I developed a web application (with java) and in it I have a business rule which requires saving the user time signature. But I need to save 3 times: Local timezone user, global timezone, and server timezone.

My problem is to test the local timezone of the user. Anyone know how I can simulate (only for a test in local machine) a different timezone to the browser, or server, for me to simulate this test?

Note: I thought I'd create a virtual machine and host my application on it, and take the test, it would have only to change the timezone of the virtual machine. So this process will require a long time, I want to know if there is any simple way.

Erico Souza
  • 276
  • 4
  • 18
  • 2
    Well, isn't as simple as changing clock time in Windows? It would simulate Client's time and timezone... – mlewandowski Mar 10 '16 at 12:04
  • 1
    You need to restart your browser after you change your system's timezone. – Benja Garrido Mar 10 '16 at 12:08
  • 1
    Relevant? http://sqa.stackexchange.com/questions/8838/faking-system-time-date-with-selenium-webdriver – Andy Turner Mar 10 '16 at 12:10
  • @mlewandowski if I change clock time in Windows, change local server too. My execution environment is local, for test – Erico Souza Mar 10 '16 at 12:17
  • 1
    @EricoSuoza How about changing local Tomcat timezone? http://stackoverflow.com/questions/28777322/how-to-change-tomcat-7s-server-timezone – mlewandowski Mar 10 '16 at 12:31
  • @BenjaGarrido - Only in some browsers. Many will pick up on the time zone change automatically. – Matt Johnson-Pint Mar 11 '16 at 16:17
  • 1
    for example chrome... this webrowser does not change automatically timezone... I think @MattJohnson – Benja Garrido Mar 11 '16 at 17:00
  • 1
    Actually, it does. Try it. :) It didn't used to, but they fixed that a while back. I can't recall which version. But yes - in general, there will be some that have to be restarted. – Matt Johnson-Pint Mar 11 '16 at 17:55
  • Guys, remember, I need the browser to be different from the server timezone. So I can not restart the browser, right? – Erico Souza Mar 11 '16 at 17:57
  • because I need to make sure that the database I will save the local time of the User, and also the local time server. – Erico Souza Mar 11 '16 at 17:58
  • 1
    @EricoSouza - Honestly, if you're doing it correctly, your server-side code should be completely unaffected by the time zone of the server. Relying on a server's local time zone setting is an anti-pattern. Only ever ask for UTC time from the server's clock, and do all time zone conversions yourself in application code. See also: http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices – Matt Johnson-Pint Mar 11 '16 at 18:05
  • @MattJohnson In my application I need to save in the database, 3 times, just when the user signs a test. These times are: 1 - local time client. 2 - Time of the server location. 3 - Global Time GMT. And I need to simulate and test it in my application in my logal machine. – Erico Souza Mar 11 '16 at 18:13
  • 1
    Right, I read that in your question. What I'm saying is that #2 *should* be irrelevant in the vast majority of scenarios. You may be [trying to solve problem X when really you should be looking at problem Y](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Matt Johnson-Pint Mar 11 '16 at 18:28
  • 1
    Looks like this was asked before, but only answered from a Linux perspective. I added a Windows answer to that question which may interest you. See the duplicate link. Thanks. – Matt Johnson-Pint Mar 11 '16 at 18:53

2 Answers2

3

I discovered that I can manually set the GMT by tomcat arguments. To easy:

-Duser.timezone=GMT+13

As I needed to simulate a zone far away time, I put GMT + 13. Then my server got GMT + 13, and my client (browser) with GMT-3

Erico Souza
  • 276
  • 4
  • 18
  • @MattJohnson, see my answer. Do you agree? – Erico Souza Mar 11 '16 at 18:14
  • 1
    I usually discourage using fixed time zone offsets. The world is much more complex than that. Be sure to read [the timezone tag wiki](http://stackoverflow.com/tags/timezone/info). – Matt Johnson-Pint Mar 11 '16 at 18:31
  • 1
    I also think that while this may accomplish your goal for Java/Tomcat - it doesn't solve the problem you asked about (changing the time zone of the *browser*). And as I alluded to in the original question comments, if your code behaves any differently whatsoever when you pass in this argument, then you're using the time APIs incorrectly in your code. Don't ask the server for it's local time - that should be irrelevant. – Matt Johnson-Pint Mar 11 '16 at 18:34
  • @MattJohnson I understood, your explanation is great. This my answer is only for a local test. When my application is in production server, and my client is signing a test and that customer is in Mozambique (timezone UTC + 2), and my server in Brazil (UTC-3), it is necessary I save both times the client signed the test. I don't set -Duser.timezone=GMT in server in production – Erico Souza Mar 11 '16 at 19:05
2

change your computer's time zone to user's time zone and restart your browser.

Darshan
  • 1,064
  • 7
  • 15