FEATURE: Show relative time when date is omitted (#18547)

This commit is contained in:
Natalie Tay 2022-10-12 11:23:34 +08:00 committed by GitHub
parent 84f2529138
commit 1631394826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 10 deletions

View File

@ -67,12 +67,12 @@ export default class LocalDateBuilder {
}
const previews = this._generatePreviews(localDate, displayedTimezone);
const hasTime = hour !== undefined;
return {
pastEvent:
!this.recurring &&
moment.tz(this.localTimezone).isAfter(localDate.datetime),
formatted: this._applyFormatting(localDate, displayedTimezone),
formatted: this._applyFormatting(localDate, displayedTimezone, hasTime),
previews,
textPreview: this._generateTextPreviews(previews),
};
@ -210,7 +210,7 @@ export default class LocalDateBuilder {
return duration < 0 ? dates.reverse() : dates;
}
_applyFormatting(localDate, displayedTimezone) {
_applyFormatting(localDate, displayedTimezone, hasTime) {
if (this.countdown) {
const diffTime = moment.tz(this.localTimezone).diff(localDate.datetime);
@ -241,7 +241,7 @@ export default class LocalDateBuilder {
if (inCalendarRange && sameTimezone) {
const date = localDate.datetimeWithZone(this.localTimezone);
if (date.hours() === 0 && date.minutes() === 0) {
if (hasTime && date.hours() === 0 && date.minutes() === 0) {
return date.format("dddd");
}

View File

@ -1,6 +1,6 @@
import { module, test } from "qunit";
import { applyLocalDates } from "../initializers/discourse-local-dates";
import { freezeTime } from "../lib/local-date-builder-test";
import { freezeTime } from "./local-date-builder-test";
module("Unit | Discourse Local Dates | discourse-local-dates", function () {
function createElementFromHTML(htmlString) {
@ -72,7 +72,7 @@ module("Unit | Discourse Local Dates | discourse-local-dates", function () {
);
assert.equal(
to.querySelector(".relative-time").textContent,
"Thursday"
"Yesterday"
);
}
);

View File

@ -1,5 +1,5 @@
import I18n from "I18n";
import LocalDateBuilder from "./local-date-builder";
import LocalDateBuilder from "../lib/local-date-builder";
import sinon from "sinon";
import QUnit, { module, test } from "qunit";
@ -10,6 +10,7 @@ const NEW_YORK = "America/New_York";
const PARIS = "Europe/Paris";
const LAGOS = "Africa/Lagos";
const LONDON = "Europe/London";
const SINGAPORE = "Asia/Singapore";
export function freezeTime({ date, timezone }, cb) {
date = date || "2020-01-22 10:34";
@ -61,7 +62,7 @@ QUnit.assert.buildsCorrectDate = function (options, expected, message) {
}
};
module("lib:local-date-builder", function () {
module("Unit | Library | local-date-builder", function () {
test("date", function (assert) {
freezeTime({ date: "2020-03-11" }, () => {
assert.buildsCorrectDate(
@ -70,6 +71,23 @@ module("lib:local-date-builder", function () {
"it displays the date without time"
);
});
freezeTime({ date: "2022-10-11", timezone: "Asia/Singapore" }, () => {
const localDateBuilder = new LocalDateBuilder(
{
date: "2022-10-12",
timezone: SINGAPORE,
localTimezone: SINGAPORE,
},
SINGAPORE
);
assert.strictEqual(
localDateBuilder.build().formatted,
"Tomorrow",
"Displays relative day"
);
});
});
test("date and time", function (assert) {
@ -91,8 +109,8 @@ module("lib:local-date-builder", function () {
{
time: "12:22:00",
date: "2022-10-07",
timezone: "Asia/Singapore",
localTimezone: "Asia/Singapore",
timezone: SINGAPORE,
localTimezone: SINGAPORE,
sameLocalDayAsFrom: true,
},
{ formatted: "12:22 PM (Singapore)" },