I am building a time-sensitive web application where the users input time related data in local time, and it gets saved in UTC format in the database. The app, so far, uses timezone that the browser provides (which I assume uses the timezone that the OS provides). The problem appears when the system timezone is manually set by the owner of the computer and it may be wrong. A potential solution would be get correct timezone from the internet. But I would rather not call an API somewhere just to ascertain correct timezone settings every time I need it (unless there is no other option). Can anyone suggest a proper way to deal with this problem?
Asked
Active
Viewed 51 times
-1
-
you can make an api call – brk Aug 31 '18 at 18:38
-
@brk what?? Like what kind of API call? – Pointy Aug 31 '18 at 18:38
-
4You ultimately cannot tell what should be "correct" for a client because everything you think you know about the client *comes from* the client. Just let them use whatever time zone they want. – Pointy Aug 31 '18 at 18:39
-
@Pointy but that would make reports incorrect (I intend to create customized reports using the data). And the client is an employee bound to an organization, so his wrong entry could hurt reports of others as well. – PPrasai Aug 31 '18 at 18:42
-
1Like Pointy said: There is no API for "second guess the system configuration". – paulsm4 Aug 31 '18 at 18:43
-
Well again, you **cannot** trust the client. If you update some server-side state based on client reports of timezone, that's probably a mistake to begin with. – Pointy Aug 31 '18 at 18:44
-
1@charlietfl that would probably be better but of course the client could have a clock that's accidentally or deliberately wildly out of whack. – Pointy Aug 31 '18 at 18:45
-
1@charlietfl - Yes, in general one SHOULD use UTC between hosts. However, if the user set his time zone "incorrectly" ... then the UTC time is likely to be off by that amount, too. It's a good idea - but it doesn't solve this particular problem. – paulsm4 Aug 31 '18 at 18:46
-
so I just need to trust the consumers of the application? – PPrasai Aug 31 '18 at 18:47
-
2well your server certainly knows what time it is when the HTTP request comes in. – Pointy Aug 31 '18 at 18:51
-
Could have user confirm there timezone in initial app setup and store it then periodically check how in sync it stays – charlietfl Aug 31 '18 at 18:56
-
@Pointy that's probably a better idea. if the client cannot do a garbage-input, there cannot be a garbage-output – PPrasai Aug 31 '18 at 19:01
1 Answers
1
I don't know what timings you're trying to capture, but if it's actions the user will take on your app you start a JS timer at 0, then instead of fixed timestamps, you store the offset from zero, submit that all to your app (along with the total seconds captured).
From that point you can reverse engineer the offset from your server time to workout when things happened and transition it to the correct timezone using the client's local timezone settings.
For example, if they performed actions at: 1s, 60s and 120s and the total time on your app was 200s, you could do the following:
first action is current_timestamp - 200s + 1s
second action is current_timestamp - 200s + 60s
third action is current_timestamp - 200s + 120s
That'll give you the correct timestamp for you to swap out timezones as you please.

Royal Wares
- 1,192
- 10
- 23
-
It's also worth noting that data from the client should always be treated as though they made it up, so add reasonable checks to ensure you're not putting total trust into what's coming through. I.e. if something ran for '10 years' but your app has only been running for 24 hours, that's fishy. – Royal Wares Aug 31 '18 at 19:07
-
How would this help with determining the time zone? Time zone is not the same as offset. See "time zone != offset" in [the timezone tag wiki](https://stackoverflow.com/tags/timezone/info) – Matt Johnson-Pint Sep 04 '18 at 17:52
-
@MattJohnson I think you may have misunderstood how I'm using offsets here, the offsets do not relate to timezones, they are so that you can place actions in an absolute point in time against a UNIX timestamp. Only after you have done that can you look to find the local timezone code and convert the unix timestamp to a local time. https://stackoverflow.com/questions/23062515/do-unix-timestamps-change-across-timezones – Royal Wares Sep 05 '18 at 07:09
-
-
@MattJohnson I disagree, if you read the whole question you can see it ends in "Can anyone suggest a proper way to deal with this problem?" which is not limited to JS, if the questions was "I only want to use JS" then the answer would have been "Well you can't achieve that reliably". – Royal Wares Sep 05 '18 at 14:18
-
No, I mean that the question was specifically how to find the correct time zone, not how to determine the offsets between the UTC time of the client clock and the server clock. – Matt Johnson-Pint Sep 05 '18 at 14:49