Skip to content

Commit

Permalink
Modified datetime parse
Browse files Browse the repository at this point in the history
  • Loading branch information
suecharo committed Sep 17, 2022
1 parent 32890d4 commit 95de1ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/crate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class Crate {
}`;
let startTime: Date;
try {
startTime = datetime.parse(startTimeStr, "yyyy-MM-dd'T'HH:mm:ss");
startTime = utils.parseDatetime(startTimeStr);
} catch (_) {
throw new Error(`Invalid start time ${startTimeStr}`);
}
Expand All @@ -158,7 +158,7 @@ export class Crate {
}`;
let endTime: Date;
try {
endTime = datetime.parse(endTimeStr, "yyyy-MM-dd'T'HH:mm:ss");
endTime = utils.parseDatetime(endTimeStr);
} catch (_) {
throw new Error(`Invalid end time ${endTimeStr}`);
}
Expand Down Expand Up @@ -328,10 +328,7 @@ export class Entity {
if (dateModified == undefined) {
throw new Error(`Entity ${this.id} has no dateModified`);
}
const dateModifiedDate = datetime.parse(
dateModified,
"yyyy-MM-dd'T'HH:mm:ss.SSSSSS",
);
const dateModifiedDate = utils.parseDatetime(dateModified);
const duration = datetime.difference(
this.crate.summary.startTime,
dateModifiedDate,
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,11 @@ export function warningColor(pair: string[]): string[] {
? [color.yellow(pair[0]), color.yellow(pair[1])]
: pair;
}

export function parseDatetime(datetimeStr: string): Date {
const datetime = new Date(datetimeStr);
if (isNaN(datetime.getTime())) {
throw new Error(`Invalid datetime: ${datetimeStr}`);
}
return datetime;
}

0 comments on commit 95de1ae

Please sign in to comment.