diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-26 15:04:37 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-26 15:04:37 +0200 |
commit | 15a7eafb892441957ba7dd6fcbf556086fe5b2b3 (patch) | |
tree | 0786bd1a96c7d168a097ffcf5893737db2ab578e /shared/core-utils/common/date.ts | |
parent | 9162fdd36300d2478f13d6ad346ec2c323f40faa (diff) | |
download | PeerTube-15a7eafb892441957ba7dd6fcbf556086fe5b2b3.tar.gz PeerTube-15a7eafb892441957ba7dd6fcbf556086fe5b2b3.tar.zst PeerTube-15a7eafb892441957ba7dd6fcbf556086fe5b2b3.zip |
Refactor video links builders
Diffstat (limited to 'shared/core-utils/common/date.ts')
-rw-r--r-- | shared/core-utils/common/date.ts | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/shared/core-utils/common/date.ts b/shared/core-utils/common/date.ts index 4f92f758f..3e4a3c08c 100644 --- a/shared/core-utils/common/date.ts +++ b/shared/core-utils/common/date.ts | |||
@@ -43,6 +43,49 @@ function isLastWeek (d: Date) { | |||
43 | return getDaysDifferences(now, d) <= 7 | 43 | return getDaysDifferences(now, d) <= 7 |
44 | } | 44 | } |
45 | 45 | ||
46 | function timeToInt (time: number | string) { | ||
47 | if (!time) return 0 | ||
48 | if (typeof time === 'number') return time | ||
49 | |||
50 | const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/ | ||
51 | const matches = time.match(reg) | ||
52 | |||
53 | if (!matches) return 0 | ||
54 | |||
55 | const hours = parseInt(matches[2] || '0', 10) | ||
56 | const minutes = parseInt(matches[4] || '0', 10) | ||
57 | const seconds = parseInt(matches[6] || '0', 10) | ||
58 | |||
59 | return hours * 3600 + minutes * 60 + seconds | ||
60 | } | ||
61 | |||
62 | function secondsToTime (seconds: number, full = false, symbol?: string) { | ||
63 | let time = '' | ||
64 | |||
65 | if (seconds === 0 && !full) return '0s' | ||
66 | |||
67 | const hourSymbol = (symbol || 'h') | ||
68 | const minuteSymbol = (symbol || 'm') | ||
69 | const secondsSymbol = full ? '' : 's' | ||
70 | |||
71 | const hours = Math.floor(seconds / 3600) | ||
72 | if (hours >= 1) time = hours + hourSymbol | ||
73 | else if (full) time = '0' + hourSymbol | ||
74 | |||
75 | seconds %= 3600 | ||
76 | const minutes = Math.floor(seconds / 60) | ||
77 | if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol | ||
78 | else if (minutes >= 1) time += minutes + minuteSymbol | ||
79 | else if (full) time += '00' + minuteSymbol | ||
80 | |||
81 | seconds %= 60 | ||
82 | if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol | ||
83 | else if (seconds >= 1) time += seconds + secondsSymbol | ||
84 | else if (full) time += '00' | ||
85 | |||
86 | return time | ||
87 | } | ||
88 | |||
46 | // --------------------------------------------------------------------------- | 89 | // --------------------------------------------------------------------------- |
47 | 90 | ||
48 | export { | 91 | export { |
@@ -51,7 +94,9 @@ export { | |||
51 | isThisMonth, | 94 | isThisMonth, |
52 | isToday, | 95 | isToday, |
53 | isLastMonth, | 96 | isLastMonth, |
54 | isLastWeek | 97 | isLastWeek, |
98 | timeToInt, | ||
99 | secondsToTime | ||
55 | } | 100 | } |
56 | 101 | ||
57 | // --------------------------------------------------------------------------- | 102 | // --------------------------------------------------------------------------- |