Commit f6ff259 1 parent f26a2e3 commit f6ff259 Copy full SHA for f6ff259
File tree 2 files changed +26
-9
lines changed
2 files changed +26
-9
lines changed Original file line number Diff line number Diff line change 14
14
<template v-if =" ! collapsed " >
15
15
<div class =" flex flex-col w-full h-full p-6 grow bg-base-100 rounded-2xl" >
16
16
<template v-if =" torrent .creation_date " >
17
- <!-- <Markdown :source="torrent.name" />-->
18
- {{ creationDateUTC }}
17
+ {{ unixTimeToHumanReadableUTC(torrent.creation_date) }}
19
18
</template >
20
19
<template v-else >
21
20
<span class =" italic text-neutral-content" >No creation date provided.</span >
@@ -42,8 +41,6 @@ const props = defineProps({
42
41
}
43
42
});
44
43
45
- const creationDateUTC = unixTimeToHumanReadableUTC (props .torrent .creation_date );
46
-
47
44
</script >
48
45
49
46
<style scoped>
Original file line number Diff line number Diff line change 1
1
type UnixTime = number ;
2
2
3
- function isValidDate ( date : Date ) {
4
- return date instanceof Date && ! isNaN ( date . valueOf ( ) ) ;
5
- }
3
+ class InvalidDateError extends Error { }
6
4
7
5
// Takes the date in seconds from Epoch time and converts it to human readable format.
8
6
9
- export function unixTimeToHumanReadableUTC ( seconds : UnixTime ) {
7
+ export function unixTimeToHumanReadableUTC ( creationDate : UnixTime ) {
8
+ try {
9
+ return dateFromSeconds ( creationDate ) ;
10
+ } catch ( error ) {
11
+ return ( "Invalid date" ) ;
12
+ }
13
+ }
14
+
15
+ function dateFromSeconds ( seconds : number ) {
16
+ if ( ! validateTimestamp ( seconds ) )
17
+ { throw new TypeError ( "Torrent creation date is not an integer" ) ; }
18
+
10
19
const milliseconds = seconds * 1000 ;
11
20
const convertedDate = new Date ( milliseconds ) ;
12
21
13
- return isValidDate ( convertedDate ) ? convertedDate . toDateString ( ) : "Invalid date" ;
22
+ if ( ! validateDate ( convertedDate ) )
23
+ { throw new InvalidDateError ( "Date is not valid" ) ; }
24
+
25
+ return convertedDate . toDateString ( ) ;
26
+ }
27
+
28
+ function validateDate ( date : Date ) {
29
+ return date instanceof Date && ! isNaN ( date . valueOf ( ) ) ;
30
+ }
31
+
32
+ function validateTimestamp ( timestamp : UnixTime ) {
33
+ return Number . isInteger ( timestamp ) ;
14
34
}
You can’t perform that action at this time.
0 commit comments