timerender: Extract new variant of get_full_datetime.

Separating these concepts allows us to provide a much nicer format for
contexts where ultra-specific clarification is not a priority.

This new variant is currently only used in the scheduled messages UI.
This commit is contained in:
Tim Abbott 2023-04-30 22:02:10 -07:00
parent 341f3a1ce2
commit 6fe02e933a
6 changed files with 28 additions and 6 deletions

View File

@ -189,6 +189,8 @@ function valid_to(expires_in) {
if (!time_valid) { if (!time_valid) {
return $t({defaultMessage: "Never expires"}); return $t({defaultMessage: "Never expires"});
} }
// The below is a duplicate of timerender.get_full_datetime, with a different base string.
const valid_to = add(new Date(), {minutes: time_valid}); const valid_to = add(new Date(), {minutes: time_valid});
const date = timerender.get_localized_date_or_time_for_format(valid_to, "dayofyear_year"); const date = timerender.get_localized_date_or_time_for_format(valid_to, "dayofyear_year");
const time = timerender.get_localized_date_or_time_for_format(valid_to, "time"); const time = timerender.get_localized_date_or_time_for_format(valid_to, "time");

View File

@ -360,7 +360,7 @@ function format_conversation(conversation_data) {
const last_msg = message_store.get(conversation_data.last_msg_id); const last_msg = message_store.get(conversation_data.last_msg_id);
const time = new Date(last_msg.timestamp * 1000); const time = new Date(last_msg.timestamp * 1000);
const type = last_msg.type; const type = last_msg.type;
context.full_last_msg_date_time = timerender.get_full_datetime(time); context.full_last_msg_date_time = timerender.get_full_datetime_clarification(time);
context.conversation_key = get_key_from_message(last_msg); context.conversation_key = get_key_from_message(last_msg);
context.unread_count = message_to_conversation_unread_count(last_msg); context.unread_count = message_to_conversation_unread_count(last_msg);
context.last_msg_time = timerender.relative_time_string_from_date(time); context.last_msg_time = timerender.relative_time_string_from_date(time);

View File

@ -446,6 +446,18 @@ export function absolute_time(timestamp: number, today = new Date()): string {
// Pass time_format="time" to not include seconds in the time format. // Pass time_format="time" to not include seconds in the time format.
export function get_full_datetime(time: Date, time_format: TimeFormat = "time_sec"): string { export function get_full_datetime(time: Date, time_format: TimeFormat = "time_sec"): string {
const date_string = get_localized_date_or_time_for_format(time, "dayofyear_year");
const time_string = get_localized_date_or_time_for_format(time, time_format);
return $t({defaultMessage: "{date} at {time}"}, {date: date_string, time: time_string});
}
// Preferred variant for displaying a full datetime to users in
// contexts like tooltips, where the time was already displayed to the
// user in a less precise format.
export function get_full_datetime_clarification(
time: Date,
time_format: TimeFormat = "time_sec",
): string {
const locale = get_user_locale(); const locale = get_user_locale();
const date_string = time.toLocaleDateString(locale); const date_string = time.toLocaleDateString(locale);
let time_string = get_localized_date_or_time_for_format(time, time_format); let time_string = get_localized_date_or_time_for_format(time, time_format);

View File

@ -371,7 +371,7 @@ export function initialize() {
return false; return false;
} }
const time = new Date(message.timestamp * 1000); const time = new Date(message.timestamp * 1000);
instance.setContent(timerender.get_full_datetime(time)); instance.setContent(timerender.get_full_datetime_clarification(time));
return true; return true;
}, },
onHidden(instance) { onHidden(instance) {

View File

@ -138,7 +138,7 @@ mock_esm("../src/stream_list", {
}); });
mock_esm("../src/timerender", { mock_esm("../src/timerender", {
relative_time_string_from_date: () => "Just now", relative_time_string_from_date: () => "Just now",
get_full_datetime: () => "date at time", get_full_datetime_clarification: () => "date at time",
}); });
mock_esm("../src/sub_store", { mock_esm("../src/sub_store", {
get(stream) { get(stream) {

View File

@ -288,7 +288,7 @@ run_test("format_time_modern_different_timezones", () => {
process.env.TZ = "America/Juneau"; process.env.TZ = "America/Juneau";
let expected = "translated: 5/16/2017 at 11:12:53 PM AKDT (UTC-08:00)"; let expected = "translated: 5/16/2017 at 11:12:53 PM AKDT (UTC-08:00)";
assert.equal(timerender.get_full_datetime(yesterday), expected); assert.equal(timerender.get_full_datetime_clarification(yesterday), expected);
assert.equal(timerender.format_time_modern(yesterday, today), "Tuesday"); assert.equal(timerender.format_time_modern(yesterday, today), "Tuesday");
process.env.TZ = utc_tz; process.env.TZ = utc_tz;
@ -299,7 +299,7 @@ run_test("format_time_modern_different_timezones", () => {
process.env.TZ = "Asia/Brunei"; process.env.TZ = "Asia/Brunei";
expected = "translated: 5/17/2017 at 5:12:53 AM (UTC+08:00)"; expected = "translated: 5/17/2017 at 5:12:53 AM (UTC+08:00)";
assert.equal(timerender.get_full_datetime(yesterday), expected); assert.equal(timerender.get_full_datetime_clarification(yesterday), expected);
assert.equal(timerender.format_time_modern(yesterday, today), "translated: Yesterday"); assert.equal(timerender.format_time_modern(yesterday, today), "translated: Yesterday");
process.env.TZ = utc_tz; process.env.TZ = utc_tz;
@ -310,7 +310,7 @@ run_test("format_time_modern_different_timezones", () => {
process.env.TZ = "America/Juneau"; process.env.TZ = "America/Juneau";
expected = "translated: 5/11/2017 at 11:12:53 PM AKDT (UTC-08:00)"; expected = "translated: 5/11/2017 at 11:12:53 PM AKDT (UTC-08:00)";
assert.equal(timerender.get_full_datetime(yesterday), expected); assert.equal(timerender.get_full_datetime_clarification(yesterday), expected);
assert.equal(timerender.format_time_modern(yesterday, today), "May 11"); assert.equal(timerender.format_time_modern(yesterday, today), "May 11");
process.env.TZ = utc_tz; process.env.TZ = utc_tz;
}); });
@ -446,14 +446,20 @@ run_test("get_full_datetime", () => {
const time = date_2017_PM; const time = date_2017_PM;
let expected = "translated: 5/18/2017 at 9:12:53 PM UTC"; let expected = "translated: 5/18/2017 at 9:12:53 PM UTC";
assert.equal(timerender.get_full_datetime_clarification(time), expected);
expected = "translated: May 18, 2017 at 9:12:53 PM";
assert.equal(timerender.get_full_datetime(time), expected); assert.equal(timerender.get_full_datetime(time), expected);
expected = "translated: 5/18/2017 at 9:12 PM UTC"; expected = "translated: 5/18/2017 at 9:12 PM UTC";
assert.equal(timerender.get_full_datetime_clarification(time, "time"), expected);
expected = "translated: May 18, 2017 at 9:12 PM";
assert.equal(timerender.get_full_datetime(time, "time"), expected); assert.equal(timerender.get_full_datetime(time, "time"), expected);
// test 24 hour time setting. // test 24 hour time setting.
user_settings.twenty_four_hour_time = true; user_settings.twenty_four_hour_time = true;
expected = "translated: 5/18/2017 at 21:12:53 UTC"; expected = "translated: 5/18/2017 at 21:12:53 UTC";
assert.equal(timerender.get_full_datetime_clarification(time), expected);
expected = "translated: May 18, 2017 at 21:12:53";
assert.equal(timerender.get_full_datetime(time), expected); assert.equal(timerender.get_full_datetime(time), expected);
user_settings.twenty_four_hour_time = false; user_settings.twenty_four_hour_time = false;
@ -462,6 +468,8 @@ run_test("get_full_datetime", () => {
const previous_env_tz = process.env.TZ; const previous_env_tz = process.env.TZ;
process.env.TZ = "Asia/Kolkata"; process.env.TZ = "Asia/Kolkata";
expected = "translated: 5/19/2017 at 2:42:53 AM (UTC+05:30)"; expected = "translated: 5/19/2017 at 2:42:53 AM (UTC+05:30)";
assert.equal(timerender.get_full_datetime_clarification(time), expected);
expected = "translated: May 19, 2017 at 2:42:53 AM";
assert.equal(timerender.get_full_datetime(time), expected); assert.equal(timerender.get_full_datetime(time), expected);
process.env.TZ = previous_env_tz; process.env.TZ = previous_env_tz;
}); });