-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
moment.js
370 lines (311 loc) · 9.99 KB
/
moment.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import { DateLocalizer } from '../localizer'
const weekRangeFormat = ({ start, end }, culture, local) =>
local.format(start, 'MMMM DD', culture) +
' – ' +
// updated to use this localizer 'eq()' method
local.format(end, local.eq(start, end, 'month') ? 'DD' : 'MMMM DD', culture)
const dateRangeFormat = ({ start, end }, culture, local) =>
local.format(start, 'L', culture) + ' – ' + local.format(end, 'L', culture)
const timeRangeFormat = ({ start, end }, culture, local) =>
local.format(start, 'LT', culture) + ' – ' + local.format(end, 'LT', culture)
const timeRangeStartFormat = ({ start }, culture, local) =>
local.format(start, 'LT', culture) + ' – '
const timeRangeEndFormat = ({ end }, culture, local) =>
' – ' + local.format(end, 'LT', culture)
export const formats = {
dateFormat: 'DD',
dayFormat: 'DD ddd',
weekdayFormat: 'ddd',
selectRangeFormat: timeRangeFormat,
eventTimeRangeFormat: timeRangeFormat,
eventTimeRangeStartFormat: timeRangeStartFormat,
eventTimeRangeEndFormat: timeRangeEndFormat,
timeGutterFormat: 'LT',
monthHeaderFormat: 'MMMM YYYY',
dayHeaderFormat: 'dddd MMM DD',
dayRangeHeaderFormat: weekRangeFormat,
agendaHeaderFormat: dateRangeFormat,
agendaDateFormat: 'ddd MMM DD',
agendaTimeFormat: 'LT',
agendaTimeRangeFormat: timeRangeFormat,
}
function fixUnit(unit) {
let datePart = unit ? unit.toLowerCase() : unit
if (datePart === 'FullYear') {
datePart = 'year'
} else if (!datePart) {
datePart = undefined
}
return datePart
}
export default function(moment) {
const locale = (m, c) => (c ? m.locale(c) : m)
/*** BEGIN localized date arithmetic methods with moment ***/
function defineComparators(a, b, unit) {
const datePart = fixUnit(unit)
const dtA = datePart ? moment(a).startOf(datePart) : moment(a)
const dtB = datePart ? moment(b).startOf(datePart) : moment(b)
return [dtA, dtB, datePart]
}
function startOf(date = null, unit) {
const datePart = fixUnit(unit)
if (datePart) {
return moment(date)
.startOf(datePart)
.toDate()
}
return moment(date).toDate()
}
function endOf(date = null, unit) {
const datePart = fixUnit(unit)
if (datePart) {
return moment(date)
.endOf(datePart)
.toDate()
}
return moment(date).toDate()
}
// moment comparison operations *always* convert both sides to moment objects
// prior to running the comparisons
function eq(a, b, unit) {
const [dtA, dtB, datePart] = defineComparators(a, b, unit)
return dtA.isSame(dtB, datePart)
}
function neq(a, b, unit) {
return !eq(a, b, unit)
}
function gt(a, b, unit) {
const [dtA, dtB, datePart] = defineComparators(a, b, unit)
return dtA.isAfter(dtB, datePart)
}
function lt(a, b, unit) {
const [dtA, dtB, datePart] = defineComparators(a, b, unit)
return dtA.isBefore(dtB, datePart)
}
function gte(a, b, unit) {
const [dtA, dtB, datePart] = defineComparators(a, b, unit)
return dtA.isSameOrBefore(dtB, datePart)
}
function lte(a, b, unit) {
const [dtA, dtB, datePart] = defineComparators(a, b, unit)
return dtA.isSameOrBefore(dtB, datePart)
}
function inRange(day, min, max, unit = 'day') {
const datePart = fixUnit(unit)
const mDay = moment(day)
const mMin = moment(min)
const mMax = moment(max)
return mDay.isBetween(mMin, mMax, datePart, '[]')
}
function min(dateA, dateB) {
const dtA = moment(dateA)
const dtB = moment(dateB)
const minDt = moment.min(dtA, dtB)
return minDt.toDate()
}
function max(dateA, dateB) {
const dtA = moment(dateA)
const dtB = moment(dateB)
const maxDt = moment.max(dtA, dtB)
return maxDt.toDate()
}
function merge(date, time) {
if (!date && !time) return null
const tm = moment(time).format('HH:mm:ss')
const dt = moment(date)
.startOf('day')
.format('MM/DD/YYYY')
// We do it this way to avoid issues when timezone switching
return moment(`${dt} ${tm}`, 'MM/DD/YYYY HH:mm:ss').toDate()
}
function add(date, adder, unit) {
const datePart = fixUnit(unit)
return moment(date)
.add(adder, datePart)
.toDate()
}
function range(start, end, unit = 'day') {
const datePart = fixUnit(unit)
// because the add method will put these in tz, we have to start that way
let current = moment(start).toDate()
const days = []
while (lte(current, end)) {
days.push(current)
current = add(current, 1, datePart)
}
return days
}
function ceil(date, unit) {
const datePart = fixUnit(unit)
const floor = startOf(date, datePart)
return eq(floor, date) ? floor : add(floor, 1, datePart)
}
function diff(a, b, unit = 'day') {
const datePart = fixUnit(unit)
// don't use 'defineComparators' here, as we don't want to mutate the values
const dtA = moment(a)
const dtB = moment(b)
return dtB.diff(dtA, datePart)
}
function minutes(date) {
const dt = moment(date)
return dt.minutes()
}
function firstOfWeek(culture) {
const data = culture ? moment.localeData(culture) : moment.localeData()
return data ? data.firstDayOfWeek() : 0
}
function firstVisibleDay(date) {
return moment(date)
.startOf('month')
.startOf('week')
.toDate()
}
function lastVisibleDay(date) {
return moment(date)
.endOf('month')
.endOf('week')
.toDate()
}
function visibleDays(date) {
let current = firstVisibleDay(date)
const last = lastVisibleDay(date)
const days = []
while (lte(current, last)) {
days.push(current)
current = add(current, 1, 'd')
}
return days
}
/*** END localized date arithmetic methods with moment ***/
/**
* Moved from TimeSlots.js, this method overrides the method of the same name
* in the localizer.js, using moment to construct the js Date
* @param {Date} dt - date to start with
* @param {Number} minutesFromMidnight
* @param {Number} offset
* @returns {Date}
*/
function getSlotDate(dt, minutesFromMidnight, offset) {
return moment(dt)
.startOf('day')
.minute(minutesFromMidnight + offset)
.toDate()
}
// moment will automatically handle DST differences in it's calculations
function getTotalMin(start, end) {
return diff(start, end, 'minutes')
}
function getMinutesFromMidnight(start) {
const dayStart = moment(start).startOf('day')
const day = moment(start)
return day.diff(dayStart, 'minutes')
}
// These two are used by DateSlotMetrics
function continuesPrior(start, first) {
const mStart = moment(start)
const mFirst = moment(first)
return mStart.isBefore(mFirst, 'day')
}
function continuesAfter(start, end, last) {
const mEnd = moment(end)
const mLast = moment(last)
return mEnd.isSameOrAfter(mLast, 'minutes')
}
// These two are used by eventLevels
function sortEvents({
evtA: { start: aStart, end: aEnd, allDay: aAllDay },
evtB: { start: bStart, end: bEnd, allDay: bAllDay },
}) {
const startSort = +startOf(aStart, 'day') - +startOf(bStart, 'day')
const durA = diff(aStart, ceil(aEnd, 'day'), 'day')
const durB = diff(bStart, ceil(bEnd, 'day'), 'day')
return (
startSort || // sort by start Day first
Math.max(durB, 1) - Math.max(durA, 1) || // events spanning multiple days go first
!!bAllDay - !!aAllDay || // then allDay single day events
+aStart - +bStart || // then sort by start time *don't need moment conversion here
+aEnd - +bEnd // then sort by end time *don't need moment conversion here either
)
}
function inEventRange({
event: { start, end },
range: { start: rangeStart, end: rangeEnd },
}) {
const startOfDay = moment(start).startOf('day')
const eEnd = moment(end)
const rStart = moment(rangeStart)
const rEnd = moment(rangeEnd)
const startsBeforeEnd = startOfDay.isSameOrBefore(rEnd, 'day')
// when the event is zero duration we need to handle a bit differently
const sameMin = !startOfDay.isSame(eEnd, 'minutes')
const endsAfterStart = sameMin
? eEnd.isAfter(rStart, 'minutes')
: eEnd.isSameOrAfter(rStart, 'minutes')
return startsBeforeEnd && endsAfterStart
}
// moment treats 'day' and 'date' equality very different
// moment(date1).isSame(date2, 'day') would test that they were both the same day of the week
// moment(date1).isSame(date2, 'date') would test that they were both the same date of the month of the year
function isSameDate(date1, date2) {
const dt = moment(date1)
const dt2 = moment(date2)
return dt.isSame(dt2, 'date')
}
/**
* This method, called once in the localizer constructor, is used by eventLevels
* 'eventSegments()' to assist in determining the 'span' of the event in the display,
* specifically when using a timezone that is greater than the browser native timezone.
* @returns number
*/
function browserTZOffset() {
/**
* Date.prototype.getTimezoneOffset horrifically flips the positive/negative from
* what you see in it's string, so we have to jump through some hoops to get a value
* we can actually compare.
*/
const dt = new Date()
const neg = /-/.test(dt.toString()) ? '-' : ''
const dtOffset = dt.getTimezoneOffset()
const comparator = Number(`${neg}${Math.abs(dtOffset)}`)
// moment correctly provides positive/negative offset, as expected
const mtOffset = moment().utcOffset()
return mtOffset > comparator ? 1 : 0
}
return new DateLocalizer({
formats,
firstOfWeek,
firstVisibleDay,
lastVisibleDay,
visibleDays,
format(value, format, culture) {
return locale(moment(value), culture).format(format)
},
lt,
lte,
gt,
gte,
eq,
neq,
merge,
inRange,
startOf,
endOf,
range,
add,
diff,
ceil,
min,
max,
minutes,
getSlotDate,
getTotalMin,
getMinutesFromMidnight,
continuesPrior,
continuesAfter,
sortEvents,
inEventRange,
isSameDate,
browserTZOffset,
})
}