From f60c192264d710f08f592d6af0dd531516d73d61 Mon Sep 17 00:00:00 2001 From: Ferdinand Prantl Date: Wed, 26 Sep 2018 09:25:07 +0200 Subject: [PATCH] fix: Clone return time zone list to prevent the caller from modifying the cached value Use getTime instead of valueOf for clarity. --- perf/getZonedTime.perf.js | 2 +- src/convert/convert.js | 6 +++--- src/lookup/lookup.js | 8 ++++---- test/convertDateToTime.test.js | 2 +- test/getZonedTime.test.js | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/perf/getZonedTime.perf.js b/perf/getZonedTime.perf.js index 264198f..fc62aca 100644 --- a/perf/getZonedTime.perf.js +++ b/perf/getZonedTime.perf.js @@ -1,7 +1,7 @@ const createSuite = require('./createSuite') const { findTimeZone, getZonedTime } = require('../dist') -const unixTime = new Date(2018, 9, 1, 18, 1, 36, 386).valueOf() +const unixTime = new Date(2018, 9, 1, 18, 1, 36, 386).getTime() const berlin = findTimeZone('Europe/Berlin') function getZonedTimeUsingDate () { diff --git a/src/convert/convert.js b/src/convert/convert.js index 17fbbb7..bacba69 100644 --- a/src/convert/convert.js +++ b/src/convert/convert.js @@ -21,14 +21,14 @@ function attachEpoch (time, unixTime) { } function getUTCOffset (date, timeZone) { - const unixTime = typeof date === 'number' ? date : date.valueOf() + const unixTime = typeof date === 'number' ? date : date.getTime() const { abbreviation, offset } = getTransition(unixTime, timeZone) return { abbreviation, offset } } function getZonedTime (date, timeZone) { const gotUnixTime = typeof date === 'number' - const unixTime = gotUnixTime ? date : date.valueOf() + const unixTime = gotUnixTime ? date : date.getTime() const { abbreviation, offset } = getTransition(unixTime, timeZone) if (gotUnixTime || offset) { date = new Date(unixTime - offset * 60000) @@ -99,7 +99,7 @@ function convertDateToTime (date) { : '???', offset: date.getTimezoneOffset() } - attachEpoch(time, date.valueOf()) + attachEpoch(time, date.getTime()) return time } diff --git a/src/lookup/lookup.js b/src/lookup/lookup.js index d31925e..8bca5bc 100644 --- a/src/lookup/lookup.js +++ b/src/lookup/lookup.js @@ -5,14 +5,14 @@ let names let links let instances -function populateTimeZones (data) { +function populateTimeZones ({ zones: zoneData, links: linkData }) { zones = {} - names = data.zones.map(packed => { + names = zoneData.map(packed => { const name = packed.substr(0, packed.indexOf('|')) zones[name] = packed return name }) - links = data.links.reduce((result, packed) => { + links = linkData.reduce((result, packed) => { const [ name, alias ] = packed.split('|') result[alias] = name return result @@ -21,7 +21,7 @@ function populateTimeZones (data) { } function listTimeZones () { - return names + return names.slice() } function findTimeZone (alias) { diff --git a/test/convertDateToTime.test.js b/test/convertDateToTime.test.js index 5a438a0..736b70f 100644 --- a/test/convertDateToTime.test.js +++ b/test/convertDateToTime.test.js @@ -19,7 +19,7 @@ it('converts a date to time', () => { expect(minutes).toEqual(30) expect(seconds).toEqual(15) expect(milliseconds).toEqual(234) - expect(epoch).toEqual(date.valueOf()) + expect(epoch).toEqual(date.getTime()) expect(typeof zone === 'object').toBeTruthy() const { abbreviation, offset } = zone expect(/\w+/.test(abbreviation)).toBeTruthy() diff --git a/test/getZonedTime.test.js b/test/getZonedTime.test.js index 666432a..e8954c1 100644 --- a/test/getZonedTime.test.js +++ b/test/getZonedTime.test.js @@ -33,7 +33,7 @@ it('converts the UNIX time to the correct time object', () => { it('recognizes daylight-saving time', () => { const unixTime = Date.UTC(2018, 6, 2, 9, 30, 15, 234) - const berlinTime = getZonedTime(unixTime.valueOf(), berlin) + const berlinTime = getZonedTime(unixTime, berlin) expect(typeof berlinTime === 'object').toBeTruthy() const { year, month, day, dayOfWeek, hours, minutes, seconds, milliseconds, zone, epoch } = berlinTime expect(year).toEqual(2018)