Commit 8e47ec0 1 parent f6ff259 commit 8e47ec0 Copy full SHA for 8e47ec0
File tree 1 file changed +11
-23
lines changed
1 file changed +11
-23
lines changed Original file line number Diff line number Diff line change 1
1
type UnixTime = number ;
2
2
3
3
class InvalidDateError extends Error { }
4
+ class WrongTimestamp extends Error { }
4
5
5
6
// Takes the date in seconds from Epoch time and converts it to human readable format.
6
7
7
8
export function unixTimeToHumanReadableUTC ( creationDate : UnixTime ) {
9
+ let milliseconds ;
10
+ let convertedDate ;
8
11
try {
9
- return dateFromSeconds ( creationDate ) ;
12
+ milliseconds = creationDate * 1000 ;
10
13
} catch ( error ) {
11
- return ( "Invalid date" ) ;
14
+ return new WrongTimestamp ( `Could not convert ${ creationDate } to milliseconds` ) ;
12
15
}
13
- }
14
-
15
- function dateFromSeconds ( seconds : number ) {
16
- if ( ! validateTimestamp ( seconds ) )
17
- { throw new TypeError ( "Torrent creation date is not an integer" ) ; }
18
-
19
- const milliseconds = seconds * 1000 ;
20
- const convertedDate = new Date ( milliseconds ) ;
21
-
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 ) ;
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 } ` ) ;
34
22
}
You can’t perform that action at this time.
0 commit comments