-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathclearAtService.js
More file actions
99 lines (86 loc) · 2.28 KB
/
Copy pathclearAtService.js
File metadata and controls
99 lines (86 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { formatRelativeTime, getFirstDay, t } from '@nextcloud/l10n'
import { dateFactory } from './dateService.js'
/**
* Calculates the actual clearAt timestamp
*
* @param {object | null} clearAt The clear-at config
* @return {number | null}
*/
export function getTimestampForClearAt(clearAt) {
if (clearAt === null) {
return null
}
const date = dateFactory()
if (clearAt.type === 'period') {
date.setSeconds(date.getSeconds() + clearAt.time)
return Math.floor(date.getTime() / 1000)
}
if (clearAt.type === 'end-of') {
switch (clearAt.time) {
case 'day':
return Math.floor(getEndOfDay(date).getTime() / 1000)
case 'week':
return Math.floor(getEndOfWeek(date).getTime() / 1000)
}
}
// This is not an officially supported type
// but only used internally to show the remaining time
// in the Set Status Modal
if (clearAt.type === '_time') {
return clearAt.time
}
return null
}
/**
* Formats a clearAt object to be human readable
*
* @param {object} clearAt The clearAt object
* @return {string|null}
*/
export function clearAtFormat(clearAt) {
if (clearAt === null) {
return t('user_status', 'Don\'t clear')
}
if (clearAt.type === 'end-of') {
switch (clearAt.time) {
case 'day':
return t('user_status', 'Today')
case 'week':
return t('user_status', 'This week')
default:
return null
}
}
if (clearAt.type === 'period') {
return formatRelativeTime(Date.now() + clearAt.time * 1000)
}
// This is not an officially supported type
// but only used internally to show the remaining time
// in the Set Status Modal
if (clearAt.type === '_time') {
return formatRelativeTime(clearAt.time * 1000)
}
return null
}
/**
* @param {Date} date - The date to calculate the end of the day for
*/
function getEndOfDay(date) {
const endOfDay = new Date(date)
endOfDay.setHours(23, 59, 59, 999)
return endOfDay
}
/**
* Calculates the end of the week for a given date
*
* @param {Date} date - The date to calculate the end of the week for
*/
function getEndOfWeek(date) {
const endOfWeek = getEndOfDay(date)
endOfWeek.setDate(date.getDate() + ((getFirstDay() - 1 - endOfWeek.getDay() + 7) % 7))
return endOfWeek
}