refactor: Remove today arg from absolute_time.

The today argument was only used in tests and now we're using
MockDate to set the current date.
This commit is contained in:
Shubham Padia
2024-01-15 14:15:56 +07:00
committed by Tim Abbott
parent 3d2110be51
commit a43a898e12
2 changed files with 22 additions and 10 deletions

View File

@@ -465,7 +465,8 @@ export function format_time_modern(time: number | Date, today = new Date()): str
// this is for rendering absolute time based off the preferences for twenty-four
// hour time in the format of "%mmm %d, %h:%m %p".
export function absolute_time(timestamp: number, today = new Date()): string {
export function absolute_time(timestamp: number): string {
const today = new Date();
const date = new Date(timestamp);
const is_older_year = today.getFullYear() - date.getFullYear() > 0;

View File

@@ -394,29 +394,35 @@ run_test("absolute_time_12_hour", () => {
let timestamp = date_2019.getTime();
let today = date_2019;
MockDate.set(today.getTime());
let expected = "Apr 12, 5:52 PM";
let actual = timerender.absolute_time(timestamp, today);
let actual = timerender.absolute_time(timestamp);
assert.equal(actual, expected);
// timestamp with hour > 12, different year
let next_year = add(today, {years: 1});
MockDate.set(next_year.getTime());
expected = "Apr 12, 2019, 5:52 PM";
actual = timerender.absolute_time(timestamp, next_year);
actual = timerender.absolute_time(timestamp);
assert.equal(actual, expected);
// timestamp with hour < 12, same year
timestamp = date_2017.getTime();
today = date_2017;
MockDate.set(today.getTime());
expected = "May 18, 7:12 AM";
actual = timerender.absolute_time(timestamp, today);
actual = timerender.absolute_time(timestamp);
assert.equal(actual, expected);
// timestamp with hour < 12, different year
next_year = add(today, {years: 1});
MockDate.set(next_year.getTime());
expected = "May 18, 2017, 7:12 AM";
actual = timerender.absolute_time(timestamp, next_year);
actual = timerender.absolute_time(timestamp);
assert.equal(actual, expected);
MockDate.reset();
});
run_test("absolute_time_24_hour", () => {
@@ -424,28 +430,33 @@ run_test("absolute_time_24_hour", () => {
// date with hour > 12, same year
let today = date_2019;
MockDate.set(today.getTime());
let expected = "Apr 12, 17:52";
let actual = timerender.absolute_time(date_2019.getTime(), today);
let actual = timerender.absolute_time(date_2019.getTime());
assert.equal(actual, expected);
// date with hour > 12, different year
let next_year = add(today, {years: 1});
MockDate.set(next_year.getTime());
expected = "Apr 12, 2019, 17:52";
actual = timerender.absolute_time(date_2019.getTime(), next_year);
actual = timerender.absolute_time(date_2019.getTime());
assert.equal(actual, expected);
// timestamp with hour < 12, same year
today = date_2017;
MockDate.set(today.getTime());
expected = "May 18, 07:12";
actual = timerender.absolute_time(date_2017.getTime(), today);
actual = timerender.absolute_time(date_2017.getTime());
assert.equal(actual, expected);
// timestamp with hour < 12, different year
next_year = add(today, {years: 1});
MockDate.set(next_year.getTime());
expected = "May 18, 2017, 07:12";
actual = timerender.absolute_time(date_2017.getTime(), next_year);
actual = timerender.absolute_time(date_2017.getTime());
assert.equal(actual, expected);
MockDate.reset();
});
run_test("get_full_datetime", () => {