Commit 85a927c 1 parent 8e47ec0 commit 85a927c Copy full SHA for 85a927c
File tree 2 files changed +20
-19
lines changed
2 files changed +20
-19
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
- {{ unixTimeToHumanReadableUTC(torrent.creation_date) }}
17
+ {{ formatedDateFromTimestamp }}
18
18
</template >
19
19
<template v-else >
20
20
<span class =" italic text-neutral-content" >No creation date provided.</span >
@@ -30,7 +30,7 @@ import { TorrentResponse } from "torrust-index-types-lib";
30
30
import { PropType } from " vue" ;
31
31
import { ref } from " #imports" ;
32
32
import Markdown from " ~/components/Markdown.vue" ;
33
- import { unixTimeToHumanReadableUTC } from " ~/src/helpers/DateConverter" ;
33
+ import { formatTimestamp } from " ~/src/helpers/DateConverter" ;
34
34
35
35
const collapsed = ref (false );
36
36
@@ -41,6 +41,8 @@ const props = defineProps({
41
41
}
42
42
});
43
43
44
+ const formatedDateFromTimestamp = formatTimestamp (props .torrent .creation_date );
45
+
44
46
</script >
45
47
46
48
<style scoped>
Original file line number Diff line number Diff line change 1
- type UnixTime = number ;
1
+ type UnixTimestamp = number ;
2
+ type FormattedDate = string ;
2
3
3
4
class InvalidDateError extends Error { }
4
- class WrongTimestamp extends Error { }
5
5
6
- // Takes the date in seconds from Epoch time and converts it to human readable format.
6
+ /**
7
+ * Takes the date in seconds from Unix Epoch time and converts it to human readable format.
8
+ *
9
+ * For example: 1701688451 -> "Mon Dec 04 2023"
10
+ */
7
11
8
- export function unixTimeToHumanReadableUTC ( creationDate : UnixTime ) {
9
- let milliseconds ;
10
- let convertedDate ;
11
- try {
12
- milliseconds = creationDate * 1000 ;
13
- } catch ( error ) {
14
- return new WrongTimestamp ( `Could not convert ${ creationDate } to milliseconds` ) ;
15
- }
16
- try {
17
- convertedDate = new Date ( milliseconds ) ;
18
- } catch ( error ) {
19
- return new InvalidDateError ( `Could not create a new date from ${ milliseconds } ` ) ;
20
- }
21
- return ! isNaN ( convertedDate . valueOf ( ) ) ? convertedDate . toDateString ( ) : new InvalidDateError ( `Could not create a valid date from ${ milliseconds } ` ) ;
12
+ export function formatTimestamp ( creationDate : UnixTimestamp ) : FormattedDate | Error {
13
+ const milliseconds = creationDate * 1000 ;
14
+
15
+ const convertedDate = new Date ( milliseconds ) ;
16
+
17
+ return isNaN ( convertedDate . valueOf ( ) )
18
+ ? new InvalidDateError (
19
+ `Invalid date. Could not create a new date from timestamp value: ${ creationDate } ` )
20
+ : convertedDate . toDateString ( ) ;
22
21
}
You can’t perform that action at this time.
0 commit comments