Skip to content

Commit

Permalink
#1508 invalid date time
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Nov 5, 2024
1 parent 1afc7ff commit 3d741f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
16 changes: 11 additions & 5 deletions starsky/starsky/clientapp/src/shared/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
parseTimeHour,
SecondsToHours
} from "./date";
import { SupportedLanguages } from "./language";
import {SupportedLanguages} from "./language";

describe("date", () => {
describe("parseDate", () => {
Expand All @@ -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).not.toBe("Invalid Date");
});

it("wrong format", () => {
const result = parseDate("2020-30", SupportedLanguages.nl);
expect(result).toBe("Invalid Date");
Expand Down Expand Up @@ -219,11 +225,11 @@ describe("date", () => {

let dayBeforeYesterday = `${dayBeforeYesterdayDate.getFullYear()}-
${leftPad(dayBeforeYesterdayDate.getMonth() + 1)}-${leftPad(
dayBeforeYesterdayDate.getDate()
)}T
dayBeforeYesterdayDate.getDate()
)}T
${leftPad(dayBeforeYesterdayDate.getHours())}:${leftPad(
dayBeforeYesterdayDate.getMinutes()
)}:
dayBeforeYesterdayDate.getMinutes()
)}:
${leftPad(dayBeforeYesterdayDate.getSeconds())}`;

// remove space and newlines from prev variable
Expand Down
7 changes: 5 additions & 2 deletions starsky/starsky/clientapp/src/shared/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SupportedLanguages } from "./language";
import {SupportedLanguages} from "./language";

/**
* Is the date Valid?
Expand Down Expand Up @@ -77,8 +77,11 @@ const parseRelativeDate = (
*/
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") ? `${dateTime}Z` : dateTime);
const dateTimeObject = new Date(!dateTime.endsWith("Z") && !isIncludeTimezone ? `${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

0 comments on commit 3d741f4

Please sign in to comment.