-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdates.ts
More file actions
101 lines (90 loc) · 3.03 KB
/
dates.ts
File metadata and controls
101 lines (90 loc) · 3.03 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
100
101
import { ObjectId } from 'mongodb';
/**
* Return timestamp for UTC midnight of the passed date
*
* @param {Date} date - date object
*/
export function getUTCMidnight(date: Date): number {
const copy = new Date(date);
return copy.setUTCHours(0, 0, 0, 0);
}
/**
* Sets UTC midnight for a given date
*
* @param {Date} date — date to set midnight
*/
export function setUTCMidnight(date: Date): void {
date.setHours(0, 0, 0, 0);
}
/**
* Adds passed offset in minutes to a given date
*
* @param {Date} date — date to change
* @param {number} offset — offset in minutes
*/
export function addTimezoneOffset(date: Date, offset: number): void {
date.setTime(date.getTime() + offset * 60 * 1000);
}
/**
* We have occurrence time and midnight in UTC, and user's timezone offset
* We need to compute local occurrence time and its midnight
*
* Explanation:
*
* Event accepted at 13/05 at 1:30 in UTC+3 local.
*
* It means that occurrence time will be:
* Local 13/05/2020, 01:30:00 <-- we need to find this value
* UTC 12/05/2020, 22:30:00 <-- we have this value in db (lastRepetitionTime)
* The midnight mill be:
* Local 12/05/2020, 03:00:00
* UTC 12/05/2020, 00:00:00 <--- so event stored in 12/05 group, but user should see it at 13/05 group
*
* We get 12/05/2020, 22:30:00, and compute timezone diff in hours and minutes:
* Hours diff: 22 - (-180/60) = 22 + 3 = 25
* Minutes diff: 30 - 00 = 30
*
* Next, we will add hours and minutes diff to real (UTC) midnight:
* 12/05/2020, 00:00:00 + 25 hours + 30 min = 13/05/2020, 01:30:00 <-- now we have local occurrence time
*
* Then, we can compute local midnight group by local time.
*
*
* @param utcOccurrenceTime - last repetition time in UTC
* @param utcMidnight - event day's midnight in UTC
* @param timezoneOffset - user's time zone offset (in minutes)
*
* @returns midnight in local timezone
*/
export function getMidnightWithTimezoneOffset(utcOccurrenceTime: number, utcMidnight: number, timezoneOffset: number): number {
const milliseconds = 1000;
const hour = 60;
const timezoneOffsetHours = Math.ceil(timezoneOffset / hour);
const timezoneOffsetMinutes = timezoneOffset % hour;
/**
* Compute hours and minutes diff of occurrence time
*/
const dateOccur = new Date(utcOccurrenceTime * milliseconds);
const hoursDiff = dateOccur.getUTCHours() - timezoneOffsetHours;
const minutesDiff = dateOccur.getUTCMinutes() - timezoneOffsetMinutes;
/**
* Add computed diff to the utc midnight
*/
const localDate = new Date(utcMidnight * milliseconds);
localDate.setUTCHours(hoursDiff);
localDate.setUTCMinutes(minutesDiff);
/**
* Now we have local occurrence time,
* so we can get its midnight
*/
const localMidnight = getUTCMidnight(localDate);
return localMidnight / milliseconds;
}
/**
* Returns date parsed from object id
*
* @param objectId - id of entity for getting date
*/
export function dateFromObjectId(objectId: ObjectId): Date {
return objectId.getTimestamp();
}