350 lines
12 KiB
JavaScript
350 lines
12 KiB
JavaScript
/* eslint-disable class-methods-use-this */
|
|
import addDays from 'date-fns/addDays';
|
|
import addSeconds from 'date-fns/addSeconds';
|
|
import addMinutes from 'date-fns/addMinutes';
|
|
import addHours from 'date-fns/addHours';
|
|
import addWeeks from 'date-fns/addWeeks';
|
|
import addMonths from 'date-fns/addMonths';
|
|
import addYears from 'date-fns/addYears';
|
|
import differenceInYears from 'date-fns/differenceInYears';
|
|
import differenceInQuarters from 'date-fns/differenceInQuarters';
|
|
import differenceInMonths from 'date-fns/differenceInMonths';
|
|
import differenceInWeeks from 'date-fns/differenceInWeeks';
|
|
import differenceInDays from 'date-fns/differenceInDays';
|
|
import differenceInHours from 'date-fns/differenceInHours';
|
|
import differenceInMinutes from 'date-fns/differenceInMinutes';
|
|
import differenceInSeconds from 'date-fns/differenceInSeconds';
|
|
import differenceInMilliseconds from 'date-fns/differenceInMilliseconds';
|
|
import eachDayOfInterval from 'date-fns/eachDayOfInterval';
|
|
import endOfDay from 'date-fns/endOfDay';
|
|
import endOfWeek from 'date-fns/endOfWeek';
|
|
import endOfYear from 'date-fns/endOfYear';
|
|
import dateFnsFormat from 'date-fns/format';
|
|
import getDate from 'date-fns/getDate';
|
|
import getDaysInMonth from 'date-fns/getDaysInMonth';
|
|
import getHours from 'date-fns/getHours';
|
|
import getMinutes from 'date-fns/getMinutes';
|
|
import getMonth from 'date-fns/getMonth';
|
|
import getSeconds from 'date-fns/getSeconds';
|
|
import getMilliseconds from 'date-fns/getMilliseconds';
|
|
import getWeek from 'date-fns/getWeek';
|
|
import getYear from 'date-fns/getYear';
|
|
import isAfter from 'date-fns/isAfter';
|
|
import isBefore from 'date-fns/isBefore';
|
|
import isEqual from 'date-fns/isEqual';
|
|
import isSameDay from 'date-fns/isSameDay';
|
|
import isSameYear from 'date-fns/isSameYear';
|
|
import isSameMonth from 'date-fns/isSameMonth';
|
|
import isSameHour from 'date-fns/isSameHour';
|
|
import isValid from 'date-fns/isValid';
|
|
import dateFnsParse from 'date-fns/parse';
|
|
import setDate from 'date-fns/setDate';
|
|
import setHours from 'date-fns/setHours';
|
|
import setMinutes from 'date-fns/setMinutes';
|
|
import setMonth from 'date-fns/setMonth';
|
|
import setSeconds from 'date-fns/setSeconds';
|
|
import setMilliseconds from 'date-fns/setMilliseconds';
|
|
import setYear from 'date-fns/setYear';
|
|
import startOfDay from 'date-fns/startOfDay';
|
|
import startOfMonth from 'date-fns/startOfMonth';
|
|
import endOfMonth from 'date-fns/endOfMonth';
|
|
import startOfWeek from 'date-fns/startOfWeek';
|
|
import startOfYear from 'date-fns/startOfYear';
|
|
import parseISO from 'date-fns/parseISO';
|
|
import formatISO from 'date-fns/formatISO';
|
|
import isWithinInterval from 'date-fns/isWithinInterval';
|
|
import defaultLocale from 'date-fns/locale/en-US';
|
|
// @ts-ignore
|
|
import longFormatters from 'date-fns/_lib/format/longFormatters';
|
|
import { AdapterDateFnsBase } from '../AdapterDateFnsBase';
|
|
/**
|
|
* Based on `@date-io/date-fns`
|
|
*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2017 Dmitriy Kovalenko
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
export class AdapterDateFns extends AdapterDateFnsBase {
|
|
constructor({
|
|
locale,
|
|
formats
|
|
} = {}) {
|
|
if (typeof addDays !== 'function') {
|
|
throw new Error(['MUI: The `date-fns` package v3.x is not compatible with this adapter.', 'Please, install v2.x of the package or use the `AdapterDateFnsV3` instead.'].join('\n'));
|
|
}
|
|
super({
|
|
locale: locale != null ? locale : defaultLocale,
|
|
formats,
|
|
longFormatters
|
|
});
|
|
this.parseISO = isoString => {
|
|
return parseISO(isoString);
|
|
};
|
|
this.toISO = value => {
|
|
return formatISO(value, {
|
|
format: 'extended'
|
|
});
|
|
};
|
|
this.parse = (value, format) => {
|
|
if (value === '') {
|
|
return null;
|
|
}
|
|
return dateFnsParse(value, format, new Date(), {
|
|
locale: this.locale
|
|
});
|
|
};
|
|
this.isValid = value => {
|
|
return isValid(this.date(value));
|
|
};
|
|
this.format = (value, formatKey) => {
|
|
return this.formatByString(value, this.formats[formatKey]);
|
|
};
|
|
this.formatByString = (value, formatString) => {
|
|
return dateFnsFormat(value, formatString, {
|
|
locale: this.locale
|
|
});
|
|
};
|
|
this.getDiff = (value, comparing, unit) => {
|
|
switch (unit) {
|
|
case 'years':
|
|
return differenceInYears(value, this.date(comparing));
|
|
case 'quarters':
|
|
return differenceInQuarters(value, this.date(comparing));
|
|
case 'months':
|
|
return differenceInMonths(value, this.date(comparing));
|
|
case 'weeks':
|
|
return differenceInWeeks(value, this.date(comparing));
|
|
case 'days':
|
|
return differenceInDays(value, this.date(comparing));
|
|
case 'hours':
|
|
return differenceInHours(value, this.date(comparing));
|
|
case 'minutes':
|
|
return differenceInMinutes(value, this.date(comparing));
|
|
case 'seconds':
|
|
return differenceInSeconds(value, this.date(comparing));
|
|
default:
|
|
{
|
|
return differenceInMilliseconds(value, this.date(comparing));
|
|
}
|
|
}
|
|
};
|
|
this.isEqual = (value, comparing) => {
|
|
if (value === null && comparing === null) {
|
|
return true;
|
|
}
|
|
return isEqual(value, comparing);
|
|
};
|
|
this.isSameYear = (value, comparing) => {
|
|
return isSameYear(value, comparing);
|
|
};
|
|
this.isSameMonth = (value, comparing) => {
|
|
return isSameMonth(value, comparing);
|
|
};
|
|
this.isSameDay = (value, comparing) => {
|
|
return isSameDay(value, comparing);
|
|
};
|
|
this.isSameHour = (value, comparing) => {
|
|
return isSameHour(value, comparing);
|
|
};
|
|
this.isAfter = (value, comparing) => {
|
|
return isAfter(value, comparing);
|
|
};
|
|
this.isAfterYear = (value, comparing) => {
|
|
return isAfter(value, endOfYear(comparing));
|
|
};
|
|
this.isAfterDay = (value, comparing) => {
|
|
return isAfter(value, endOfDay(comparing));
|
|
};
|
|
this.isBefore = (value, comparing) => {
|
|
return isBefore(value, comparing);
|
|
};
|
|
this.isBeforeYear = (value, comparing) => {
|
|
return isBefore(value, startOfYear(comparing));
|
|
};
|
|
this.isBeforeDay = (value, comparing) => {
|
|
return isBefore(value, startOfDay(comparing));
|
|
};
|
|
this.isWithinRange = (value, [start, end]) => {
|
|
return isWithinInterval(value, {
|
|
start,
|
|
end
|
|
});
|
|
};
|
|
this.startOfYear = value => {
|
|
return startOfYear(value);
|
|
};
|
|
this.startOfMonth = value => {
|
|
return startOfMonth(value);
|
|
};
|
|
this.startOfWeek = value => {
|
|
return startOfWeek(value, {
|
|
locale: this.locale
|
|
});
|
|
};
|
|
this.startOfDay = value => {
|
|
return startOfDay(value);
|
|
};
|
|
this.endOfYear = value => {
|
|
return endOfYear(value);
|
|
};
|
|
this.endOfMonth = value => {
|
|
return endOfMonth(value);
|
|
};
|
|
this.endOfWeek = value => {
|
|
return endOfWeek(value, {
|
|
locale: this.locale
|
|
});
|
|
};
|
|
this.endOfDay = value => {
|
|
return endOfDay(value);
|
|
};
|
|
this.addYears = (value, amount) => {
|
|
return addYears(value, amount);
|
|
};
|
|
this.addMonths = (value, amount) => {
|
|
return addMonths(value, amount);
|
|
};
|
|
this.addWeeks = (value, amount) => {
|
|
return addWeeks(value, amount);
|
|
};
|
|
this.addDays = (value, amount) => {
|
|
return addDays(value, amount);
|
|
};
|
|
this.addHours = (value, amount) => {
|
|
return addHours(value, amount);
|
|
};
|
|
this.addMinutes = (value, amount) => {
|
|
return addMinutes(value, amount);
|
|
};
|
|
this.addSeconds = (value, amount) => {
|
|
return addSeconds(value, amount);
|
|
};
|
|
this.getYear = value => {
|
|
return getYear(value);
|
|
};
|
|
this.getMonth = value => {
|
|
return getMonth(value);
|
|
};
|
|
this.getDate = value => {
|
|
return getDate(value);
|
|
};
|
|
this.getHours = value => {
|
|
return getHours(value);
|
|
};
|
|
this.getMinutes = value => {
|
|
return getMinutes(value);
|
|
};
|
|
this.getSeconds = value => {
|
|
return getSeconds(value);
|
|
};
|
|
this.getMilliseconds = value => {
|
|
return getMilliseconds(value);
|
|
};
|
|
this.setYear = (value, year) => {
|
|
return setYear(value, year);
|
|
};
|
|
this.setMonth = (value, month) => {
|
|
return setMonth(value, month);
|
|
};
|
|
this.setDate = (value, date) => {
|
|
return setDate(value, date);
|
|
};
|
|
this.setHours = (value, hours) => {
|
|
return setHours(value, hours);
|
|
};
|
|
this.setMinutes = (value, minutes) => {
|
|
return setMinutes(value, minutes);
|
|
};
|
|
this.setSeconds = (value, seconds) => {
|
|
return setSeconds(value, seconds);
|
|
};
|
|
this.setMilliseconds = (value, milliseconds) => {
|
|
return setMilliseconds(value, milliseconds);
|
|
};
|
|
this.getDaysInMonth = value => {
|
|
return getDaysInMonth(value);
|
|
};
|
|
this.getNextMonth = value => {
|
|
return addMonths(value, 1);
|
|
};
|
|
this.getPreviousMonth = value => {
|
|
return addMonths(value, -1);
|
|
};
|
|
this.getMonthArray = value => {
|
|
const firstMonth = startOfYear(value);
|
|
const monthArray = [firstMonth];
|
|
while (monthArray.length < 12) {
|
|
const prevMonth = monthArray[monthArray.length - 1];
|
|
monthArray.push(this.getNextMonth(prevMonth));
|
|
}
|
|
return monthArray;
|
|
};
|
|
this.mergeDateAndTime = (dateParam, timeParam) => {
|
|
return this.setSeconds(this.setMinutes(this.setHours(dateParam, this.getHours(timeParam)), this.getMinutes(timeParam)), this.getSeconds(timeParam));
|
|
};
|
|
this.getWeekdays = () => {
|
|
const now = new Date();
|
|
return eachDayOfInterval({
|
|
start: startOfWeek(now, {
|
|
locale: this.locale
|
|
}),
|
|
end: endOfWeek(now, {
|
|
locale: this.locale
|
|
})
|
|
}).map(day => this.formatByString(day, 'EEEEEE'));
|
|
};
|
|
this.getWeekArray = value => {
|
|
const start = startOfWeek(startOfMonth(value), {
|
|
locale: this.locale
|
|
});
|
|
const end = endOfWeek(endOfMonth(value), {
|
|
locale: this.locale
|
|
});
|
|
let count = 0;
|
|
let current = start;
|
|
const nestedWeeks = [];
|
|
while (isBefore(current, end)) {
|
|
const weekNumber = Math.floor(count / 7);
|
|
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];
|
|
nestedWeeks[weekNumber].push(current);
|
|
current = addDays(current, 1);
|
|
count += 1;
|
|
}
|
|
return nestedWeeks;
|
|
};
|
|
this.getWeekNumber = value => {
|
|
return getWeek(value, {
|
|
locale: this.locale
|
|
});
|
|
};
|
|
this.getYearRange = (start, end) => {
|
|
const startDate = startOfYear(start);
|
|
const endDate = endOfYear(end);
|
|
const years = [];
|
|
let current = startDate;
|
|
while (isBefore(current, endDate)) {
|
|
years.push(current);
|
|
current = addYears(current, 1);
|
|
}
|
|
return years;
|
|
};
|
|
}
|
|
} |