Skip to content

Commit

Permalink
@wordpress/date: Fix ordinal token (S), RFC2822 token (r), and timezo…
Browse files Browse the repository at this point in the history
…ne offset handling (#39670)

* Add tests to wp-date for different format strings

* Fix failing wp-date tests
  • Loading branch information
noisysocks authored Mar 24, 2022
1 parent b11e3a1 commit 266799e
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 130 deletions.
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?
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

0 comments on commit 266799e

Please sign in to comment.