2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-10-10 12:56:00 +00:00
|
|
|
import Helper from '@ember/component/helper';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
|
|
|
|
const MILLISECONDS_IN_DAY = 1000 * 60 * 60 * 24;
|
|
|
|
const MILLISECONDS_IN_WEEK = MILLISECONDS_IN_DAY * 7;
|
|
|
|
/**
|
|
|
|
* A function that returns if a date is within a week of the current time
|
|
|
|
* @param {*} date - the date to check
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function isNearDate(date) {
|
|
|
|
const now = new Date();
|
2022-10-11 14:51:55 +00:00
|
|
|
const aWeekAgo = +now - MILLISECONDS_IN_WEEK;
|
|
|
|
const aWeekInFuture = +now + MILLISECONDS_IN_WEEK;
|
2022-10-10 12:56:00 +00:00
|
|
|
|
2022-10-11 14:51:55 +00:00
|
|
|
return date >= aWeekAgo && date <= aWeekInFuture;
|
2022-10-10 12:56:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class SmartDateFormat extends Helper {
|
|
|
|
@service temporal;
|
|
|
|
@service intl;
|
|
|
|
|
|
|
|
compute([value], hash) {
|
|
|
|
return {
|
|
|
|
isNearDate: isNearDate(value),
|
|
|
|
relative: `${this.temporal.format(value)} ago`,
|
|
|
|
friendly: this.intl.formatTime(value, {
|
|
|
|
month: 'short',
|
|
|
|
day: 'numeric',
|
|
|
|
year: 'numeric',
|
|
|
|
hour: 'numeric',
|
|
|
|
minute: 'numeric',
|
|
|
|
second: 'numeric',
|
|
|
|
hourCycle: 'h24',
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|