Skip to content

Commit dee584f

Browse files
committed
Improve support for v8's Date.toString time zone
1 parent 5a1db7b commit dee584f

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

strftime.js

+38-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

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

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

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

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

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

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

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

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

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

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

@@ -809,6 +819,31 @@
809819
return (date.getTimezoneOffset() || 0) * 60000;
810820
}
811821

822+
// Tries to get a short timezone name using Date.toLocaleString, falling back on the platform default
823+
// using Date.toString if necessary.
824+
function getTimezoneName(date, localeIdentifier) {
825+
return getShortTimezoneName(date, localeIdentifier) || getDefaultTimezoneName(date);
826+
}
827+
828+
// Unfortunately this returns GMT+2 when running with `TZ=Europe/Amsterdam node test.js` so it's not
829+
// perfect.
830+
function getShortTimezoneName(date, localeIdentifier) {
831+
if (localeIdentifier == null) return null;
832+
833+
var tzString = date
834+
.toLocaleString(localeIdentifier, { timeZoneName: 'short' })
835+
.match(/\s([\w]+)$/);
836+
return tzString && tzString[1];
837+
}
838+
839+
// This varies by platform so it's not an ideal way to get the time zone name. On most platforms it's
840+
// a short name but in Node v10+ and Chrome 66+ it's a long name now. Prefer getShortTimezoneName(date)
841+
// where possible.
842+
function getDefaultTimezoneName(date) {
843+
var tzString = date.toString().match(/\(([\w\s]+)\)/);
844+
return tzString && tzString[1];
845+
}
846+
812847
function warn(message) {
813848
if (typeof console !== 'undefined' && typeof console.warn == 'function') {
814849
console.warn(message)

test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if (process.env.TZ == 'America/Vancouver') {
3838
testTimezone('America/Vancouver');
3939
assert.format('%C', '01', '01', new Date(100, 0, 1));
4040
assert.format('%X', '11:51:45 AM', '06:51:45 PM');
41-
assert.format('%c', 'Tue 07 Jun 2011 11:51:45 AM PDT', 'Tue 07 Jun 2011 06:51:45 PM GMT');
41+
assert.format('%c', 'Tue 07 Jun 2011 11:51:45 AM Pacific Daylight Time', 'Tue 07 Jun 2011 06:51:45 PM GMT');
4242
assert.format('%j', '097', '098', new Date(1365390736236));
4343
assert.format('%x', '06/07/11');
4444
assert.format('%U', '12', null, new Date('2017-03-25 00:00:00 +0000'));
@@ -57,7 +57,7 @@ else if (process.env.TZ == 'Europe/Amsterdam') {
5757
testTimezone('Europe/Amsterdam');
5858
assert.format('%C', '01', '00', new Date(100, 0, 1));
5959
assert.format('%X', '08:51:45 PM', '06:51:45 PM');
60-
assert.format('%c', 'Tue 07 Jun 2011 08:51:45 PM CEST', 'Tue 07 Jun 2011 06:51:45 PM GMT');
60+
assert.format('%c', 'Tue 07 Jun 2011 08:51:45 PM Central European Summer Time', 'Tue 07 Jun 2011 06:51:45 PM GMT');
6161
assert.format('%j', '098', '098', new Date(1365390736236));
6262
assert.format('%x', '06/07/11');
6363
assert.format('%U', '12', null, new Date('2017-03-25 00:00:00 +0000'));

0 commit comments

Comments
 (0)