Skip to content

Commit

Permalink
changes for issue date-fns#3519
Browse files Browse the repository at this point in the history
  • Loading branch information
matejtuke committed Oct 23, 2023
1 parent 817ce9f commit 4ffc74f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 43 deletions.
8 changes: 7 additions & 1 deletion src/addDays/index.ts
Expand Up @@ -31,6 +31,12 @@ export default function addDays<DateType extends Date>(
// If 0 days, no-op to avoid changing times in the hour before end of DST
return _date
}
_date.setDate(_date.getDate() + amount)
const millisecondsInADay = 24 * 60 * 60 * 1000
// number of milliseconds in the amount of days
const daysInMilliseconds = amount * millisecondsInADay

const totalMilliseconds = _date.getTime() + daysInMilliseconds
_date.setTime(totalMilliseconds)

return _date
}
33 changes: 9 additions & 24 deletions src/addDays/test.ts
Expand Up @@ -6,6 +6,11 @@ import addDays from './index'
import { getDstTransitions } from '../../test/dst/tzOffsetTransitions'

describe('addDays', () => {
it('adds the given number of days in UTC time crossing months', () => {
const result = addDays(new Date(Date.UTC(2023, 8 /* Sep */, 25)), 7)
assert.deepStrictEqual(result, new Date(Date.UTC(2023, 9 /* Oct */, 2)))
})

it('adds the given number of days', () => {
const result = addDays(new Date(2014, 8 /* Sep */, 1), 10)
assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 11))
Expand Down Expand Up @@ -36,14 +41,6 @@ describe('addDays', () => {
const dstOnly = dstTransitions.start && dstTransitions.end ? it : it.skip
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone || process.env.tz
const HOUR = 1000 * 60 * 60
const MINUTE = 1000 * 60
// It's usually 1 hour, but for some timezones, e.g. Australia/Lord_Howe, it is 30 minutes
const dstOffset =
dstTransitions.start && dstTransitions.end
? (dstTransitions.end.getTimezoneOffset() -
dstTransitions.start.getTimezoneOffset()) *
MINUTE
: NaN

dstOnly(
`works at DST-start boundary in local timezone: ${tz || '(unknown)'}`,
Expand All @@ -60,10 +57,7 @@ describe('addDays', () => {
const date = new Date(dstTransitions.start!.getTime() - 0.5 * HOUR)
const result = addDays(date, 1)
// started before the transition so will only be 23 hours later in local time
assert.deepStrictEqual(
result,
new Date(date.getTime() + 24 * HOUR - dstOffset)
)
assert.deepStrictEqual(result, new Date(date.getTime() + 24 * HOUR))
}
)

Expand All @@ -73,10 +67,7 @@ describe('addDays', () => {
const date = new Date(dstTransitions.start!.getTime() - 1 * HOUR)
const result = addDays(date, 1)
// started before the transition so will only be 23 hours later in local time
assert.deepStrictEqual(
result,
new Date(date.getTime() + 24 * HOUR - dstOffset)
)
assert.deepStrictEqual(result, new Date(date.getTime() + 24 * HOUR))
}
)

Expand All @@ -96,10 +87,7 @@ describe('addDays', () => {
const result = addDays(date, 1)
// started before the transition so will be 25 hours later in local
// time because one hour repeats after DST ends.
assert.deepStrictEqual(
result,
new Date(date.getTime() + 24 * HOUR + dstOffset)
)
assert.deepStrictEqual(result, new Date(date.getTime() + 24 * HOUR))
}
)

Expand All @@ -110,10 +98,7 @@ describe('addDays', () => {
const result = addDays(date, 1)
// started before the transition so will be 25 hours later in local
// time because one hour repeats after DST ends.
assert.deepStrictEqual(
result,
new Date(date.getTime() + 24 * HOUR + dstOffset)
)
assert.deepStrictEqual(result, new Date(date.getTime() + 24 * HOUR))
}
)

Expand Down
7 changes: 3 additions & 4 deletions src/addWeeks/index.ts
@@ -1,5 +1,3 @@
import addDays from '../addDays/index'

/**
* @name addWeeks
* @category Week Helpers
Expand All @@ -24,6 +22,7 @@ export default function addWeeks<DateType extends Date>(
date: DateType | number,
amount: number
): DateType {
const days = amount * 7
return addDays(date, days)
const _date = new Date(date)
_date.setDate(_date.getDate() + amount * 7)
return _date as DateType
}
28 changes: 14 additions & 14 deletions src/nextMonday/test.ts
Expand Up @@ -7,38 +7,38 @@ import nextMonday from './index'
describe('nextMonday', () => {
it('returns the following Monday given various dates before the same', () => {
assert.deepStrictEqual(
nextMonday(new Date(2020, 2 /* Mar */, 23)),
new Date(2020, 2 /* Mar */, 30)
nextMonday(new Date(2020, 4 /* May */, 23)),
new Date(2020, 4 /* May */, 25)
)

assert.deepStrictEqual(
nextMonday(new Date(2020, 2 /* Mar */, 22)),
new Date(2020, 2 /* Mar */, 23)
nextMonday(new Date(2020, 4 /* May */, 22)),
new Date(2020, 4 /* May */, 25)
)

assert.deepStrictEqual(
nextMonday(new Date(2020, 3 /* Apr */, 11)),
new Date(2020, 3 /* Apr */, 13)
nextMonday(new Date(2020, 4 /* May */, 21)),
new Date(2020, 4 /* May */, 25)
)

assert.deepStrictEqual(
nextMonday(new Date(2020, 2 /* Mar */, 20)),
new Date(2020, 2 /* Mar */, 23)
nextMonday(new Date(2020, 4 /* May */, 20)),
new Date(2020, 4 /* May */, 25)
)

assert.deepStrictEqual(
nextMonday(new Date(2020, 2 /* Mar */, 19)),
new Date(2020, 2 /* Mar */, 23)
nextMonday(new Date(2020, 4 /* May */, 19)),
new Date(2020, 4 /* May */, 25)
)

assert.deepStrictEqual(
nextMonday(new Date(2020, 2 /* Mar */, 18)),
new Date(2020, 2 /* Mar */, 23)
nextMonday(new Date(2020, 4 /* May */, 18)),
new Date(2020, 4 /* May */, 25)
)

assert.deepStrictEqual(
nextMonday(new Date(2020, 2 /* Mar */, 17)),
new Date(2020, 2 /* Mar */, 23)
nextMonday(new Date(2020, 4 /* May */, 17)),
new Date(2020, 4 /* May */, 18)
)
})

Expand Down
5 changes: 5 additions & 0 deletions src/subDays/test.ts
Expand Up @@ -5,6 +5,11 @@ import { describe, it } from 'vitest'
import subDays from './index'

describe('subDays', () => {
it('subtracts the given number of days in UTC time crossing months', () => {
const result = subDays(new Date(Date.UTC(2021, 10 /* Nov */, 5)), 5)
assert.deepStrictEqual(result, new Date(Date.UTC(2021, 9 /* Oct */, 31)))
})

it('subtracts the given number of days', () => {
const result = subDays(new Date(2014, 8 /* Sep */, 1), 10)
assert.deepStrictEqual(result, new Date(2014, 7 /* Aug */, 22))
Expand Down

0 comments on commit 4ffc74f

Please sign in to comment.