From 7edda2fecd7ebb812d03bf466c090f96034b8f19 Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Sat, 11 May 2024 01:17:33 +0200 Subject: [PATCH] feat: LocalDate.plusDays, weeks, months, years --- src/datetime/localDate.test.ts | 4 ++-- src/datetime/localDate.ts | 25 ++++++++++++++++++++ src/datetime/localTime.test.ts | 10 ++++---- src/datetime/localTime.ts | 43 ++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/datetime/localDate.test.ts b/src/datetime/localDate.test.ts index a21a494e..6ad973e1 100644 --- a/src/datetime/localDate.test.ts +++ b/src/datetime/localDate.test.ts @@ -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', () => { diff --git a/src/datetime/localDate.ts b/src/datetime/localDate.ts index 40e375f1..ef7a08ae 100644 --- a/src/datetime/localDate.ts +++ b/src/datetime/localDate.ts @@ -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 diff --git a/src/datetime/localTime.test.ts b/src/datetime/localTime.test.ts index c031a2b7..56892d97 100644 --- a/src/datetime/localTime.test.ts +++ b/src/datetime/localTime.test.ts @@ -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') @@ -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', () => { diff --git a/src/datetime/localTime.ts b/src/datetime/localTime.ts index e60055a2..f35a35fa 100644 --- a/src/datetime/localTime.ts +++ b/src/datetime/localTime.ts @@ -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