Skip to content

Commit 142a409

Browse files
authored
Merge pull request #286 from yangchristina/get-default-options
Compatibility with setDefaultOptions in date-fns
2 parents 9877487 + 6bb572a commit 142a409

3 files changed

Lines changed: 75 additions & 3 deletions

File tree

src/_lib/tzIntlTimeZoneName/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Locale } from 'date-fns'
1+
import { getDefaultOptions, type Locale } from 'date-fns'
22
import type { FormatOptionsWithTZ } from '../../index.js'
33

44
/**
@@ -11,7 +11,8 @@ export function tzIntlTimeZoneName(
1111
date: Date,
1212
options: FormatOptionsWithTZ
1313
): string | undefined {
14-
const dtf = getDTF(length, options.timeZone, options.locale)
14+
const defaultOptions = getDefaultOptions()
15+
const dtf = getDTF(length, options.timeZone, options.locale ?? defaultOptions.locale)
1516
return 'formatToParts' in dtf ? partsTimeZone(dtf, date) : hackyTimeZone(dtf, date)
1617
}
1718

src/_lib/tzIntlTimeZoneName/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import assert from 'power-assert'
22
import { tzIntlTimeZoneName } from './index.js'
3+
import { setDefaultOptions } from 'date-fns'
4+
import { enGB } from 'date-fns/locale'
35

46
describe('tzIntlTimeZoneName', function () {
57
it('returns the short time zone name', function () {
@@ -66,4 +68,18 @@ describe('tzIntlTimeZoneName', function () {
6668
var result = tzIntlTimeZoneName('short', date, { timeZone, locale })
6769
assert.equal(result, 'GMT+2')
6870
})
71+
72+
describe('setDefaultOptions for locale', function () {
73+
it('returns the short time zone name in the specified locale', function () {
74+
setDefaultOptions({ locale: enGB })
75+
var date = new Date('2014-10-25T13:46:20Z')
76+
var timeZone = 'Europe/Paris'
77+
var result = tzIntlTimeZoneName('short', date, { timeZone })
78+
assert.equal(result, 'CEST')
79+
})
80+
81+
afterEach(() => {
82+
setDefaultOptions({ locale: undefined })
83+
})
84+
})
6985
})

src/formatInTimeZone/test.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from 'power-assert'
2-
import { enGB } from 'date-fns/locale/en-GB'
32
import { formatInTimeZone } from './index.js'
3+
import { setDefaultOptions } from 'date-fns'
4+
import { vi, enGB, de } from 'date-fns/locale'
45

56
describe('formatInTimeZone', function () {
67
it('treat date only string in the timezone specified in the options', function () {
@@ -115,4 +116,58 @@ describe('formatInTimeZone', function () {
115116
RangeError
116117
)
117118
})
119+
120+
describe('setDefaultOptions for locale', function () {
121+
var date = '1986-04-04T10:32:55.123Z'
122+
var timeZone = 'Europe/Paris'
123+
var format = "PPPP 'UTC'xxx"
124+
var tests = [
125+
{
126+
expected: 'Freitag, 4. April 1986 UTC+02:00',
127+
locale: de,
128+
},
129+
{
130+
expected: 'Thứ Sáu, ngày 4 tháng 04 năm 1986 UTC+02:00',
131+
locale: vi,
132+
},
133+
]
134+
135+
tests.forEach(function (test) {
136+
it(`locale: ${test.locale.code}`, function () {
137+
setDefaultOptions({ locale: test.locale })
138+
assert.equal(formatInTimeZone(date, timeZone, format), test.expected)
139+
})
140+
})
141+
142+
afterEach(() => {
143+
setDefaultOptions({ locale: undefined })
144+
})
145+
})
146+
147+
describe('setDefaultOptions for locale using tzIntlTimeZoneName', function () {
148+
var date = '1986-04-04T10:32:55.123Z'
149+
var timeZone = 'Europe/Paris'
150+
var format = 'dd.MM.yyyy HH:mm zzzz'
151+
var tests = [
152+
{
153+
expected: '04.04.1986 12:32 Mitteleuropäische Sommerzeit',
154+
locale: de,
155+
},
156+
{
157+
expected: '04.04.1986 12:32 Giờ mùa hè Trung Âu',
158+
locale: vi,
159+
},
160+
]
161+
162+
tests.forEach(function (test) {
163+
it(`locale: ${test.locale.code}`, function () {
164+
setDefaultOptions({ locale: test.locale })
165+
assert.equal(formatInTimeZone(date, timeZone, format), test.expected)
166+
})
167+
})
168+
169+
afterEach(() => {
170+
setDefaultOptions({ locale: undefined })
171+
})
172+
})
118173
})

0 commit comments

Comments
 (0)