Merge pull request #701 from pmelanson/700-date-change-format-fallback

#700 added fallback mode for unsupported .toLocaleDateString()
This commit is contained in:
Lorenz Hübschle-Schneider 2016-01-06 22:09:03 +01:00
commit ef8d32221b
2 changed files with 41 additions and 4 deletions

View file

@ -761,6 +761,23 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
} }
}; };
$rootScope.supports_formatting_date = (function() {
// function toLocaleDateStringSupportsLocales taken from MDN:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString#Checking_for_support_for_locales_and_options_arguments
try {
new Date().toLocaleDateString('i');
} catch (e) {
if (e.name !== 'RangeError') {
$log.info("Browser does not support toLocaleDateString()," +
" falling back to en-US");
}
return e.name === 'RangeError';
}
$log.info("Browser does not support toLocaleDateString()," +
" falling back to en-US");
return false;
})();
// Prevent user from accidentally leaving the page // Prevent user from accidentally leaving the page
window.onbeforeunload = function(event) { window.onbeforeunload = function(event) {

View file

@ -59,8 +59,16 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
var content = "\u001943"; // this colour corresponds to chat_day_change var content = "\u001943"; // this colour corresponds to chat_day_change
// Add day of the week // Add day of the week
content += new_date.toLocaleDateString(window.navigator.language, if ($rootScope.supports_formatting_date) {
{weekday: "long"}); content += new_date.toLocaleDateString(window.navigator.language,
{weekday: "long"});
} else {
// Gross code that only does English dates ew gross
var dow_to_word = [
"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"];
content += dow_to_word[new_date.getDay()];
}
// if you're testing different date formats, // if you're testing different date formats,
// make sure to test different locales such as "en-US", // make sure to test different locales such as "en-US",
// "en-US-u-ca-persian" (which has different weekdays, year 0, and an ERA) // "en-US-u-ca-persian" (which has different weekdays, year 0, and an ERA)
@ -73,8 +81,20 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
extra_date_format.year = "numeric"; extra_date_format.year = "numeric";
} }
content += " ("; content += " (";
content += new_date.toLocaleDateString(window.navigator.language, if ($rootScope.supports_formatting_date) {
extra_date_format); content += new_date.toLocaleDateString(window.navigator.language,
extra_date_format);
} else {
// ew ew not more gross code
var month_to_word = [
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"];
content += month_to_word[new_date.getMonth()] + " " + new_date.getDate().toString();
if (extra_date_format.year === "numeric") {
content += ", " + new_date.getFullYear().toString();
}
}
// Result should be something like // Result should be something like
// Friday (November 27) // Friday (November 27)
// or if the year is different, // or if the year is different,