mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
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:
committed by
Tim Abbott
parent
3d2110be51
commit
a43a898e12
@@ -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
|
// this is for rendering absolute time based off the preferences for twenty-four
|
||||||
// hour time in the format of "%mmm %d, %h:%m %p".
|
// 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 date = new Date(timestamp);
|
||||||
const is_older_year = today.getFullYear() - date.getFullYear() > 0;
|
const is_older_year = today.getFullYear() - date.getFullYear() > 0;
|
||||||
|
|
||||||
|
|||||||
@@ -394,29 +394,35 @@ run_test("absolute_time_12_hour", () => {
|
|||||||
let timestamp = date_2019.getTime();
|
let timestamp = date_2019.getTime();
|
||||||
|
|
||||||
let today = date_2019;
|
let today = date_2019;
|
||||||
|
MockDate.set(today.getTime());
|
||||||
let expected = "Apr 12, 5:52 PM";
|
let expected = "Apr 12, 5:52 PM";
|
||||||
let actual = timerender.absolute_time(timestamp, today);
|
let actual = timerender.absolute_time(timestamp);
|
||||||
assert.equal(actual, expected);
|
assert.equal(actual, expected);
|
||||||
|
|
||||||
// timestamp with hour > 12, different year
|
// timestamp with hour > 12, different year
|
||||||
let next_year = add(today, {years: 1});
|
let next_year = add(today, {years: 1});
|
||||||
|
MockDate.set(next_year.getTime());
|
||||||
expected = "Apr 12, 2019, 5:52 PM";
|
expected = "Apr 12, 2019, 5:52 PM";
|
||||||
actual = timerender.absolute_time(timestamp, next_year);
|
actual = timerender.absolute_time(timestamp);
|
||||||
assert.equal(actual, expected);
|
assert.equal(actual, expected);
|
||||||
|
|
||||||
// timestamp with hour < 12, same year
|
// timestamp with hour < 12, same year
|
||||||
timestamp = date_2017.getTime();
|
timestamp = date_2017.getTime();
|
||||||
|
|
||||||
today = date_2017;
|
today = date_2017;
|
||||||
|
MockDate.set(today.getTime());
|
||||||
expected = "May 18, 7:12 AM";
|
expected = "May 18, 7:12 AM";
|
||||||
actual = timerender.absolute_time(timestamp, today);
|
actual = timerender.absolute_time(timestamp);
|
||||||
assert.equal(actual, expected);
|
assert.equal(actual, expected);
|
||||||
|
|
||||||
// timestamp with hour < 12, different year
|
// timestamp with hour < 12, different year
|
||||||
next_year = add(today, {years: 1});
|
next_year = add(today, {years: 1});
|
||||||
|
MockDate.set(next_year.getTime());
|
||||||
expected = "May 18, 2017, 7:12 AM";
|
expected = "May 18, 2017, 7:12 AM";
|
||||||
actual = timerender.absolute_time(timestamp, next_year);
|
actual = timerender.absolute_time(timestamp);
|
||||||
assert.equal(actual, expected);
|
assert.equal(actual, expected);
|
||||||
|
|
||||||
|
MockDate.reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
run_test("absolute_time_24_hour", () => {
|
run_test("absolute_time_24_hour", () => {
|
||||||
@@ -424,28 +430,33 @@ run_test("absolute_time_24_hour", () => {
|
|||||||
|
|
||||||
// date with hour > 12, same year
|
// date with hour > 12, same year
|
||||||
let today = date_2019;
|
let today = date_2019;
|
||||||
|
MockDate.set(today.getTime());
|
||||||
let expected = "Apr 12, 17:52";
|
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);
|
assert.equal(actual, expected);
|
||||||
|
|
||||||
// date with hour > 12, different year
|
// date with hour > 12, different year
|
||||||
let next_year = add(today, {years: 1});
|
let next_year = add(today, {years: 1});
|
||||||
|
MockDate.set(next_year.getTime());
|
||||||
expected = "Apr 12, 2019, 17:52";
|
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);
|
assert.equal(actual, expected);
|
||||||
|
|
||||||
// timestamp with hour < 12, same year
|
// timestamp with hour < 12, same year
|
||||||
today = date_2017;
|
today = date_2017;
|
||||||
|
MockDate.set(today.getTime());
|
||||||
expected = "May 18, 07:12";
|
expected = "May 18, 07:12";
|
||||||
actual = timerender.absolute_time(date_2017.getTime(), today);
|
actual = timerender.absolute_time(date_2017.getTime());
|
||||||
assert.equal(actual, expected);
|
assert.equal(actual, expected);
|
||||||
|
|
||||||
// timestamp with hour < 12, different year
|
// timestamp with hour < 12, different year
|
||||||
next_year = add(today, {years: 1});
|
next_year = add(today, {years: 1});
|
||||||
|
MockDate.set(next_year.getTime());
|
||||||
expected = "May 18, 2017, 07:12";
|
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);
|
assert.equal(actual, expected);
|
||||||
|
|
||||||
|
MockDate.reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
run_test("get_full_datetime", () => {
|
run_test("get_full_datetime", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user