Skip to content
Draft
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"react-dom": "19.2.6",
"react-helmet": "6.1.0",
"react-lazyload": "3.2.1",
"temporal-polyfill": "0.3.0",
"undici": "8.3.0",
"uuid": "13.0.1",
"winston": "patch:winston@3.8.2#./patches/winston-file-descriptor.patch"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import 'temporal-polyfill/global';
import moment from 'moment-timezone';

const createDateAdapter = () => ({
// Temporary seam for gradual Temporal adoption in follow-up PRs.
createLocalisedMoment: ({ locale, timestamp }) =>
moment(timestamp).locale(locale),
createMomentInTimezone: ({ locale, timestamp, timezone }) =>
moment(timestamp).locale(locale).tz(timezone),
formatDuration: ({ duration, format, locale }) => {
const defaultDurationFormat = duration?.includes('H') ? 'h:mm:ss' : 'mm:ss';
const durationInMilliseconds = moment.duration(duration).asMilliseconds();

return moment
.utc(durationInMilliseconds)
.locale(locale)
.format(format || defaultDurationFormat);
},
// Exposed to make Temporal available at runtime without changing behavior yet.
toTemporalInstant: timestamp =>
globalThis.Temporal?.Instant?.fromEpochMilliseconds(timestamp),

Check failure on line 21 in src/app/legacy/psammead/psammead-timestamp-container/src/utilities/index.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'globalThis' is not defined
});

const dateAdapter = createDateAdapter();

// Note that this next section is globally configuring moment.
// It is not possible to configure these on specific moment instances.
// The current requirements for rounding & thresholding are the same universally
Expand All @@ -20,12 +43,11 @@
moment.relativeTimeThreshold('M', 12);

export const formatDuration = ({ duration, format, locale = 'en-gb' }) => {
const defaultDurationFormat = duration?.includes('H') ? 'h:mm:ss' : 'mm:ss';
const durationInMilliseconds = moment.duration(duration).asMilliseconds();
return moment
.utc(durationInMilliseconds)
.locale(locale)
.format(format || defaultDurationFormat);
return dateAdapter.formatDuration({
duration,
format,
locale,
});
};

// if the date is invalid return false - https://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript#answer-1353711
Expand All @@ -39,7 +61,10 @@

// when using the following 2 functions, we recommend using webpack configuration to only load in the relevant timezone, rather than all of moment-timezone
export const localisedMoment = ({ locale, timestamp }) => {
return moment(timestamp).locale(locale);
return dateAdapter.createLocalisedMoment({
locale,
timestamp,
});
};

export const formatUnixTimestamp = ({
Expand All @@ -51,7 +76,11 @@
}) => {
if (!timestamp) return undefined;

const momentObj = moment(timestamp).locale(locale).tz(timezone);
const momentObj = dateAdapter.createMomentInTimezone({
locale,
timestamp,
timezone,
});

if (isRelative) {
return momentObj.fromNow();
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15866,6 +15866,7 @@ __metadata:
retry: "npm:0.13.1"
storybook: "npm:10.3.6"
supertest: "npm:7.2.2"
temporal-polyfill: "npm:0.3.0"
timemachine: "npm:0.3.2"
ts-jest: "npm:29.4.9"
typescript: "npm:5.9.3"
Expand Down
Loading