forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.js
More file actions
96 lines (81 loc) · 3.39 KB
/
clock.js
File metadata and controls
96 lines (81 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const moment = require('moment');
const japanese_client = require('../common_functions/country_base').japanese_client;
const Clock = (function () {
let clock_started = false;
const showLocalTimeOnHover = function(s) {
if (japanese_client()) return;
$(s || '.date').each(function(idx, ele) {
const gmtTimeStr = ele.textContent.replace('\n', ' ');
const localTime = moment.utc(gmtTimeStr, 'YYYY-MM-DD HH:mm:ss').local();
if (!localTime.isValid()) {
return;
}
const localTimeStr = localTime.format('YYYY-MM-DD HH:mm:ss Z');
$(ele).attr('data-balloon', localTimeStr);
});
};
const toJapanTimeIfNeeded = function(gmtTimeStr, showTimeZone, longcode, hideSeconds) {
let match;
if (longcode && longcode !== '') {
match = longcode.match(/((?:\d{4}-\d{2}-\d{2})\s?(\d{2}:\d{2}:\d{2})?(?:\sGMT)?)/);
if (!match) return longcode;
}
const jp_client = japanese_client();
let time;
if (typeof gmtTimeStr === 'number') {
time = moment.utc(gmtTimeStr * 1000);
} else if (gmtTimeStr) {
time = moment.utc(gmtTimeStr, 'YYYY-MM-DD HH:mm:ss');
} else {
time = moment.utc(match[0], 'YYYY-MM-DD HH:mm:ss');
}
if (!time.isValid()) {
return null;
}
const timeStr = time.utcOffset(jp_client ? '+09:00' : '+00:00').format((hideSeconds ? 'YYYY-MM-DD HH:mm' : 'YYYY-MM-DD HH:mm:ss') + (showTimeZone && showTimeZone !== '' ? jp_client ? ' zZ' : ' Z' : ''));
return (longcode ? longcode.replace(match[0], timeStr) : timeStr);
};
const start_clock_ws = function() {
const getTime = function() {
clock_started = true;
BinarySocket.send({ time: 1, passthrough: { client_time: moment().valueOf() } });
};
setInterval(getTime, 30000);
getTime();
};
const time_counter = function(response) {
if (isNaN(response.echo_req.passthrough.client_time) || response.error) {
start_clock_ws();
return;
}
clearTimeout(window.HeaderTimeUpdateTimeOutRef);
const $clock = $('#gmt-clock'),
start_timestamp = response.time,
pass = response.echo_req.passthrough.client_time;
const client_time_at_response = moment().valueOf(),
server_time_at_response = ((start_timestamp * 1000) + (client_time_at_response - pass));
const update_time = function() {
window.time = moment((server_time_at_response + moment().valueOf()) -
client_time_at_response).utc();
const timeStr = window.time.format('YYYY-MM-DD HH:mm') + ' GMT';
if (japanese_client()) {
$clock.html(toJapanTimeIfNeeded(timeStr, 1, '', 1));
} else {
$clock.html(timeStr);
showLocalTimeOnHover('#gmt-clock');
}
window.HeaderTimeUpdateTimeOutRef = setTimeout(update_time, 1000);
};
update_time();
};
return {
start_clock_ws : start_clock_ws,
time_counter : time_counter,
getClockStarted: function () { return clock_started; },
showLocalTimeOnHover: showLocalTimeOnHover,
toJapanTimeIfNeeded : toJapanTimeIfNeeded,
};
})();
module.exports = {
Clock: Clock,
};