From a4bcc1d920d8799a17294df67f81f5895469192f Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Wed, 5 Jun 2024 21:39:00 +0300 Subject: [PATCH] feat: add custom api model --- src/shared/structs/DateOnly.ts | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/shared/structs/DateOnly.ts diff --git a/src/shared/structs/DateOnly.ts b/src/shared/structs/DateOnly.ts new file mode 100644 index 0000000..690a4b3 --- /dev/null +++ b/src/shared/structs/DateOnly.ts @@ -0,0 +1,45 @@ +export class DateOnly { + private ticks: number; + + /* + constructor(year: number, month: number, day: number) { + this.ticks = new Date(year, month - 1, day).getTime(); + } +*/ + + constructor(year: number, month: number, day: number); + constructor(date: Date); + constructor(date: string); + constructor(yearOrDate: number | Date | string, month?: number, day?: number) { + if (yearOrDate instanceof Date) { + this.ticks = yearOrDate.getTime(); + } else if (typeof yearOrDate === 'number' && month !== undefined && day !== undefined) { + this.ticks = new Date(yearOrDate, month - 1, day).getTime(); + } else if (typeof yearOrDate === 'string') { + const [year, month, day] = yearOrDate.split('-').map(Number); + this.ticks = new Date(year, month - 1, day).getTime(); + } else { + throw new Error('Invalid constructor arguments'); + } + } + + get year(): number { + return new Date(this.ticks).getFullYear(); + } + + get month(): number { + return new Date(this.ticks).getMonth() + 1; + } + + get day(): number { + return new Date(this.ticks).getDate(); + } + + get date(): Date { + return new Date(this.ticks); + } + + toString(): string { + return `${this.year}-${String(this.month).padStart(2, '0')}-${String(this.day).padStart(2, '0')}`; + } +}