diff --git a/index.ts b/index.ts index 2e016df..d0e0f72 100644 --- a/index.ts +++ b/index.ts @@ -44,18 +44,57 @@ export class UTCDate extends Date { } } + getTimezoneOffset() { + return 0; + } + toString(): string { + const date = this.toDateString(); + const time = this.toTimeString(); + return `${date} ${time}`; + } + + toDateString(): string { const weekday = weekdayFormat.format(this); const date = dateFormat.format(this); const year = this.getFullYear(); - const time = this.toTimeString(); - return `${weekday} ${date} ${year} ${time}`; + return `${weekday} ${date} ${year}`; } toTimeString(): string { const time = timeFormat.format(this); return `${time} GMT+0000 (Coordinated Universal Time)`; } + + toLocaleString( + locales?: string | string[], + options?: Intl.DateTimeFormatOptions + ): string { + return Date.prototype.toLocaleString.call(this, locales, { + timeZone: "UTC", + ...options, + }); + } + + toLocaleDateString( + locales?: string | string[], + options?: Intl.DateTimeFormatOptions + ): string { + return Date.prototype.toLocaleDateString.call(this, locales, { + timeZone: "UTC", + ...options, + }); + } + + toLocaleTimeString( + locales?: string | string[], + options?: Intl.DateTimeFormatOptions + ): string { + return Date.prototype.toLocaleTimeString.call(this, locales, { + timeZone: "UTC", + ...options, + }); + } } var getMethods: Array = [ diff --git a/test.ts b/test.ts index 33a1b96..23d344a 100644 --- a/test.ts +++ b/test.ts @@ -62,6 +62,12 @@ describe("UTCDate", () => { it.todo("returns UTC seconds"); }); + describe("getTimezoneOffset", () => { + it("returns 0", () => { + expect(new UTCDate(1999, 11, 31, 23).getTimezoneOffset()).toBe(0); + }); + }); + describe("setDate", () => { it("sets UTC date", () => { const date = new UTCDate(1987, 1, 11, 23); @@ -116,6 +122,24 @@ describe("UTCDate", () => { "Wed Feb 11 1987 12:13:14 GMT+0000 (Coordinated Universal Time)" ); }); + + it("works with Symbol.toPrimitive", () => { + expect( + new UTCDate(1987, 1, 11, 12, 13, 14, 15)[Symbol.toPrimitive]("string") + ).toBe("Wed Feb 11 1987 12:13:14 GMT+0000 (Coordinated Universal Time)"); + + expect( + new UTCDate(1987, 1, 11, 12, 13, 14, 15)[Symbol.toPrimitive]("default") + ).toBe("Wed Feb 11 1987 12:13:14 GMT+0000 (Coordinated Universal Time)"); + }); + }); + + describe("toDateString", () => { + it("returns string representing the given date in UTC timezone", () => { + expect(new UTCDate(1987, 1, 11, 12, 13, 14, 15).toDateString()).toBe( + "Wed Feb 11 1987" + ); + }); }); describe("toTimeString", () => { @@ -125,4 +149,76 @@ describe("UTCDate", () => { ); }); }); + + describe("toLocaleString", () => { + it("returns localized date time string", () => { + expect( + new UTCDate(1987, 1, 11, 12, 13, 14, 15).toLocaleString("en-GB") + ).toBe("11/02/1987, 12:13:14"); + }); + + it("allows to pass options", () => { + expect( + new UTCDate(1987, 1, 11, 12, 13, 14, 15).toLocaleString("en-GB", { + month: "long", + }) + ).toBe("February"); + }); + + it("allows to override the timezone", () => { + expect( + new UTCDate(1987, 1, 11, 12, 13, 14, 15).toLocaleString("en-GB", { + timeZone: "Asia/Kolkata", + }) + ).toBe("11/02/1987, 17:43:14"); + }); + }); + + describe("toLocaleDateString", () => { + it("returns localized date string", () => { + expect( + new UTCDate(1999, 11, 31, 23, 59, 59).toLocaleDateString("en-GB") + ).toBe("31/12/1999"); + }); + + it("allows to pass options", () => { + expect( + new UTCDate(1999, 11, 31, 23, 59, 59).toLocaleString("en-GB", { + month: "long", + }) + ).toBe("December"); + }); + + it("allows to override the timezone", () => { + expect( + new UTCDate(1999, 11, 31, 23, 59, 59).toLocaleDateString("en-GB", { + timeZone: "Asia/Kolkata", + }) + ).toBe("01/01/2000"); + }); + }); + + describe("toLocaleTimeString", () => { + it("returns localized time string", () => { + expect( + new UTCDate(1987, 1, 11, 12, 13, 14, 15).toLocaleTimeString("en-GB") + ).toBe("12:13:14"); + }); + + it("allows to pass options", () => { + expect( + new UTCDate(1987, 1, 11, 12, 13, 14, 15).toLocaleTimeString("en-GB", { + hour: "numeric", + }) + ).toBe("12"); + }); + + it("allows to override the timezone", () => { + expect( + new UTCDate(1987, 1, 11, 12, 13, 14, 15).toLocaleTimeString("en-GB", { + timeZone: "Asia/Kolkata", + }) + ).toBe("17:43:14"); + }); + }); });