Skip to content

Commit

Permalink
feat: LocalDate.plusDays, weeks, months, years
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed May 10, 2024
1 parent 8eb126c commit 7edda2f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/datetime/localDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ test('add basic', () => {
expect(ld.plus(-1, 'month').toString()).toBe('2021-12-01')

const d = localDate('2022-05-31')
expect(d.plus(1, 'month').toISODate()).toBe('2022-06-30')
expect(d.plus(-1, 'month').toISODate()).toBe('2022-04-30')
expect(d.plusMonths(1).toISODate()).toBe('2022-06-30')
expect(d.minusMonths(1).toISODate()).toBe('2022-04-30')
})

test('add', () => {
Expand Down
25 changes: 25 additions & 0 deletions src/datetime/localDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,31 @@ export class LocalDate {
return days * sign || 0
}

plusDays(num: number): LocalDate {
return this.plus(num, 'day')
}
plusWeeks(num: number): LocalDate {
return this.plus(num, 'week')
}
plusMonths(num: number): LocalDate {
return this.plus(num, 'month')
}
plusYears(num: number): LocalDate {
return this.plus(num, 'year')
}
minusDays(num: number): LocalDate {
return this.plus(-num, 'day')
}
minusWeeks(num: number): LocalDate {
return this.plus(-num, 'week')
}
minusMonths(num: number): LocalDate {
return this.plus(-num, 'month')
}
minusYears(num: number): LocalDate {
return this.plus(-num, 'year')
}

plus(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
let { $day, $month, $year } = this

Expand Down
10 changes: 5 additions & 5 deletions src/datetime/localTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,10 @@ test('add edge', () => {
// 2020-02-29 - 2020 year == 0000-02-29 00:00:00
// expect(localTime('2020-02-29').add(-2020, 'year').toPretty()).toBe(dayjs('2020-02-29').add(-2020, 'year').toPretty())

expect(localTime('2022-05-31').plus(21, 'month').toPretty()).toBe('2024-02-29 00:00:00')
expect(localTime('2022-05-31').plusMonths(21).toPretty()).toBe('2024-02-29 00:00:00')

expect(localTime('2022-05-31').plus(1, 'month').toPretty()).toBe('2022-06-30 00:00:00')
expect(localTime('2022-05-31').plus(-1, 'month').toPretty()).toBe('2022-04-30 00:00:00')
expect(localTime('2022-05-31').plusMonths(1).toPretty()).toBe('2022-06-30 00:00:00')
expect(localTime('2022-05-31').minusMonths(1).toPretty()).toBe('2022-04-30 00:00:00')

expect(localTime('2020-02-29').plus(1, 'month').toPretty()).toBe('2020-03-29 00:00:00')
expect(localTime('2020-03-29').plus(-1, 'month').toPretty()).toBe('2020-02-29 00:00:00')
Expand All @@ -368,8 +368,8 @@ test('add edge', () => {
expect(localTime('2020-01-31').plus(1, 'month').toPretty()).toBe('2020-02-29 00:00:00')
expect(localTime('2020-01-31').plus(2, 'month').toPretty()).toBe('2020-03-31 00:00:00')
expect(localTime('2020-01-31').plus(3, 'month').toPretty()).toBe('2020-04-30 00:00:00')
expect(localTime('2020-02-29').plus(1, 'year').toPretty()).toBe('2021-02-28 00:00:00')
expect(localTime('2021-02-28').plus(-1, 'year').toPretty()).toBe('2020-02-28 00:00:00')
expect(localTime('2020-02-29').plusYears(1).toPretty()).toBe('2021-02-28 00:00:00')
expect(localTime('2021-02-28').minusYears(1).toPretty()).toBe('2020-02-28 00:00:00')
})

test('diff edge', () => {
Expand Down
43 changes: 43 additions & 0 deletions src/datetime/localTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,49 @@ export class LocalTime {
return mutate ? this : new LocalTime(d)
}

plusSeconds(num: number): LocalTime {
return this.plus(num, 'second')
}
plusMinutes(num: number): LocalTime {
return this.plus(num, 'minute')
}
plusHours(num: number): LocalTime {
return this.plus(num, 'hour')
}
plusDays(num: number): LocalTime {
return this.plus(num, 'day')
}
plusWeeks(num: number): LocalTime {
return this.plus(num, 'week')
}
plusMonths(num: number): LocalTime {
return this.plus(num, 'month')
}
plusYears(num: number): LocalTime {
return this.plus(num, 'year')
}
minusSeconds(num: number): LocalTime {
return this.plus(-num, 'second')
}
minusMinutes(num: number): LocalTime {
return this.plus(-num, 'minute')
}
minusHours(num: number): LocalTime {
return this.plus(-num, 'hour')
}
minusDays(num: number): LocalTime {
return this.plus(-num, 'day')
}
minusWeeks(num: number): LocalTime {
return this.plus(-num, 'week')
}
minusMonths(num: number): LocalTime {
return this.plus(-num, 'month')
}
minusYears(num: number): LocalTime {
return this.plus(-num, 'year')
}

plus(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
if (unit === 'week') {
num *= 7
Expand Down

0 comments on commit 7edda2f

Please sign in to comment.