timerender: Extract method to compute exact date-time from `time`.

This commit is contained in:
Aman Agrawal 2020-06-26 15:23:06 +05:30 committed by Tim Abbott
parent e56910e22d
commit b0c4dd2c98
1 changed files with 15 additions and 7 deletions

View File

@ -278,6 +278,18 @@ exports.absolute_time = (function () {
};
}());
exports.get_full_datetime = function (time) {
// Convert to number of hours ahead/behind UTC.
// The sign of getTimezoneOffset() is reversed wrt
// the conventional meaning of UTC+n / UTC-n
const tz_offset = -time.getTimezoneOffset() / 60;
return {
date: time.toLocaleDateString(),
time: time.toLocaleTimeString() +
' (UTC' + (tz_offset < 0 ? '' : '+') + tz_offset + ')',
};
};
// XDate.toLocaleDateString and XDate.toLocaleTimeString are
// expensive, so we delay running the following code until we need
// the full date and time strings.
@ -287,14 +299,10 @@ exports.set_full_datetime = function timerender_set_full_datetime(message, time_
}
const time = new XDate(message.timestamp * 1000);
// Convert to number of hours ahead/behind UTC.
// The sign of getTimezoneOffset() is reversed wrt
// the conventional meaning of UTC+n / UTC-n
const tz_offset = -time.getTimezoneOffset() / 60;
const full_datetime = exports.get_full_datetime(time);
message.full_date_str = time.toLocaleDateString();
message.full_time_str = time.toLocaleTimeString() +
' (UTC' + (tz_offset < 0 ? '' : '+') + tz_offset + ')';
message.full_date_str = full_datetime.date;
message.full_time_str = full_datetime.time;
time_elem.attr('title', message.full_date_str + ' ' + message.full_time_str);
};