2017-09-07 14:17:36 +03:00
|
|
|
import Helper from '@ember/component/helper';
|
2022-09-23 20:15:08 +03:00
|
|
|
import moment from 'moment-timezone';
|
2017-09-07 14:17:36 +03:00
|
|
|
import {assert} from '@ember/debug';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-09-07 14:17:36 +03:00
|
|
|
|
2022-09-21 13:13:49 +03:00
|
|
|
export function formatPostTime(timeago, {timezone = 'etc/UTC', format, draft, scheduled, published}) {
|
2020-07-01 15:13:28 +03:00
|
|
|
if (draft) {
|
|
|
|
// No special handling for drafts, just use moment.from
|
|
|
|
return moment(timeago).from(moment.utc());
|
|
|
|
}
|
|
|
|
|
|
|
|
let time = moment.tz(timeago, timezone);
|
2022-09-21 13:13:49 +03:00
|
|
|
|
|
|
|
if (format) {
|
|
|
|
return time.format(format);
|
|
|
|
}
|
|
|
|
|
2020-07-01 15:13:28 +03:00
|
|
|
let now = moment.tz(moment.utc(), timezone);
|
|
|
|
|
2020-07-01 20:57:36 +03:00
|
|
|
let utcOffset;
|
|
|
|
if (time.utcOffset() === 0) {
|
|
|
|
utcOffset = '(UTC)';
|
|
|
|
} else {
|
|
|
|
utcOffset = `(UTC${time.format('Z').replace(/([+-])0/, '$1').replace(/:00/, '')})`;
|
|
|
|
}
|
|
|
|
|
2020-07-01 15:13:28 +03:00
|
|
|
// If not a draft and post was published <= 12 hours ago
|
|
|
|
// or scheduled to be published <= 12 hours from now, use moment.from
|
|
|
|
if (Math.abs(now.diff(time, 'hours')) <= 12) {
|
|
|
|
return time.from(now);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If scheduled for or published on the same day, render the time + Today
|
|
|
|
if (time.isSame(now, 'day')) {
|
2020-07-01 20:57:36 +03:00
|
|
|
let formatted = time.format(`HH:mm [${utcOffset}] [Today]`);
|
2020-07-01 15:13:28 +03:00
|
|
|
return scheduled ? `at ${formatted}` : formatted;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if published yesterday, render time + yesterday
|
|
|
|
// This check comes before scheduled, because there are likely to be more published
|
|
|
|
// posts than scheduled posts.
|
|
|
|
if (published && time.isSame(now.clone().subtract(1, 'days').startOf('day'), 'day')) {
|
2020-07-01 20:57:36 +03:00
|
|
|
return time.format(`HH:mm [${utcOffset}] [Yesterday]`);
|
2020-07-01 15:13:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-11 11:49:07 +03:00
|
|
|
// if scheduled for tomorrow, render the time + tomorrow
|
2020-07-01 15:13:28 +03:00
|
|
|
if (scheduled && time.isSame(now.clone().add(1, 'days').startOf('day'), 'day')) {
|
2020-12-11 11:49:07 +03:00
|
|
|
return time.format(`[at] HH:mm [${utcOffset}] [tomorrow]`);
|
2020-07-01 15:13:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Else, render just the date if published, or the time & date if scheduled
|
2022-09-21 13:13:49 +03:00
|
|
|
let f = scheduled ? `[at] HH:mm [${utcOffset}] [on] DD MMM YYYY` : 'DD MMM YYYY';
|
|
|
|
return time.format(f);
|
2020-07-01 15:13:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class GhFormatPostTimeHelper extends Helper {
|
|
|
|
@service settings;
|
2017-09-07 14:17:36 +03:00
|
|
|
|
2020-07-01 15:13:28 +03:00
|
|
|
compute([timeago], options) {
|
2019-03-05 20:34:00 +03:00
|
|
|
assert('You must pass a time to the gh-format-post-time helper', timeago);
|
2017-09-07 14:17:36 +03:00
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
return formatPostTime(timeago, Object.assign({}, options, {timezone: this.settings.timezone}));
|
2017-09-07 14:17:36 +03:00
|
|
|
}
|
2020-07-01 15:13:28 +03:00
|
|
|
}
|