Skip to content

Commit

Permalink
Complete UTCDate implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp committed Jun 27, 2022
1 parent 6fb4b0d commit 88271ff
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 2 deletions.
43 changes: 41 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<keyof Date> = [
Expand Down
96 changes: 96 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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", () => {
Expand All @@ -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");
});
});
});

0 comments on commit 88271ff

Please sign in to comment.