Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 51f20f5

Browse files
authored
Merge pull request #15 from icambron/formatToParts-test
add formatToParts
2 parents 99a6c52 + 1f98a42 commit 51f20f5

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

src/code/polyfill.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,71 @@ export default function polyfill(globalSpace) {
210210
}
211211
});
212212

213+
const _formatToParts = _get(
214+
DateTimeFormatPolyfill.prototype.__proto__ ||
215+
Object.getPrototypeOf(DateTimeFormatPolyfill.prototype),
216+
'formatToParts',
217+
this
218+
);
219+
220+
if (_formatToParts) {
221+
Object.defineProperty(DateTimeFormatPolyfill.prototype, 'formatToParts', {
222+
configurable: true,
223+
value: function(date) {
224+
225+
const _resolvedOptions = _get(
226+
DateTimeFormatPolyfill.prototype.__proto__ ||
227+
Object.getPrototypeOf(DateTimeFormatPolyfill.prototype),
228+
'resolvedOptions',
229+
this
230+
);
231+
232+
if (!this._dateTimeFormatPolyfill) {
233+
return _formatToParts.call(this, date);
234+
}
235+
236+
if (date === null || date === undefined) {
237+
date = new Date();
238+
}
239+
240+
if (!(date instanceof Date)) {
241+
date = new Date(date);
242+
}
243+
244+
const polyfill = this._dateTimeFormatPolyfill;
245+
const timeZoneOffsetInfo = getTimeZoneOffsetInfo(polyfill.timeZoneData, date);
246+
const timeZoneOffset = timeZoneOffsetInfo.offset * 60000;
247+
const shiftedDate = new Date(date.getTime() + timeZoneOffset); // We need to format time by offseting it
248+
const shiftedParts = _formatToParts.call(this, shiftedDate);
249+
const resolvedLocale = _resolvedOptions.call(this).locale;
250+
const doNeedToReplaceTimeZoneName = (polyfill.optionTimeZoneName !== undefined);
251+
252+
if (doNeedToReplaceTimeZoneName) {
253+
const isShort = (polyfill.optionTimeZoneName === 'short');
254+
const timeZoneName = getZoneNameForLocale({
255+
locale: resolvedLocale,
256+
ianaTimeZone: polyfill.optionTimeZone,
257+
isdst: timeZoneOffsetInfo.isdst,
258+
offset: timeZoneOffsetInfo.offset,
259+
timeStamp: date.getTime(),
260+
isShort: isShort
261+
});
262+
263+
const index = shiftedParts.map(i => i.type).indexOf('timeZoneName');
264+
265+
if (index >= 0) {
266+
shiftedParts[index] = {
267+
type: 'timeZoneName',
268+
value: timeZoneName
269+
};
270+
}
271+
}
272+
273+
return shiftedParts;
274+
}
275+
});
276+
}
277+
213278
Object.defineProperty(DateTimeFormatPolyfill.prototype, 'resolvedOptions', {
214279
writable: true,
215280
configurable: true,

test/test-complete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('Polyfill with complete package', () => {
4646
});
4747
});
4848

49-
describe.skip('.formatToParts(date)', () => {
49+
describe('.formatToParts(date)', () => {
5050
it('polyfilled DateTimeFormat should implement iff native DateTimeFormat implemented it', () => {
5151
const nativeDateTimeFormat = new Intl.DateTimeFormat('en', {
5252
timeZone: 'UTC'

test/test-saucelabs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('SauceLabs# ', () => {
5252
});
5353
});
5454

55-
describe.skip('.formatToParts(date)', () => {
55+
describe('.formatToParts(date)', () => {
5656
it('polyfilled DateTimeFormat should implement iff native DateTimeFormat implemented it', () => {
5757
const nativeDateTimeFormat = new Intl.DateTimeFormat('en', {timeZone: 'UTC'}); // UTC is always implemented
5858
const polyfilledDateTimeFormat = new Intl.DateTimeFormat('en', {timeZone: 'Moon/Nearside'});// Moon/Nearside can never be implemented.
@@ -283,4 +283,4 @@ describe('SauceLabs# ', () => {
283283
});
284284
});
285285
});
286-
});
286+
});

0 commit comments

Comments
 (0)