-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathTimeBase.js
154 lines (142 loc) · 3.19 KB
/
TimeBase.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
define([
"dcl/dcl",
"luxon"
], function (
dcl,
luxon
) {
var DateTime = luxon.DateTime;
/**
* Mixin with time methods.
*/
return dcl(null, {
/**
* First day of the week.
* 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday.
*/
firstDayOfWeek: 0,
/**
* Returns a hash containing the start and end days of the weekend in the specified locale,
* where 0 = Sunday, 1 = Monday, ... and 6 = Saturday.
* e.g. {start:6, end:0}
* @param locale
* @returns {{start, end}}
*/
getWeekend: function (/*String?*/locale) {
var weekendStart = {/*default is 6=Saturday*/
"in": 0,
"af": 4,
"dz": 4,
"ir": 4,
"om": 4,
"sa": 4,
"ye": 4,
"ae": 5,
"bh": 5,
"eg": 5,
"il": 5,
"iq": 5,
"jo": 5,
"kw": 5,
"ly": 5,
"ma": 5,
"qa": 5,
"sd": 5,
"sy": 5,
"tn": 5
},
weekendEnd = {/*default is 0=Sunday*/
af: 5,
dz: 5,
ir: 5,
om: 5,
sa: 5,
ye: 5,
ae: 6,
bh: 5,
eg: 6,
il: 6,
iq: 6,
jo: 6,
kw: 6,
ly: 6,
ma: 6,
qa: 6,
sd: 6,
sy: 6,
tn: 6
},
country = locale.split("-")[1].toLowerCase(),
start = weekendStart[country],
end = weekendEnd[country];
if (start === undefined) {
start = 6;
}
if (end === undefined) {
end = 0;
}
return { start: start, end: end };
},
/**
* Determines whether the specified date is a week-end.
* @param {DateTime} date
* @returns {boolean}
*/
isWeekEnd: function (date) {
var weekend = this.getWeekend(date.locale),
day = date.weekday;
if (weekend.end < weekend.start) {
weekend.end += 7;
if (day < weekend.start) {
day += 7;
}
}
return day >= weekend.start && day <= weekend.end;
},
/**
* Floors the specified date to the beginning of week, according to this.firstDayOfWeek.
* Call this instead of startOf("week") because startOf("week") always uses Monday.
* @param {DateTime} date
* @returns {DateTime}
*/
floorToWeek: function (date) {
const fd = this.firstDayOfWeek;
const day = date.weekday % 7; // convert to 0=sunday .. 6=saturday
const dayAdjust = day >= fd ? -day + fd : -day + fd - 7;
return date.plus({days: dayAdjust});
},
/**
* Returns whether the specified date is in the current day.
* @param {DateTime} date
* @returns {boolean}
*/
isToday: function (date) {
let today = DateTime.local();
return date.year === today.year &&
date.month === today.month &&
date.day === today.day;
},
/**
* Tests if the specified date represents the start of the day.
* @param {DateTime} date
* @returns {boolean}
*/
isStartOfDay: function (date) {
return +date === +date.startOf("day");
},
/**
* Tests if the specified dates are in the same day.
* @param {DateTime} date1 - The first date.
* @param {DateTime} date2 - The second date.
* @returns {boolean}
*/
isSameDay: function (date1, date2) {
if (date1 === null || date2 === null) {
return false;
}
return date1.year === date2.year &&
date1.month === date2.month &&
date1.day === date2.day;
}
});
});