aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/common/date.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /shared/core-utils/common/date.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'shared/core-utils/common/date.ts')
-rw-r--r--shared/core-utils/common/date.ts114
1 files changed, 0 insertions, 114 deletions
diff --git a/shared/core-utils/common/date.ts b/shared/core-utils/common/date.ts
deleted file mode 100644
index f0684ff86..000000000
--- a/shared/core-utils/common/date.ts
+++ /dev/null
@@ -1,114 +0,0 @@
1function isToday (d: Date) {
2 const today = new Date()
3
4 return areDatesEqual(d, today)
5}
6
7function isYesterday (d: Date) {
8 const yesterday = new Date()
9 yesterday.setDate(yesterday.getDate() - 1)
10
11 return areDatesEqual(d, yesterday)
12}
13
14function isThisWeek (d: Date) {
15 const minDateOfThisWeek = new Date()
16 minDateOfThisWeek.setHours(0, 0, 0)
17
18 // getDay() -> Sunday - Saturday : 0 - 6
19 // We want to start our week on Monday
20 let dayOfWeek = minDateOfThisWeek.getDay() - 1
21 if (dayOfWeek < 0) dayOfWeek = 6 // Sunday
22
23 minDateOfThisWeek.setDate(minDateOfThisWeek.getDate() - dayOfWeek)
24
25 return d >= minDateOfThisWeek
26}
27
28function isThisMonth (d: Date) {
29 const thisMonth = new Date().getMonth()
30
31 return d.getMonth() === thisMonth
32}
33
34function isLastMonth (d: Date) {
35 const now = new Date()
36
37 return getDaysDifferences(now, d) <= 30
38}
39
40function isLastWeek (d: Date) {
41 const now = new Date()
42
43 return getDaysDifferences(now, d) <= 7
44}
45
46// ---------------------------------------------------------------------------
47
48function timeToInt (time: number | string) {
49 if (!time) return 0
50 if (typeof time === 'number') return time
51
52 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
53 const matches = time.match(reg)
54
55 if (!matches) return 0
56
57 const hours = parseInt(matches[2] || '0', 10)
58 const minutes = parseInt(matches[4] || '0', 10)
59 const seconds = parseInt(matches[6] || '0', 10)
60
61 return hours * 3600 + minutes * 60 + seconds
62}
63
64function secondsToTime (seconds: number, full = false, symbol?: string) {
65 let time = ''
66
67 if (seconds === 0 && !full) return '0s'
68
69 const hourSymbol = (symbol || 'h')
70 const minuteSymbol = (symbol || 'm')
71 const secondsSymbol = full ? '' : 's'
72
73 const hours = Math.floor(seconds / 3600)
74 if (hours >= 1) time = hours + hourSymbol
75 else if (full) time = '0' + hourSymbol
76
77 seconds %= 3600
78 const minutes = Math.floor(seconds / 60)
79 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
80 else if (minutes >= 1) time += minutes + minuteSymbol
81 else if (full) time += '00' + minuteSymbol
82
83 seconds %= 60
84 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
85 else if (seconds >= 1) time += seconds + secondsSymbol
86 else if (full) time += '00'
87
88 return time
89}
90
91// ---------------------------------------------------------------------------
92
93export {
94 isYesterday,
95 isThisWeek,
96 isThisMonth,
97 isToday,
98 isLastMonth,
99 isLastWeek,
100 timeToInt,
101 secondsToTime
102}
103
104// ---------------------------------------------------------------------------
105
106function areDatesEqual (d1: Date, d2: Date) {
107 return d1.getFullYear() === d2.getFullYear() &&
108 d1.getMonth() === d2.getMonth() &&
109 d1.getDate() === d2.getDate()
110}
111
112function getDaysDifferences (d1: Date, d2: Date) {
113 return (d1.getTime() - d2.getTime()) / (86400000)
114}