1import { createLocal } from '../create/local';
2import { cloneWithOffset } from '../units/offset';
3import isFunction from '../utils/is-function';
4import { hooks } from '../utils/hooks';
5import { isMomentInput } from '../utils/is-moment-input';
6import isCalendarSpec from '../utils/is-calendar-spec';
7
8export function getCalendarFormat(myMoment, now) {
9 var diff = myMoment.diff(now, 'days', true);
10 return diff < -6
11 ? 'sameElse'
12 : diff < -1
13 ? 'lastWeek'
14 : diff < 0
15 ? 'lastDay'
16 : diff < 1
17 ? 'sameDay'
18 : diff < 2
19 ? 'nextDay'
20 : diff < 7
21 ? 'nextWeek'
22 : 'sameElse';
23}
24
25export function calendar(time, formats) {
26 // Support for single parameter, formats only overload to the calendar function
27 if (arguments.length === 1) {
28 if (!arguments[0]) {
29 time = undefined;
30 formats = undefined;
31 } else if (isMomentInput(arguments[0])) {
32 time = arguments[0];
33 formats = undefined;
34 } else if (isCalendarSpec(arguments[0])) {
35 formats = arguments[0];
36 time = undefined;
37 }
38 }
39 // We want to compare the start of today, vs this.
40 // Getting start-of-today depends on whether we're local/utc/offset or not.
41 var now = time || createLocal(),
42 sod = cloneWithOffset(now, this).startOf('day'),
43 format = hooks.calendarFormat(this, sod) || 'sameElse',
44 output =
45 formats &&
46 (isFunction(formats[format])
47 ? formats[format].call(this, now)
48 : formats[format]);
49
50 return this.format(
51 output || this.localeData().calendar(format, this, createLocal(now))
52 );
53}