Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/202411 invalid date #1508 #1802

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions history.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Semantic Versioning 2.0.0 is from version 0.1.6+
- [x] (Fixed) _Back-end_ Suggest API gives 200 when no suggestions are found (PR #1797)
- [x] (Fixed) _Back-end_ Timeout in Thumbnail cleaner fixed (PR #1798)
- [x] (Fixed) _Back-end_ Add logging for thumbnail background scanning (PR #1798)
- [x] (Fixed) _Front-end_ Invalid date parsing (PR #1802, Issue #1508)

## version 0.6.2 - 2024-10-11 {#v0.6.2}

Expand Down
42 changes: 42 additions & 0 deletions starsky/starsky/clientapp/src/shared/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ describe("date", () => {
// NOT Invalid!
expect(result).not.toBe("Invalid Date");
});

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

it("wrong format", () => {
const result = parseDate("2020-30", SupportedLanguages.nl);
expect(result).toBe("Invalid Date");
Expand Down Expand Up @@ -51,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 @@ -73,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 @@ -95,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 @@ -117,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 @@ -139,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 @@ -160,6 +191,11 @@ 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 @@ -178,6 +214,12 @@ describe("date", () => {
expect(result).toBe("");
});

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

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

Expand Down
33 changes: 26 additions & 7 deletions starsky/starsky/clientapp/src/shared/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,24 @@ 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 or 2018-09-11T11:23:19Z
* @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 "";

// UTC DateTime already ends with Z
const dateTimeObject = new Date(!dateTime.endsWith("Z") ? `${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 @@ -107,7 +116,9 @@ 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 @@ -124,7 +135,9 @@ 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 @@ -141,7 +154,9 @@ 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 @@ -158,7 +173,9 @@ 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 @@ -178,7 +195,9 @@ 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