Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Post Schedule: Depending on the timezone show/hide the AM/PM selector
  • Loading branch information
youknowriad committed May 24, 2017
commit 9625759d8d563157281d0113a15ff4512f556639
18 changes: 12 additions & 6 deletions editor/sidebar/post-schedule/clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@
import { __ } from 'i18n';
import Button from 'components/button';

function PostScheduleClock( { selected, onChange } ) {
function PostScheduleClock( { is12Hour, selected, onChange } ) {
const minutes = selected ? selected.format( 'mm' ) : '';
const am = selected ? selected.format( 'A' ) : 'AM';
const hours = selected ? selected.format( 'hh' ) : '';
const hours = selected ? selected.format( is12Hour ? 'hh' : 'HH' ) : '';

const updateHours = ( event ) => {
const value = parseInt( event.target.value, 10 );
if ( value < 1 || value > 12 ) {
if (
( is12Hour && ( value < 1 || value > 12 ) ) ||
( ! is12Hour && ( value < 0 || value > 23 ) )
) {
return;
}
const newDate = selected.clone().hours( am === 'AM' ? value % 12 : ( ( ( value % 12 ) + 12 ) % 24 ) );

const newDate = is12Hour
? selected.clone().hours( am === 'AM' ? value % 12 : ( ( ( value % 12 ) + 12 ) % 24 ) )
: selected.clone().hours( value );
onChange( newDate );
};

Expand Down Expand Up @@ -58,7 +64,7 @@ function PostScheduleClock( { selected, onChange } ) {
value={ minutes }
onChange={ updateMinutes }
/>
<div>
{ is12Hour && <div>
<Button
className="button-secondary editor-post-schedule__clock-am-button"
isToggled={ am === 'AM' }
Expand All @@ -73,7 +79,7 @@ function PostScheduleClock( { selected, onChange } ) {
>
{ __( 'PM' ) }
</Button>
</div>
</div> }
</div>
);
}
Expand Down
2 changes: 2 additions & 0 deletions editor/sidebar/post-schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class PostSchedule extends Component {
const handleChange = ( newDate ) => {
onUpdateDate( newDate.format( 'YYYY-MM-DDTHH:mm:ss' ) );
};
const is12HourTime = settings.formats.time.indexOf( 'a' ) !== -1 || settings.formats.time.indexOf( 'A' ) !== -1;
Copy link
Member

Choose a reason for hiding this comment

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

Can be inaccurate because it's possible to escape characters in the format. In other words, 'H:i:s \a\w\e\s\o\m\e' is not twelve hour time, but this logic would treat it as though it were.

Also, it could be simpler to force to lower/upper and test index of the one instance of 'a'/'A'.

Accounting for escaping could be tricky, particularly since JavaScript doesn't support lookbehind regular expression patterns. Dunno if there's a better way, but "flipping" the string and doing a look ahead is one option:

/a(?!\\)/i.test( 'h:i:s a'.split( '' ).reverse().join( '' ) )
// true
/a(?!\\)/i.test( 'h:i:s A'.split( '' ).reverse().join( '' ) )
// true
/a(?!\\)/i.test( 'H:i:s \\a\\w\\e\\s\\o\\m\\e'.split( '' ).reverse().join( '' ) )
// false

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even this solution is not perfect because this \\\\\a is a 12 hour time but the test returns false

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess this is the right test /a(?!\\)/i.test( '\\\\\\\\a'.replace(/\\\\/g).split( '' ).reverse().join( '' ) )

return (
<div className="editor-post-schedule">
<span>{ __( 'Publish' ) }</span>
Expand All @@ -68,6 +69,7 @@ class PostSchedule extends Component {
<PostScheduleClock
selected={ momentDate }
onChange={ handleChange }
is12Hour={ is12HourTime }
/>
</div>
}
Expand Down