Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Fix browser incompatible way of parsing local datetime strings #304

Merged
merged 1 commit into from
Oct 4, 2016
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
4 changes: 2 additions & 2 deletions js/customize-post-date-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@
return;
}

remainingTime = ( new Date( control.setting.get().post_date ) ).valueOf();
remainingTime -= ( new Date( api.Posts.getCurrentTime() ) ).valueOf();
remainingTime = api.Posts.parsePostDate( control.setting.get().post_date ).valueOf();
remainingTime -= api.Posts.parsePostDate( api.Posts.getCurrentTime() ).valueOf();
remainingTime = Math.ceil( remainingTime / 1000 );
if ( remainingTime > 0 ) {
control.scheduledCountdownContainer.text( control.scheduledCountdownTemplate( { remainingTime: remainingTime } ) );
Expand Down
6 changes: 4 additions & 2 deletions js/customize-post-status-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,17 @@
*/
updateChoices: function updateChoices() {
var control = this, data = control.setting.get(), isFuture, postTimestamp, currentTimestamp;
postTimestamp = ( new Date( data.post_date ) ).valueOf();
currentTimestamp = ( new Date( api.Posts.getCurrentTime() ) ).valueOf();
postTimestamp = api.Posts.parsePostDate( data.post_date );
currentTimestamp = api.Posts.parsePostDate( api.Posts.getCurrentTime() );
isFuture = postTimestamp > currentTimestamp;

/*
* Account for race condition when saving a post with an empty date
* when server time and client time aren't exactly aligned. If the
* status is publish, and yet the post date is less than 15 seconds
* into the future, consider it as not future.
*
* See also https://github.com/xwp/wp-customize-posts/issues/303
*/
if ( isFuture && 'publish' === data.post_status && postTimestamp - currentTimestamp < 15 * 1000 ) {
isFuture = false;
Expand Down
15 changes: 14 additions & 1 deletion js/customize-posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,26 @@
component.getCurrentTime = function getCurrentTime() {
var currentDate, currentTimestamp, timestampDifferential;
currentTimestamp = ( new Date() ).valueOf();
currentDate = new Date( component.data.initialServerDate.replace( ' ', 'T' ) );
currentDate = component.parsePostDate( component.data.initialServerDate );
timestampDifferential = currentTimestamp - component.data.initialClientTimestamp;
timestampDifferential += component.data.initialClientTimestamp - component.data.initialServerTimestamp;
currentDate.setTime( currentDate.getTime() + timestampDifferential );
return component.formatDate( currentDate );
};

/**
* Parse post date string in YYYY-MM-DD HH:MM:SS format (local timezone).
*
* @param {string} postDate Post date string.
* @returns {Date} Parsed date.
*/
component.parsePostDate = function parsePostDate( postDate ) {
var dateParts = _.map( postDate.split( /\D/ ), function( datePart ) {
return parseInt( datePart, 10 );
} );
return new Date( dateParts[0], dateParts[1] - 1, dateParts[2], dateParts[3], dateParts[4], dateParts[5] ); // eslint-disable-line no-magic-numbers
};

/**
* Focus on the control requested from the preview.
*
Expand Down