From 41571ac7a73c8dc6dda854e116a602c28c539b0b Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Tue, 28 Apr 2026 17:11:34 +0000 Subject: [PATCH] [Sync Iteration] typescript/clock/1 --- solutions/typescript/clock/1/clock.ts | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 solutions/typescript/clock/1/clock.ts diff --git a/solutions/typescript/clock/1/clock.ts b/solutions/typescript/clock/1/clock.ts new file mode 100644 index 0000000..203c1e7 --- /dev/null +++ b/solutions/typescript/clock/1/clock.ts @@ -0,0 +1,32 @@ +export class Clock { + private _hour: number; + private _minute: number; + + constructor(hour: number, minute: number = 0) { + const total = this.normalize(hour * 60 + minute); + this._hour = Math.floor(total / 60); + this._minute = total % 60; + } + + private normalize(minutes: number): number { + return ((minutes % 1440) + 1440) % 1440; + } + + public toString(): string { + return `${this._hour.toString().padStart(2, "0")}:${this._minute + .toString() + .padStart(2, "0")}`; + } + + public plus(minutes: number): Clock { + return new Clock(this._hour, this._minute + minutes); + } + + public minus(minutes: number): Clock { + return new Clock(this._hour, this._minute - minutes); + } + + public equals(other: Clock): boolean { + return this.toString() === other.toString(); + } +}