mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-17 17:14:18 +02:00
New: Project Aphrodite
This commit is contained in:
33
frontend/src/Utilities/Date/dateFilterPredicate.js
Normal file
33
frontend/src/Utilities/Date/dateFilterPredicate.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import moment from 'moment';
|
||||
import isAfter from 'Utilities/Date/isAfter';
|
||||
import isBefore from 'Utilities/Date/isBefore';
|
||||
import * as filterTypes from 'Helpers/Props/filterTypes';
|
||||
|
||||
export default function(itemValue, filterValue, type) {
|
||||
if (!itemValue) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case filterTypes.LESS_THAN:
|
||||
return moment(itemValue).isBefore(filterValue);
|
||||
|
||||
case filterTypes.GREATER_THAN:
|
||||
return moment(itemValue).isAfter(filterValue);
|
||||
|
||||
case filterTypes.IN_LAST:
|
||||
return (
|
||||
isAfter(itemValue, { [filterValue.time]: filterValue.value * -1 }) &&
|
||||
isBefore(itemValue)
|
||||
);
|
||||
|
||||
case filterTypes.IN_NEXT:
|
||||
return (
|
||||
isAfter(itemValue) &&
|
||||
isBefore(itemValue, { [filterValue.time]: filterValue.value })
|
||||
);
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
11
frontend/src/Utilities/Date/formatDate.js
Normal file
11
frontend/src/Utilities/Date/formatDate.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import moment from 'moment';
|
||||
|
||||
function formatDate(date, dateFormat) {
|
||||
if (!date) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return moment(date).format(dateFormat);
|
||||
}
|
||||
|
||||
export default formatDate;
|
39
frontend/src/Utilities/Date/formatDateTime.js
Normal file
39
frontend/src/Utilities/Date/formatDateTime.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import moment from 'moment';
|
||||
import formatTime from './formatTime';
|
||||
import isToday from './isToday';
|
||||
import isTomorrow from './isTomorrow';
|
||||
import isYesterday from './isYesterday';
|
||||
|
||||
function getRelativeDay(date, includeRelativeDate) {
|
||||
if (!includeRelativeDate) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (isYesterday(date)) {
|
||||
return 'Yesterday, ';
|
||||
}
|
||||
|
||||
if (isToday(date)) {
|
||||
return 'Today, ';
|
||||
}
|
||||
|
||||
if (isTomorrow(date)) {
|
||||
return 'Tomorrow, ';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
function formatDateTime(date, dateFormat, timeFormat, { includeSeconds = false, includeRelativeDay = false } = {}) {
|
||||
if (!date) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const relativeDay = getRelativeDay(date, includeRelativeDay);
|
||||
const formattedDate = moment(date).format(dateFormat);
|
||||
const formattedTime = formatTime(date, timeFormat, { includeMinuteZero: true, includeSeconds });
|
||||
|
||||
return `${relativeDay}${formattedDate} ${formattedTime}`;
|
||||
}
|
||||
|
||||
export default formatDateTime;
|
19
frontend/src/Utilities/Date/formatTime.js
Normal file
19
frontend/src/Utilities/Date/formatTime.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import moment from 'moment';
|
||||
|
||||
function formatTime(date, timeFormat, { includeMinuteZero = false, includeSeconds = false } = {}) {
|
||||
if (!date) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (includeSeconds) {
|
||||
timeFormat = timeFormat.replace(/\(?:mm\)?/, ':mm:ss');
|
||||
} else if (includeMinuteZero) {
|
||||
timeFormat = timeFormat.replace('(:mm)', ':mm');
|
||||
} else {
|
||||
timeFormat = timeFormat.replace('(:mm)', '');
|
||||
}
|
||||
|
||||
return moment(date).format(timeFormat);
|
||||
}
|
||||
|
||||
export default formatTime;
|
24
frontend/src/Utilities/Date/formatTimeSpan.js
Normal file
24
frontend/src/Utilities/Date/formatTimeSpan.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import moment from 'moment';
|
||||
import padNumber from 'Utilities/Number/padNumber';
|
||||
|
||||
function formatTimeSpan(timeSpan) {
|
||||
if (!timeSpan) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const duration = moment.duration(timeSpan);
|
||||
const days = duration.get('days');
|
||||
const hours = padNumber(duration.get('hours'), 2);
|
||||
const minutes = padNumber(duration.get('minutes'), 2);
|
||||
const seconds = padNumber(duration.get('seconds'), 2);
|
||||
|
||||
const time = `${hours}:${minutes}:${seconds}`;
|
||||
|
||||
if (days > 0) {
|
||||
return `${days}d ${time}`;
|
||||
}
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
export default formatTimeSpan;
|
42
frontend/src/Utilities/Date/getRelativeDate.js
Normal file
42
frontend/src/Utilities/Date/getRelativeDate.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import moment from 'moment';
|
||||
import formatTime from 'Utilities/Date/formatTime';
|
||||
import isInNextWeek from 'Utilities/Date/isInNextWeek';
|
||||
import isToday from 'Utilities/Date/isToday';
|
||||
import isTomorrow from 'Utilities/Date/isTomorrow';
|
||||
import isYesterday from 'Utilities/Date/isYesterday';
|
||||
|
||||
function getRelativeDate(date, shortDateFormat, showRelativeDates, { timeFormat, includeSeconds = false, timeForToday = false } = {}) {
|
||||
if (!date) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const isTodayDate = isToday(date);
|
||||
|
||||
if (isTodayDate && timeForToday && timeFormat) {
|
||||
return formatTime(date, timeFormat, { includeMinuteZero: true, includeSeconds });
|
||||
}
|
||||
|
||||
if (!showRelativeDates) {
|
||||
return moment(date).format(shortDateFormat);
|
||||
}
|
||||
|
||||
if (isYesterday(date)) {
|
||||
return 'Yesterday';
|
||||
}
|
||||
|
||||
if (isTodayDate) {
|
||||
return 'Today';
|
||||
}
|
||||
|
||||
if (isTomorrow(date)) {
|
||||
return 'Tomorrow';
|
||||
}
|
||||
|
||||
if (isInNextWeek(date)) {
|
||||
return moment(date).format('dddd');
|
||||
}
|
||||
|
||||
return moment(date).format(shortDateFormat);
|
||||
}
|
||||
|
||||
export default getRelativeDate;
|
17
frontend/src/Utilities/Date/isAfter.js
Normal file
17
frontend/src/Utilities/Date/isAfter.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import moment from 'moment';
|
||||
|
||||
function isAfter(date, offsets = {}) {
|
||||
if (!date) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const offsetTime = moment();
|
||||
|
||||
Object.keys(offsets).forEach((key) => {
|
||||
offsetTime.add(offsets[key], key);
|
||||
});
|
||||
|
||||
return moment(date).isAfter(offsetTime);
|
||||
}
|
||||
|
||||
export default isAfter;
|
17
frontend/src/Utilities/Date/isBefore.js
Normal file
17
frontend/src/Utilities/Date/isBefore.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import moment from 'moment';
|
||||
|
||||
function isBefore(date, offsets = {}) {
|
||||
if (!date) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const offsetTime = moment();
|
||||
|
||||
Object.keys(offsets).forEach((key) => {
|
||||
offsetTime.add(offsets[key], key);
|
||||
});
|
||||
|
||||
return moment(date).isBefore(offsetTime);
|
||||
}
|
||||
|
||||
export default isBefore;
|
11
frontend/src/Utilities/Date/isInNextWeek.js
Normal file
11
frontend/src/Utilities/Date/isInNextWeek.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import moment from 'moment';
|
||||
|
||||
function isInNextWeek(date) {
|
||||
if (!date) {
|
||||
return false;
|
||||
}
|
||||
const now = moment();
|
||||
return moment(date).isBetween(now, now.clone().add(6, 'days').endOf('day'));
|
||||
}
|
||||
|
||||
export default isInNextWeek;
|
11
frontend/src/Utilities/Date/isSameWeek.js
Normal file
11
frontend/src/Utilities/Date/isSameWeek.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import moment from 'moment';
|
||||
|
||||
function isSameWeek(date) {
|
||||
if (!date) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return moment(date).isSame(moment(), 'week');
|
||||
}
|
||||
|
||||
export default isSameWeek;
|
11
frontend/src/Utilities/Date/isToday.js
Normal file
11
frontend/src/Utilities/Date/isToday.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import moment from 'moment';
|
||||
|
||||
function isToday(date) {
|
||||
if (!date) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return moment(date).isSame(moment(), 'day');
|
||||
}
|
||||
|
||||
export default isToday;
|
11
frontend/src/Utilities/Date/isTomorrow.js
Normal file
11
frontend/src/Utilities/Date/isTomorrow.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import moment from 'moment';
|
||||
|
||||
function isTomorrow(date) {
|
||||
if (!date) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return moment(date).isSame(moment().add(1, 'day'), 'day');
|
||||
}
|
||||
|
||||
export default isTomorrow;
|
11
frontend/src/Utilities/Date/isYesterday.js
Normal file
11
frontend/src/Utilities/Date/isYesterday.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import moment from 'moment';
|
||||
|
||||
function isYesterday(date) {
|
||||
if (!date) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return moment(date).isSame(moment().subtract(1, 'day'), 'day');
|
||||
}
|
||||
|
||||
export default isYesterday;
|
Reference in New Issue
Block a user