Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@wordpress/date: Fix ordinal token (S), RFC2822 token (r), and timezone offset handling #39670

Merged
merged 2 commits into from
Mar 24, 2022
Merged
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: 45 additions & 14 deletions packages/date/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,34 @@ let settings = {
export function setSettings( dateSettings ) {
settings = dateSettings;

// Backup and restore current locale.
setupWPTimezone();

// Does moment already have a locale with the right name?
if ( momentLib.locales().includes( dateSettings.l10n.locale ) ) {
// Is that locale misconfigured, e.g. because we are on a site running
// WordPress < 6.0?
Copy link
Member Author

Choose a reason for hiding this comment

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

Noting to myself that I need to submit a Core patch that makes this same fix there. Should also add a comment here to delete this code when the minimum WP version is 6.0.

if (
momentLib
.localeData( dateSettings.l10n.locale )
.longDateFormat( 'LTS' ) === null
) {
// Delete the misconfigured locale.
// @ts-ignore Type definitions are incorrect - null is permitted.
momentLib.defineLocale( dateSettings.l10n.locale, null );
} else {
// We have a properly configured locale, so no need to create one.
return;
}
}

// defineLocale() will modify the current locale, so back it up.
const currentLocale = momentLib.locale();
momentLib.updateLocale( dateSettings.l10n.locale, {
// Inherit anything missing from the default locale.
parentLocale: currentLocale,

// Create locale.
momentLib.defineLocale( dateSettings.l10n.locale, {
// Inherit anything missing from English. We don't load
// moment-with-locales.js so English is all there is.
parentLocale: 'en',
months: dateSettings.l10n.months,
monthsShort: dateSettings.l10n.monthsShort,
weekdays: dateSettings.l10n.weekdays,
Expand All @@ -157,22 +180,19 @@ export function setSettings( dateSettings ) {
},
longDateFormat: {
LT: dateSettings.formats.time,
// @ts-ignore Forcing this to `null`
LTS: null,
// @ts-ignore Forcing this to `null`
L: null,
LTS: momentLib.localeData( 'en' ).longDateFormat( 'LTS' ),
L: momentLib.localeData( 'en' ).longDateFormat( 'L' ),
LL: dateSettings.formats.date,
LLL: dateSettings.formats.datetime,
// @ts-ignore Forcing this to `null`
LLLL: null,
LLLL: momentLib.localeData( 'en' ).longDateFormat( 'LLLL' ),
},
// From human_time_diff?
// Set to `(number, withoutSuffix, key, isFuture) => {}` instead.
relativeTime: dateSettings.l10n.relative,
} );
momentLib.locale( currentLocale );

setupWPTimezone();
// Restore the locale to what it was.
momentLib.locale( currentLocale );
}

/**
Expand Down Expand Up @@ -366,7 +386,18 @@ const formatMap = {
},
// Full date/time.
c: 'YYYY-MM-DDTHH:mm:ssZ', // .toISOString.
r: 'ddd, D MMM YYYY HH:mm:ss ZZ',
/**
* Formats the date as RFC2822.
*
* @param {Moment} momentDate Moment instance.
*
* @return {string} Formatted date.
*/
r( momentDate ) {
return momentDate
.locale( 'en' )
.format( 'ddd, DD MMM YYYY HH:mm:ss ZZ' );
},
U: 'X',
};

Expand Down Expand Up @@ -558,7 +589,7 @@ function buildMoment( dateValue, timezone = '' ) {
return dateMoment.tz( settings.timezone.string );
}

return dateMoment.utcOffset( settings.timezone.offset );
return dateMoment.utcOffset( +settings.timezone.offset );
}

/**
Expand Down
Loading