Skip to content

Commit

Permalink
Improve support for v8's Date.toString time zone
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Dec 12, 2021
1 parent 5a1db7b commit dee584f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
41 changes: 38 additions & 3 deletions strftime.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

var Locales = {
de_DE: {
identifier: 'de-DE',
days: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
shortDays: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
months: ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
Expand All @@ -35,6 +36,7 @@
},

en_CA: {
identifier: 'en-CA',
days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
Expand Down Expand Up @@ -63,6 +65,7 @@
},

en_US: {
identifier: 'en-US',
days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
Expand Down Expand Up @@ -91,6 +94,7 @@
},

es_MX: {
identifier: 'es-MX',
days: ['domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado'],
shortDays: ['dom', 'lun', 'mar', 'mié', 'jue', 'vie', 'sáb'],
months: ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre',' diciembre'],
Expand All @@ -113,6 +117,7 @@
},

fr_FR: {
identifier: 'fr-FR',
days: ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'],
shortDays: ['dim.', 'lun.', 'mar.', 'mer.', 'jeu.', 'ven.', 'sam.'],
months: ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
Expand All @@ -135,6 +140,7 @@
},

it_IT: {
identifier: 'it-IT',
days: ['domenica', 'lunedì', 'martedì', 'mercoledì', 'giovedì', 'venerdì', 'sabato'],
shortDays: ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'],
months: ['gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre'],
Expand All @@ -157,6 +163,7 @@
},

nl_NL: {
identifier: 'nl-NL',
days: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
shortDays: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
months: ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
Expand All @@ -179,6 +186,7 @@
},

pt_BR: {
identifier: 'pt-BR',
days: ['domingo', 'segunda', 'terça', 'quarta', 'quinta', 'sexta', 'sábado'],
shortDays: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb'],
months: ['janeiro', 'fevereiro', 'março', 'abril', 'maio', 'junho', 'julho', 'agosto', 'setembro', 'outubro', 'novembro', 'dezembro'],
Expand All @@ -201,6 +209,7 @@
},

ru_RU: {
identifier: 'ru-RU',
days: ['Воскресенье', 'Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота'],
shortDays: ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'],
months: ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'],
Expand All @@ -223,6 +232,7 @@
},

tr_TR: {
identifier: 'tr-TR',
days: ['Pazar', 'Pazartesi', 'Salı','Çarşamba', 'Perşembe', 'Cuma', 'Cumartesi'],
shortDays: ['Paz', 'Pzt', 'Sal', 'Çrş', 'Prş', 'Cum', 'Cts'],
months: ['Ocak', 'Şubat', 'Mart', 'Nisan', 'Mayıs', 'Haziran', 'Temmuz', 'Ağustos', 'Eylül', 'Ekim', 'Kasım', 'Aralık'],
Expand All @@ -247,6 +257,7 @@
// By michaeljayt<[email protected]>
// https://github.com/michaeljayt/strftime/commit/bcb4c12743811d51e568175aa7bff3fd2a77cef3
zh_CN: {
identifier: 'zh-CN',
days: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
shortDays: ['日', '一', '二', '三', '四', '五', '六'],
months: ['一月份', '二月份', '三月份', '四月份', '五月份', '六月份', '七月份', '八月份', '九月份', '十月份', '十一月份', '十二月份'],
Expand Down Expand Up @@ -499,9 +510,8 @@
resultString += "GMT";
}
else {
// fixme optimize
var tzString = date.toString().match(/\(([\w\s]+)\)/);
resultString += tzString && tzString[1] || '';
var tzName = getTimezoneName(date);
resultString += tzName || '';
}
break;

Expand Down Expand Up @@ -809,6 +819,31 @@
return (date.getTimezoneOffset() || 0) * 60000;
}

// Tries to get a short timezone name using Date.toLocaleString, falling back on the platform default
// using Date.toString if necessary.
function getTimezoneName(date, localeIdentifier) {
return getShortTimezoneName(date, localeIdentifier) || getDefaultTimezoneName(date);
}

// Unfortunately this returns GMT+2 when running with `TZ=Europe/Amsterdam node test.js` so it's not
// perfect.
function getShortTimezoneName(date, localeIdentifier) {
if (localeIdentifier == null) return null;

var tzString = date
.toLocaleString(localeIdentifier, { timeZoneName: 'short' })
.match(/\s([\w]+)$/);
return tzString && tzString[1];
}

// This varies by platform so it's not an ideal way to get the time zone name. On most platforms it's
// a short name but in Node v10+ and Chrome 66+ it's a long name now. Prefer getShortTimezoneName(date)
// where possible.
function getDefaultTimezoneName(date) {
var tzString = date.toString().match(/\(([\w\s]+)\)/);
return tzString && tzString[1];
}

function warn(message) {
if (typeof console !== 'undefined' && typeof console.warn == 'function') {
console.warn(message)
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if (process.env.TZ == 'America/Vancouver') {
testTimezone('America/Vancouver');
assert.format('%C', '01', '01', new Date(100, 0, 1));
assert.format('%X', '11:51:45 AM', '06:51:45 PM');
assert.format('%c', 'Tue 07 Jun 2011 11:51:45 AM PDT', 'Tue 07 Jun 2011 06:51:45 PM GMT');
assert.format('%c', 'Tue 07 Jun 2011 11:51:45 AM Pacific Daylight Time', 'Tue 07 Jun 2011 06:51:45 PM GMT');
assert.format('%j', '097', '098', new Date(1365390736236));
assert.format('%x', '06/07/11');
assert.format('%U', '12', null, new Date('2017-03-25 00:00:00 +0000'));
Expand All @@ -57,7 +57,7 @@ else if (process.env.TZ == 'Europe/Amsterdam') {
testTimezone('Europe/Amsterdam');
assert.format('%C', '01', '00', new Date(100, 0, 1));
assert.format('%X', '08:51:45 PM', '06:51:45 PM');
assert.format('%c', 'Tue 07 Jun 2011 08:51:45 PM CEST', 'Tue 07 Jun 2011 06:51:45 PM GMT');
assert.format('%c', 'Tue 07 Jun 2011 08:51:45 PM Central European Summer Time', 'Tue 07 Jun 2011 06:51:45 PM GMT');
assert.format('%j', '098', '098', new Date(1365390736236));
assert.format('%x', '06/07/11');
assert.format('%U', '12', null, new Date('2017-03-25 00:00:00 +0000'));
Expand Down

0 comments on commit dee584f

Please sign in to comment.