Fix follows and followers pages to show "Today" instead of hours (#40367)

This commit is contained in:
Federico Franco
2026-09-04 15:38:45 +00:00
committed by GitHub
parent ee3cb553cd
commit 0a32b4a831
2 changed files with 31 additions and 6 deletions

View File

@@ -1,4 +1,13 @@
import { DAY, HOUR, MINUTE, relativeTimeParts, SECOND } from './time';
import { createIntl } from 'react-intl';
import {
DAY,
HOUR,
MINUTE,
SECOND,
relativeTimeParts,
formatTime,
} from './time';
describe('relativeTimeParts', () => {
const now = Date.now();
@@ -36,3 +45,19 @@ describe('relativeTimeParts', () => {
expect(relativeTimeParts(now + input, now)).toMatchObject(expected);
});
});
describe('formatTime with day-only timestamps', () => {
const intl = createIntl({ locale: 'en' });
const now = Date.parse('2026-08-20T22:56:00Z');
test.concurrent.each([
['2026-08-20', 'today'],
['2026-08-19', '1 day ago'],
['2026-08-17', '3 days ago'],
])('should format the day-only value %s as "%s"', (date, expected) => {
expect(
formatTime({ timestamp: Date.parse(date), intl, now, noTime: true }),
).toBe(expected);
});
});

View File

@@ -41,13 +41,13 @@ export function relativeTimeParts(
return { value: sign * Math.floor(absDelta / DAY), unit: 'day', delta };
}
export function isToday(ts: number, now = Date.now()): boolean {
export function isSameUTCDay(ts: number, now = Date.now()): boolean {
const date = new Date(ts);
const nowDate = new Date(now);
return (
date.getDate() === nowDate.getDate() &&
date.getMonth() === nowDate.getMonth() &&
date.getFullYear() === nowDate.getFullYear()
date.getUTCDate() === nowDate.getUTCDate() &&
date.getUTCMonth() === nowDate.getUTCMonth() &&
date.getUTCFullYear() === nowDate.getUTCFullYear()
);
}
@@ -138,7 +138,7 @@ export function formatTime({
const { value, unit } = relativeTimeParts(timestamp, now);
// If we're only showing days, show "today" for the current day.
if (noTime && isToday(timestamp, now)) {
if (noTime && isSameUTCDay(timestamp, now)) {
return intl.formatMessage(timeMessages.today);
}