Skip to content

Commit

Permalink
fix: Clone return time zone list to prevent the caller from modifying…
Browse files Browse the repository at this point in the history
… the cached value

Use getTime instead of valueOf for clarity.
  • Loading branch information
prantlf committed Sep 26, 2018
1 parent 63719e2 commit f60c192
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion perf/getZonedTime.perf.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
6 changes: 3 additions & 3 deletions src/convert/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -99,7 +99,7 @@ function convertDateToTime (date) {
: '???',
offset: date.getTimezoneOffset()
}
attachEpoch(time, date.valueOf())
attachEpoch(time, date.getTime())
return time
}

Expand Down
8 changes: 4 additions & 4 deletions src/lookup/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +21,7 @@ function populateTimeZones (data) {
}

function listTimeZones () {
return names
return names.slice()
}

function findTimeZone (alias) {
Expand Down
2 changes: 1 addition & 1 deletion test/convertDateToTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/getZonedTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f60c192

Please sign in to comment.