Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/app/components/MostRead/Canonical/LastUpdated/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import '#lib/locales/moment/pcm';
import { render, screen } from '../../../react-testing-library-with-providers';
import LastUpdated from '.';

describe('MostReadCanonical - LastUpdated', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

it('should render LastUpdated correctly', () => {
jest.setSystemTime(new Date('2026-11-12T12:00:00Z'));

render(
<LastUpdated
timestamp={864691200}
Expand All @@ -17,4 +28,52 @@ describe('MostReadCanonical - LastUpdated', () => {
expect(time.tagName).toBe('TIME');
expect(time).toHaveAttribute('datetime', '1970-01-11');
});

it('omits year for dates published this year', () => {
jest.setSystemTime(new Date('1970-01-12T12:00:00Z'));

render(
<LastUpdated
timestamp={864691200}
prefix="Last Updated:"
locale="en-gb"
timezone="Europe/London"
/>,
);

expect(screen.getByText('Last Updated: 11 January')).toBeInTheDocument();
});

it('uses locale-specific same-year format for hungarian', () => {
jest.setSystemTime(new Date('2026-11-12T12:00:00Z'));

render(
<LastUpdated
timestamp="2026-11-03T05:17:09.393Z"
prefix="Frissítve:"
locale="hu"
timezone="Europe/Budapest"
datetimeMonthDayFormat="MMMM D."
/>,
);

expect(screen.getByText('Frissítve: november 3.')).toBeInTheDocument();
});

it('preserves the ordinal day suffix for pidgin when the year is omitted', () => {
jest.setSystemTime(new Date('2026-11-12T12:00:00Z'));

render(
<LastUpdated
timestamp="2026-11-03T05:17:09.393Z"
prefix="De one we dem update for:"
locale="pcm"
timezone="Africa/Lagos"
/>,
);

expect(
screen.getByText('De one we dem update for: 3rd November'),
).toBeInTheDocument();
});
});
41 changes: 29 additions & 12 deletions src/app/components/MostRead/Canonical/LastUpdated/index.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
import moment from 'moment-timezone';
import getDayAndMonthFormat from '#lib/utilities/getDayAndMonthFormat';
import Timestamp from '#app/components/TimestampContainer';

interface LastUpdatedProps {
locale: string;
prefix: string;
timestamp: number | string;
timezone: string;
datetimeMonthDayFormat?: string;
}

const LastUpdated = ({
locale,
prefix,
timestamp,
timezone,
}: LastUpdatedProps) => (
<Timestamp
timestamp={timestamp}
dateTimeFormat="YYYY-MM-DD"
prefix={prefix}
format="LL"
locale={locale}
timezone={timezone}
padding={false}
altCalendar={null}
/>
);
datetimeMonthDayFormat,
}: LastUpdatedProps) => {
const publicationDate = moment(timestamp).locale(locale).tz(timezone);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems a bit precise, no?

The date is coming through as

                    "timeUpdated": "2026-10-06T16:04:49.565Z",

why aren't we string comparing the first 4 characters to the current year rather than importing the big moment lib?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gets the precise local time so that this works at year boundaries where different timezones start a new year at different times. But if we don't want to use moment, we can go with Aaron's suggestion of Intl.DateTimeFormat instead

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have misunderstood, but it sounds like we would be spending a fair amount of computation (relatively) for some edge cases that are published on Dec 31st and Jan 1st.

The rest of the year we're shouldering that burden unnecessarily

That said, the computation and any additional bundle size could be very minor.

Would you be able to run a simulation to work out what kind of burden we're looking at (for us on server and for the audience in terms of bundle size changes)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just got copilot to run a simulation and it found that it would be no more of a burden than the current implementation

Here is a summary of why

  • Server burden: approximately 8 µs per timestamp, or 0.083 ms for a page with 10 timestamps. Dec 31 and Jan 1 were no more expensive than ordinary dates.
  • At scale: approximately 83 ms of CPU per second for 1,000 pages per second—around 8% of one CPU core, excluding the rest of SSR work.
  • Audience bundle impact: this change adds no new dependency, so the expected bundle-size increase is effectively 0 bytes.

const isPublishedThisYear = publicationDate.isSame(
moment().tz(timezone),
'year',
);

return (
<Timestamp
timestamp={timestamp}
dateTimeFormat="YYYY-MM-DD"
prefix={prefix}
format={
isPublishedThisYear
? datetimeMonthDayFormat || getDayAndMonthFormat(locale)
: 'LL'
}
locale={locale}
timezone={timezone}
isPublishedThisYear={isPublishedThisYear}
padding={false}
altCalendar={null}
/>
);
};

export default LastUpdated;
2 changes: 2 additions & 0 deletions src/app/components/MostRead/Canonical/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const MostRead = ({
dir,
datetimeLocale,
serviceDatetimeLocale,
datetimeMonthDayFormat,
timezone,
mostRead: { lastUpdated, numberOfItems = 5 },
} = use(ServiceContext);
Expand Down Expand Up @@ -95,6 +96,7 @@ const MostRead = ({
timestamp={timestamp}
locale={locale}
timezone={timezone}
datetimeMonthDayFormat={datetimeMonthDayFormat}
/>
)}
</MostReadLink>
Expand Down
8 changes: 6 additions & 2 deletions src/app/components/TimestampContainer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ and relative-time configuration; service configuration owns locale registration.
`en-gb` for the HTML `dateTime` attribute. Defaults are `Europe/London`, `en-gb`,
absolute dates and enabled padding. Prefix, alternative-calendar date, main date
and suffix are separated by spaces. Alternative calendars are omitted for
relative dates. Their existing `formatDate(localisedMoment(...))` contract is
unchanged.
relative dates.

`altCalendar.formatDate` is called as `formatDate(localisedMoment(...), { omitYear })`.
`omitYear` mirrors `isPublishedThisYear`, so an `altCalendar` implementation can
choose to omit its own year segment to stay consistent with a `format` that
also omits the year during the current year.

`className` is forwarded to the `time` element. Semantic markup, accessible text
and hydration-warning suppression are shared across canonical, AMP and Lite.
Expand Down
25 changes: 23 additions & 2 deletions src/app/components/TimestampContainer/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ const elevenMonthsFromNow = moment(Date.now()).subtract(11, 'months').valueOf();
const noLeadingZeroTimestamp = 1530947227000; // 07 July 2018
const invalidData = '8640000000000001'; // A day holds 86,400,000 milliseconds - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Description
const mockCalendar = {
formatDate: (momentLocale: moment.Moment) => {
formatDate: (
momentLocale: moment.Moment,
{ omitYear = false }: { omitYear?: boolean } = {},
) => {
if (momentLocale) {
return '27 مهر 1397';
return omitYear ? '27 مهر' : '27 مهر 1397';
}
return null;
},
Expand Down Expand Up @@ -157,6 +160,24 @@ describe('Timestamp', () => {
const time = screen.getByRole('time');
expect(time.textContent).toEqual('۱۱ ماه پیش');
});

it('should omit the year from the alternative calendar when isPublishedThisYear is true', () => {
render(
<Timestamp
timestamp={defaultTimestamp}
dateTimeFormat="YYYY-MM-DD"
format="D MMMM"
isRelative={false}
isPublishedThisYear
locale="fa"
altCalendar={mockCalendar}
/>,
{ service: 'persian' },
);

const time = screen.getByRole('time');
expect(time.textContent).toEqual('27 مهر - ۱۹ اکتبر');
});
});

it('should render a hausa timestamp', () => {
Expand Down
13 changes: 11 additions & 2 deletions src/app/components/TimestampContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ interface TimestampContainerProps {
timestamp?: number | string;
dateTimeFormat?: string;
format?: string;
isPublishedThisYear?: boolean;
isRelative?: boolean;
padding?: boolean;
prefix?: string;
suffix?: string;
timezone?: string;
locale?: string;
altCalendar?: { formatDate: (date: Moment) => string | null } | null;
altCalendar?: {
formatDate: (
date: Moment,
options?: { omitYear?: boolean },
) => string | null;
} | null;
className?: string;
}

const TimestampContainer = ({
timestamp,
dateTimeFormat,
format = '',
isPublishedThisYear = false,
isRelative = false,
padding = true,
prefix = '',
Expand Down Expand Up @@ -51,7 +58,9 @@ const TimestampContainer = ({

const altDateTime =
altCalendar && !isRelative
? `${altCalendar.formatDate(localisedMoment({ locale, timestamp }))} -`
? `${altCalendar.formatDate(localisedMoment({ locale, timestamp }), {
omitYear: isPublishedThisYear,
})} -`
: undefined;

const timestampText = [prefix, altDateTime, mainDateTime, suffix]
Expand Down
Loading
Loading