Skip to content

Commit

Permalink
Blocks: Format date to avoid double conversion.
Browse files Browse the repository at this point in the history
These dates are _already_ in the site timezone, so `dateI18n()` et al would add the UTC offset a _second_ time. In some cases that would cross a date boundary, and the wrong date would be displayed.

Fixes #559
  • Loading branch information
iandunn committed Sep 29, 2020
1 parent 36ce495 commit 17d33ef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { dateI18n } from '@wordpress/date';
*/
import { WC_BLOCKS_STORE } from '../../data';


/*
* Create an implicit "0" track when none formally exist.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { InspectorControls } from '@wordpress/block-editor';
import { CheckboxControl, PanelBody, ToggleControl } from '@wordpress/components';
import { dateI18n } from '@wordpress/date';
import { dateI18n, format } from '@wordpress/date';
import { createInterpolateElement } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
Expand Down Expand Up @@ -62,7 +62,7 @@ export default function ScheduleInspectorControls(
}

/**
* Get all of the dates that the given sessions are assigned to.
* Get all of the dates (in site/venue timezone) that the given sessions are assigned to.
*
* @param {Array} sessions
*
Expand Down Expand Up @@ -124,7 +124,7 @@ function ChooseSpecificDays( { chooseSpecificDays, displayedDays, chosenDays, da
return (
<CheckboxControl
key={ day }
label={ dateI18n( dateFormat, day ) }
label={ format( dateFormat, day ) }
checked={ chosenDays.includes( day ) }
onChange={ ( isChecked ) => {
/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { dateI18n } from '@wordpress/date';
import { dateI18n, format } from '@wordpress/date';
import { __, _x } from '@wordpress/i18n';
import { createContext, useContext } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
Expand Down Expand Up @@ -49,7 +49,7 @@ export function ScheduleGrid( { sessions } ) {
scheduleDays.push(
<ScheduleDay
key={ date }
date={ date }
localDate={ date }
sessions={ sessionsGroup }
/>
);
Expand All @@ -63,7 +63,7 @@ export function ScheduleGrid( { sessions } ) {
}

/**
* Create an array of sessions, indexed by their date.
* Create an array of sessions, indexed by their date (according to the site's timezone).
*
* @param {Array} sessions
*
Expand Down Expand Up @@ -94,17 +94,17 @@ function groupSessionsByDate( sessions ) {
* Render the schedule for a specific day.
*
* @param {Object} props
* @param {string} props.date In a format acceptable by `wp.date.dateI18n()`.
* @param {Array} props.sessions
* @param {string} props.localDate The day being displayed in Ymd format (in the site's timezone).
* @param {Array} props.sessions The sessions assigned to the displayed date.
*
* @return {Element}
*/
function ScheduleDay( { date, sessions } ) {
function ScheduleDay( { localDate, sessions } ) {
const { attributes, allTracks, renderEnvironment, settings } = useContext( ScheduleGridContext );
const { chooseSpecificTracks, chosenTrackIds } = attributes;

const displayedTracks = getDisplayedTracks( sessions, allTracks, chooseSpecificTracks, chosenTrackIds );
const formattedDate = dateI18n( DATE_SLUG_FORMAT, date );
const formattedDate = format( DATE_SLUG_FORMAT, localDate );
const formattedTrackIds = chooseSpecificTracks ? displayedTracks.map( ( track ) => track.id ).join( '-' ) : 'all';

/*
Expand Down Expand Up @@ -145,7 +145,7 @@ function ScheduleDay( { date, sessions } ) {
</style>

<h2 className="wordcamp-schedule__date">
{ dateI18n( settings.date_format, date ) }
{ format( settings.date_format, localDate ) }
</h2>

{ 'editor' === renderEnvironment && renderOverlappingSessionsWarning( overlappingSessions ) }
Expand Down

0 comments on commit 17d33ef

Please sign in to comment.