Skip to content

Commit

Permalink
refactor: [#297] added error checks
Browse files Browse the repository at this point in the history
Error checks added to the function that converts the torrent creation date from seconds to
UTC human readable format.

Refactor to the function logic's code has been applied.
  • Loading branch information
mario-nt committed Nov 27, 2023
1 parent 964a7cd commit ad0c609
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/helpers/DateConverter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
type UnixTime = number;

/*
Takes the date in seconds from Epoch time and converts it to human readable format.
Date() can only work with integers between -8640000000000000 and 8640000000000000 to create a new Date object.
*/
function isValidDate (date: Date) {
return date instanceof Date && !isNaN(date.valueOf());
}

// Takes the date in seconds from Epoch time and converts it to human readable format.

export function unixTimeToHumanReadableUTC (seconds: UnixTime) {
if (Number.isInteger(seconds) && seconds < 8640000000000000 && seconds > -8640000000000000) {
const milliseconds = seconds * 1000;
return new Date(milliseconds).toDateString();
} else {
return "Invalid date";
}
const milliseconds = seconds * 1000;
const convertedDate = new Date(milliseconds);

return isValidDate(convertedDate) ? convertedDate.toDateString() : "Invalid date";
}

0 comments on commit ad0c609

Please sign in to comment.