Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Date/toLocale* to format date/time #475

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions core/code/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,28 @@ window.chat.renderDivider = function(text) {
}


window.chat.timeFormat = {
timeStyle: 'short'
};

window.chat.timeDateFormat = {
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit',
hour12: false,
fractionalSecondDigits: 3
};

window.chat.renderMsg = function(msg, nick, time, team, msgToPlayer, systemNarrowcast) {
var ta = unixTimeToHHmm(time);
var tb = unixTimeToDateTimeString(time, true);
var date = new Date(time);
var timeLong = date.toLocaleString(window.locale, window.chat.timeDateFormat);
//add <small> tags around the milliseconds
tb = (tb.slice(0,19)+'<small class="milliseconds">'+tb.slice(19)+'</small>').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
var repl = '<small class="milliseconds">$1</small>'.replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
timeLong = timeLong.replace(/(\.\d{3})$/, repl);

// help cursor via “#chat time”
var t = '<time title="'+tb+'" data-timestamp="'+time+'">'+ta+'</time>';
var t = '<time title="' + timeLong + '" data-timestamp="' + time + '">'
+ date.toLocaleTimeString(window.locale, window.chat.timeFormat)
+ '</time>';
if ( msgToPlayer )
{
t = '<div class="pl_nudge_date">' + t + '</div><div class="pl_nudge_pointy_spacer"></div>';
Expand Down
35 changes: 18 additions & 17 deletions core/code/region_scoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,30 +425,31 @@ window.RegionScoreboard = (function() {
}

function onTimer() {
var d = regionScore.getCheckpointEnd(regionScore.getLastCP() + 1) - (new Date());
$('#cycletimer',mainDialog).html(formatMinutes( Math.max(0,Math.floor(d/1000))) );
var d = regionScore.getCheckpointEnd(regionScore.getLastCP() + 1) - Date.now();
$('#cycletimer', mainDialog).html(formatMinutes( Math.max(0, d)) );
}

function formatMinutes(sec) {
var hours = Math.floor(sec / 3600);
var minutes = Math.floor((sec % 3600) / 60);
sec = sec % 60;

var time='';
time += hours + ':';
if (minutes<10) time += '0';
time += minutes;
time += ':';
if (sec<10) time += '0';
time += sec;
return time;
// https://stackoverflow.com/a/25279399/2520247
function formatMinutes (ms) {
// valid while ms value is not greater than 24h
return new Date(ms).toISOString().substr(12, 7);
}

this.timeFormat = {
hour: 'numeric', minute: 'numeric'
};

function formatHours(time) {
return ('0' + time.getHours()).slice(-2) + ':00';
return time.toLocaleTimeString(window.locale, this.timeFormat);
}

this.dateTimeFormat = {
month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit'
};

function formatDayHours(time) {
return ('0' + time.getDate()).slice(-2) + '.' + ('0' + (time.getMonth() + 1)).slice(-2) + ' ' + ('0' + time.getHours()).slice(-2) + ':00';
return time.toLocaleString(window.locale, this.dateTimeFormat);
}

function setup() {
Expand Down
4 changes: 3 additions & 1 deletion core/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,13 @@ em {

/* time */
#chat td:first-child, #chatinput td:first-child {
width: 44px;
width: 55px;
overflow: hidden;
padding-left: 2px;
padding-right: 2px;
color: #bbb;
white-space: nowrap;
text-align: right;
}

#chat time {
Expand Down
3 changes: 3 additions & 0 deletions core/total-conversion-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ window.TEAM_ENL = 2;
window.TEAM_TO_CSS = ['none', 'res', 'enl'];
window.TEAM_NAMES = ['Neutral', 'Resistance', 'Enlightened'];

// used in time/date formatting routines
window.locale = navigator.languages.slice();

// STORAGE ///////////////////////////////////////////////////////////
// global variables used for storage. Most likely READ ONLY. Proper
// way would be to encapsulate them in an anonymous function and write
Expand Down