Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix daily frequency compensating for DST #96

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { readdir, stat, unlink, symlink, lstat, readlink } = require('fs/promises')
const { dirname, join } = require('path')
const { format } = require('date-fns')
const { format, addDays, addHours } = require('date-fns')

function parseSize (size) {
let multiplier = 1024 ** 2
Expand Down Expand Up @@ -53,11 +53,11 @@ function validateLimitOptions (limit) {
}

function getNextDay (start) {
return new Date(start + 24 * 60 * 60 * 1000).setHours(0, 0, 0, 0)
return addDays(new Date(start), 1).setHours(0, 0, 0, 0)
}

function getNextHour (start) {
return new Date(start + 60 * 60 * 1000).setMinutes(0, 0, 0)
return addHours(new Date(start), 1).setMinutes(0, 0, 0)
}

function getNextCustom (frequency) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"devDependencies": {
"husky": "^9.0.11",
"mockdate": "^3.0.5",
"pino": "^9.2.0",
"snazzy": "^9.0.0",
"standard": "^17.0.0-2",
Expand Down
61 changes: 61 additions & 0 deletions test/lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { writeFile, rm, stat, readlink, symlink } = require('fs/promises')
const { join } = require('path')
const { test } = require('tap')
const { format } = require('date-fns')
const MockDate = require('mockdate')

const {
buildFileName,
Expand Down Expand Up @@ -69,6 +70,66 @@ test('getNext()', async ({ same }) => {
same(getNext(custom), Date.now() + custom, 'supports custom frequency and does not return start')
})

test('getNext() on dates transitioning from DST to Standard Time', async ({ same }) => {
// on these days the time rolls back 1 hour so there "are" 25 hours in the day
// genNext() should account for variable number of hours in the day

// test two different timezones
const data = [
{
tz: 'Europe/Berlin',
mockDate: '27 Oct 2024 00:00:00 GMT+0100'
},
{
tz: 'America/New_York',
mockDate: '03 Nov 2024 00:00:00 GMT-0500'
}
]

for (const d of data) {
MockDate.set(d.mockDate)
process.env.TZ = d.tz
const today = new Date()

same(getNext('daily'), startOfDay(addDays(today, 1)).getTime(), 'supports daily frequency')
same(getNext('hourly'), startOfHour(addHours(today, 1)).getTime(), 'supports hourly frequency')
const custom = 3000
same(getNext(custom), Date.now() + custom, 'supports custom frequency and does not return start')
MockDate.reset()
process.env.TZ = undefined
}
})

test('getNext() on dates transitioning from Standard Time to DST', async ({ same }) => {
// on these days the time rolls forward 1 hour so there "are" 23 hours in the day
// genNext() should account for variable number of hours in the day

// test two different timezones
const data = [
{
tz: 'Europe/Berlin',
mockDate: '31 March 2024 01:00:00 GMT+0100'
},
{
tz: 'America/New_York',
mockDate: '10 Nov 2024 01:00:00 GMT-0500'
}
]

for (const d of data) {
MockDate.set(d.mockDate)
process.env.TZ = d.tz
const today = new Date()

same(getNext('daily'), startOfDay(addDays(today, 1)).getTime(), 'supports daily frequency')
same(getNext('hourly'), startOfHour(addHours(today, 1)).getTime(), 'supports hourly frequency')
const custom = 3000
same(getNext(custom), Date.now() + custom, 'supports custom frequency and does not return start')
MockDate.reset()
process.env.TZ = undefined
}
})

test('getFileName()', async ({ equal, throws }) => {
const strFunc = () => 'my-func'
throws(getFileName, 'throws on empty input')
Expand Down