2021-04-28 11:12:56 +00:00
|
|
|
import format from 'pretty-ms';
|
2022-04-12 08:56:54 +00:00
|
|
|
import parse from 'parse-duration';
|
2021-04-28 11:12:56 +00:00
|
|
|
import { assert } from '@ember/debug';
|
2022-04-12 08:56:54 +00:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
import relativeTime from 'dayjs/plugin/relativeTime';
|
2021-04-28 11:12:56 +00:00
|
|
|
import Service from '@ember/service';
|
|
|
|
|
2022-04-12 08:56:54 +00:00
|
|
|
dayjs.extend(relativeTime);
|
|
|
|
|
2021-04-28 11:12:56 +00:00
|
|
|
export default class TemporalService extends Service {
|
2022-04-12 08:56:54 +00:00
|
|
|
format(value, options) {
|
|
|
|
const djs = dayjs(value);
|
2022-09-15 08:43:17 +00:00
|
|
|
if (dayjs().isBefore(djs)) {
|
2022-04-12 08:56:54 +00:00
|
|
|
return dayjs().to(djs, true);
|
|
|
|
} else {
|
|
|
|
return dayjs().from(djs, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
within([value, d], options) {
|
|
|
|
return dayjs(value).isBefore(dayjs().add(d, 'ms'));
|
|
|
|
}
|
|
|
|
|
|
|
|
parse(value, options) {
|
|
|
|
return parse(value);
|
|
|
|
}
|
|
|
|
|
2021-04-28 11:12:56 +00:00
|
|
|
durationFrom(value, options = {}) {
|
|
|
|
switch (true) {
|
|
|
|
case typeof value === 'number':
|
|
|
|
// if its zero, don't format just return zero as a string
|
|
|
|
if (value === 0) {
|
|
|
|
return '0';
|
|
|
|
}
|
|
|
|
return format(value / 1000000, { formatSubMilliseconds: true })
|
|
|
|
.split(' ')
|
|
|
|
.join('');
|
2022-04-12 08:56:54 +00:00
|
|
|
case typeof value === 'string':
|
|
|
|
return value;
|
|
|
|
default:
|
|
|
|
assert(`${value} is not a valid type`, false);
|
|
|
|
return value;
|
2021-04-28 11:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|