2

I use a script that coverts Earth Time into an in-game time for a game (Eorzea Time). The script that was originally written is here.

My issue is that this pulls the Earth Time from the user's device clock. So if the clock is not correct on their device the conversion to Eorzea time is not accurate.

I'd like to pull Earth Time from a clock that is guaranteed to be accurate. I've been unable to figure out how to do this.

As an example. If you view this script alongside this site (www.ffxivclock.com) and change your time by even a minute or two...the jsfiddle clock will be off while the ffxivclock.com time will still be correct.

var E_TIME = 20.5714285714;
var global = {
    utcTime: null,
    eorzeaTime: null
};
window.setInterval(updateClock, Math.floor(1000 * 60 /  E_TIME));

function updateClock() {
    global.utcTime = new Date().getTime();
    var eo_timestamp = Math.floor(global.utcTime * E_TIME);
    global.eorzeaTime = new Date();
    global.eorzeaTime.setTime(eo_timestamp);
    showTime();
}

function showTime() {
    var d = new Date();
    d.setTime(global.eorzeaTime);
    var eTime = document.getElementById('e-time');
    var hours = d.getUTCHours();
    var ampm = hours > 11 ? "PM" : "AM";
    if(hours > 12)
    hours -= 12;
    hours = padLeft(hours);
    var minutes = d.getUTCMinutes();
    minutes = padLeft(minutes);
    eTime.innerHTML = hours + ":" + minutes + " " + ampm;
}
function padLeft(val){
    var str = "" + val;
    var pad = "00";
    return pad.substring(0, pad.length - str.length) + str;
}
updateClock();
iehrlich
  • 3,572
  • 4
  • 34
  • 43
Jay
  • 31
  • 4
  • 2
    Is hitting a clock API or a server out of the question? – mhodges Jun 29 '17 at 16:07
  • 3
    like get server time? – Huangism Jun 29 '17 at 16:08
  • 3
    If you want all clocks to be the same as "Eorzea Time" your code should get the time from the server running Eorzea. – Heretic Monkey Jun 29 '17 at 16:18
  • 1
    Or have the server insert its local time in a hidden field at the time of sending the page, and then at the browser calculate the difference between the server time and local time, and translate subsequent time samples using that. Granted it won't be accurate to milliseconds, but it may be good enough. – Nisarg Shah Jun 29 '17 at 16:19
  • 2
    Possible duplicate of [Getting the current GMT world time](https://stackoverflow.com/questions/489581/getting-the-current-gmt-world-time) – gforce301 Jun 29 '17 at 16:21

0 Answers0