NodeGuy / server-date

Make the server's clock available in the browser.
http://www.nodeguy.com/serverdate/
Mozilla Public License 2.0
193 stars 52 forks source link

Changing local time will affect the server time ? #16

Closed mohamedpsx closed 9 years ago

mohamedpsx commented 10 years ago

i don't know if this is a bug or this is how the script work, but when you change your local time to inferieur time from the server time, the server time changes.

Any ideas why ? and if this is normal or not ? because i think it is not normal and server time should not be affected.

Thank you

NodeGuy commented 9 years ago

I'm sorry I didn't respond sooner; I just realized that I have to manually "watch" my own Github project to receive notifications.

The library keeps track of the server's time by remembering an offset of the local clock, so when you change the local clock then the ServerDate time will be wrong until the next time it queries the server (by default this happens automatically once per hour but you can increase the frequency).

I can't think of another way to do this aside from implementing a new time-keeping clock in JavaScript from scratch, which I doubt is feasible, but I'm open to suggestions if you have them.

I could also make the server-query function public so you could ask it to query the server's time manually after changing the local time.

curby commented 9 years ago

If we assume that the server clock will be more or less constant but the client clock may change, then any changes in client clock should trigger a resync.

Possible solution: each time the client time is gathered for display on the web page, detect changes in client clock (compare "current" to "last"). If the changes are outside of a given threshold (say -1s to +3s when we expect +1s difference between "current" and "last"), assume that the clock changed and that a rsync is needed.

Since ServerDate largely "inherits" from Date's functions, some of these may need to be "overloaded" to include the above change detection logic.

Edge/unusual cases:

  1. Computer sleep/resume may trigger a resync, but this is probably a good thing
  2. DST changes on client may trigger a (possibly unnecessary) resync

I haven't gone through the code to check if this is already possible, but an externally-accessible state variable would be nice, so the web page can display whether or not the JS engine is synchronizing, not synchronizing, or perhaps in another state (e.g. need to sync due to client clock change). This also may belong in another issue/request.

NodeGuy commented 9 years ago

If someone cares about this enough I'm open to discussing a pull request.