Skip to content

Commit

Permalink
#1508 test more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Nov 5, 2024
1 parent a3c3ca3 commit 6b0c98a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
38 changes: 37 additions & 1 deletion starsky/starsky/clientapp/src/shared/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("date", () => {

it("Timezone time", () => {
const result = parseDate("2020-04-28T10:44:43.123456+01:00", SupportedLanguages.nl);
expect(result).not.toBe("Invalid Date");
expect(result).toBe("dinsdag 28 april 2020");
});

it("wrong format", () => {
Expand Down Expand Up @@ -57,6 +57,11 @@ describe("date", () => {
expect(result).toBe("01:01:01");
});

it("Timezone time (parseTime)", () => {
const result = parseTime("2020-04-28T10:44:43.123456+01:00");
expect(result).toBe("09:44:43");
});

it("right formatted summer time (nl)", () => {
const result = parseTime("2020-04-10T23:40:33");
expect(result).toBe("23:40:33");
Expand All @@ -79,6 +84,11 @@ describe("date", () => {
expect(result).toBe(1);
});

it("Timezone time (parseTimeHour)", () => {
const result = parseTimeHour("2020-04-28T10:44:43.123456+01:00");
expect(result).toBe(9);
});

it("right formatted summer time (nl)", () => {
const result = parseTimeHour("2020-04-10T23:40:33");
expect(result).toBe(23);
Expand All @@ -101,6 +111,11 @@ describe("date", () => {
expect(result).toBe(1);
});

it("Timezone time (parseDateDate)", () => {
const result = parseDateDate("2020-04-28T10:44:43.123456+01:00");
expect(result).toBe(28);
});

it("right formatted summer time (nl)", () => {
const result = parseDateDate("2020-04-10T23:40:33");
expect(result).toBe(10);
Expand All @@ -123,6 +138,11 @@ describe("date", () => {
expect(result).toBe(1);
});

it("Timezone time (parseDateMonth)", () => {
const result = parseDateMonth("2020-04-28T10:44:43.123456+01:00");
expect(result).toBe(4);
});

it("right formatted summer time (nl)", () => {
const result = parseDateMonth("2020-12-10T23:40:33");
expect(result).toBe(12);
Expand All @@ -145,6 +165,11 @@ describe("date", () => {
expect(result).toBe(2020);
});

it("Timezone time (parseDateYear)", () => {
const result = parseDateYear("2020-04-28T10:44:43.123456+01:00");
expect(result).toBe(2020);
});

it("right formatted summer time (nl)", () => {
const result = parseDateYear("2020-12-10T23:40:33");
expect(result).toBe(2020);
Expand All @@ -166,6 +191,12 @@ describe("date", () => {
const result = isValidDate("2019-10-12 14:12:00");
expect(result).toBeTruthy();
});

it("Timezone time (isValidDate)", () => {
const result = isValidDate("2020-04-28T10:44:43.123456+01:00");
expect(result).toBe(true);
});

});

describe("parseRelativeDate", () => {
Expand All @@ -184,6 +215,11 @@ describe("date", () => {
expect(result).toBe("");
});

it("Timezone time (isValidDate)", () => {
const result = parseRelativeDate("2020-04-28T10:44:43.123456+01:00", SupportedLanguages.en);
expect(result).toBe("Tuesday, 28 April 2020");
});

it("yesterday", () => {
const yesterdayDate = new Date();

Expand Down
20 changes: 12 additions & 8 deletions starsky/starsky/clientapp/src/shared/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,22 @@ const parseRelativeDate = (
return parseDate(inputDateTime, locate);
}
};

const IsIncludeTimezone = (dateTime: string): boolean => {
const timeZoneRegex = /\+\d{2}:\d{2}/;
return timeZoneRegex.test(dateTime);
}

/**
* Get Date complete parsed for example: Monday, 4 May 2020
* @param dateTime 2018-09-11T11:23:19, 2018-09-11T11:23:19Z or 2020-04-28T10:44:43.123456+01:00
* @param locate Language
*/
const parseDate = (dateTime: string | undefined, locate: SupportedLanguages): string => {
if (!dateTime) return "";
const timeZoneRegex = /\+\d{2}:\d{2}/;
const isIncludeTimezone = timeZoneRegex.test(dateTime);

// UTC DateTime already ends with Z
const dateTimeObject = new Date(!dateTime.endsWith("Z") && !isIncludeTimezone ? `${dateTime}Z` : dateTime);
const dateTimeObject = new Date(!dateTime.endsWith("Z") && !IsIncludeTimezone(dateTime) ? `${dateTime}Z` : dateTime);
// We prefer British English, uses day-month-year order
const locateString = locate === SupportedLanguages.en ? "en-GB" : locate.toString();
if (dateTime.endsWith("Z")) {
Expand Down Expand Up @@ -110,7 +114,7 @@ const parseDateDate = (dateTime: string | undefined): number => {
if (!isValidDate(dateTime) || !dateTime) {
return 1;
}
const dateTimeObject = new Date(!dateTime.endsWith("Z") ? `${dateTime}Z` : dateTime);
const dateTimeObject = new Date(!dateTime.endsWith("Z") && !IsIncludeTimezone(dateTime) ? `${dateTime}Z` : dateTime);
// toLocaleDateString assumes that the input is UTC, which is usually not the case
const numberValue = dateTimeObject.toLocaleDateString([], {
timeZone: !dateTime.endsWith("Z") ? "UTC" : undefined,
Expand All @@ -127,7 +131,7 @@ const parseDateYear = (dateTime: string | undefined): number => {
if (!isValidDate(dateTime) || !dateTime) {
return 1;
}
const dateTimeObject = new Date(!dateTime.endsWith("Z") ? `${dateTime}Z` : dateTime);
const dateTimeObject = new Date(!dateTime.endsWith("Z") && !IsIncludeTimezone(dateTime) ? `${dateTime}Z` : dateTime);
// toLocaleDateString assumes that the input is UTC, which is usually not the case
const numberValue = dateTimeObject.toLocaleDateString([], {
timeZone: !dateTime.endsWith("Z") ? "UTC" : undefined,
Expand All @@ -144,7 +148,7 @@ const parseDateMonth = (dateTime: string | undefined): number => {
if (!isValidDate(dateTime) || !dateTime) {
return 1;
}
const dateTimeObject = new Date(!dateTime.endsWith("Z") ? `${dateTime}Z` : dateTime);
const dateTimeObject = new Date(!dateTime.endsWith("Z") && !IsIncludeTimezone(dateTime) ? `${dateTime}Z` : dateTime);
// toLocaleDateString assumes that the input is UTC, which is usually not the case
const numberValue = dateTimeObject.toLocaleDateString([], {
timeZone: !dateTime.endsWith("Z") ? "UTC" : undefined,
Expand All @@ -161,7 +165,7 @@ const parseTime = (dateTime: string | undefined): string => {
if (!isValidDate(dateTime) || !dateTime) {
return "";
}
const dateTimeObject = new Date(!dateTime.endsWith("Z") ? `${dateTime}Z` : dateTime);
const dateTimeObject = new Date(!dateTime.endsWith("Z") && !IsIncludeTimezone(dateTime) ? `${dateTime}Z` : dateTime);

// toLocaleDateString assumes that the input is UTC, which is usually not the case
return dateTimeObject.toLocaleTimeString([], {
Expand All @@ -181,7 +185,7 @@ const parseTimeHour = (dateTime: string | undefined): number => {
if (!isValidDate(dateTime) || !dateTime) {
return 1;
}
const dateTimeObject = new Date(!dateTime.endsWith("Z") ? `${dateTime}Z` : dateTime);
const dateTimeObject = new Date(!dateTime.endsWith("Z") && !IsIncludeTimezone(dateTime) ? `${dateTime}Z` : dateTime);
// toLocaleDateString assumes that the input is UTC, which is usually not the case
const numberValue = dateTimeObject.toLocaleTimeString([], {
timeZone: !dateTime.endsWith("Z") ? "UTC" : undefined,
Expand Down

0 comments on commit 6b0c98a

Please sign in to comment.