Skip to content

Commit

Permalink
feat: localDate would Math.floor fractional input on .plus
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Aug 21, 2024
1 parent dd6a6e8 commit c1c132e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/datetime/localDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,17 @@ test('todayString tz', () => {
console.log(localDate.todayString())
console.log(new Date().toString())
})

test('fractional unit input', () => {
const ld = localDate('1984-06-21')
expect(ld.plus(0.4, 'day').toISODate()).toBe('1984-06-21')
expect(ld.plus(0.5, 'day').toISODate()).toBe('1984-06-21')
expect(ld.plus(0.9, 'day').toISODate()).toBe('1984-06-21')
expect(ld.plus(1.1, 'day').toISODate()).toBe('1984-06-22')
expect(ld.minus(0.4, 'day').toISODate()).toBe('1984-06-20')
expect(ld.minus(0.5, 'day').toISODate()).toBe('1984-06-20')
expect(ld.minus(0.9, 'day').toISODate()).toBe('1984-06-20')
expect(ld.minus(1.1, 'day').toISODate()).toBe('1984-06-19')

expect(localDate('1984-06-21.5').toISODate()).toBe('1984-06-21')
})
1 change: 1 addition & 0 deletions src/datetime/localDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export class LocalDate {
}

plus(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
num = Math.floor(num) // if a fractional number like 0.5 is passed - it will be floored, as LocalDate only deals with "whole days" as minimal unit
let { day, month, year } = this

if (unit === 'week') {
Expand Down
30 changes: 30 additions & 0 deletions src/datetime/localTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,33 @@ test('parsing date string in negative timezone bug', () => {
expect(lt.toISODate()).toBe(s)
expect(lt.toPretty()).toBe('2023-03-03 00:00:00')
})

test('fractional unit input', () => {
const lt = localTime('1984-06-21')
expect(lt.toString()).toBe('1984-06-21T00:00:00')

// Comparing only IsoDate part (no time component)
expect(lt.plus(0.4, 'day').toISODate()).toBe('1984-06-21')
expect(lt.plus(0.5, 'day').toISODate()).toBe('1984-06-21')
expect(lt.plus(0.9, 'day').toISODate()).toBe('1984-06-21')
expect(lt.plus(1.1, 'day').toISODate()).toBe('1984-06-22')
expect(lt.minus(0.4, 'day').toISODate()).toBe('1984-06-20')
expect(lt.minus(0.5, 'day').toISODate()).toBe('1984-06-20')
expect(lt.minus(0.9, 'day').toISODate()).toBe('1984-06-20')
expect(lt.minus(1.1, 'day').toISODate()).toBe('1984-06-19')

// Comparing full date+time component.
// LocalTime does NOT support fractional input, so it should be coerced (Math.floored) to Integer
expect(lt.plus(0.4, 'day').toString()).toBe('1984-06-21T00:00:00')
expect(lt.plus(0.5, 'day').toString()).toBe('1984-06-21T00:00:00')
expect(lt.plus(0.9, 'day').toString()).toBe('1984-06-21T00:00:00')
expect(lt.plus(1.1, 'day').toString()).toBe('1984-06-22T00:00:00')
expect(lt.minus(0.4, 'day').toString()).toBe('1984-06-20T00:00:00')
expect(lt.minus(0.5, 'day').toString()).toBe('1984-06-20T00:00:00')
expect(lt.minus(0.9, 'day').toString()).toBe('1984-06-20T00:00:00')
expect(lt.minus(1.1, 'day').toString()).toBe('1984-06-19T00:00:00')

expect(() => localTime('1984-06-21.5')).toThrowErrorMatchingInlineSnapshot(
`"Cannot parse "1984-06-21.5" into LocalTime"`,
)
})

0 comments on commit c1c132e

Please sign in to comment.