Skip to content

Commit

Permalink
Update the php_to_js_dateformat transformation to work in more contex…
Browse files Browse the repository at this point in the history
…ts, and add some tests.
  • Loading branch information
jtsternberg committed Oct 31, 2017
1 parent 18e29be commit 7212e87
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
47 changes: 29 additions & 18 deletions includes/CMB2_Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public static function get_timestamp_from_value( $value, $date_format ) {
* Takes a php date() format string and returns a string formatted to suit for the date/time pickers
* It will work with only with the following subset ot date() options:
*
* d, j, z, m, n, y, and Y.
* d, l, j, z, m, F, n, y, and Y.
*
* A slight effort is made to deal with escaped characters.
*
Expand All @@ -467,32 +467,43 @@ public static function php_to_js_dateformat( $format ) {

// order is relevant here, since the replacement will be done sequentially.
$supported_options = array(
'd' => 'dd', // Day, leading 0
'j' => 'd', // Day, no 0
'z' => 'o', // Day of the year, no leading zeroes,
'd' => 'dd', // Day, leading 0
'j' => 'd', // Day, no 0
'z' => 'o', // Day of the year, no leading zeroes,
// 'D' => 'D', // Day name short, not sure how it'll work with translations
// 'l' => 'DD', // Day name full, idem before
'm' => 'mm', // Month of the year, leading 0
'n' => 'm', // Month of the year, no leading 0
'l ' => 'DD ', // Day name full, idem before
'l, ' => 'DD, ', // Day name full, idem before
'm' => 'mm', // Month of the year, leading 0
'n' => 'm', // Month of the year, no leading 0
// 'M' => 'M', // Month, Short name
// 'F' => 'MM', // Month, full name,
'y' => 'y', // Year, two digit
'Y' => 'yy', // Year, full
'H' => 'HH', // Hour with leading 0 (24 hour)
'G' => 'H', // Hour with no leading 0 (24 hour)
'h' => 'hh', // Hour with leading 0 (12 hour)
'g' => 'h', // Hour with no leading 0 (12 hour),
'i' => 'mm', // Minute with leading 0,
's' => 'ss', // Second with leading 0,
'a' => 'tt', // am/pm
'A' => 'TT',// AM/PM
'F ' => 'MM ', // Month, full name,
'F, ' => 'MM, ', // Month, full name,
'y' => 'y', // Year, two digit
'Y' => 'yy', // Year, full
'H' => 'HH', // Hour with leading 0 (24 hour)
'G' => 'H', // Hour with no leading 0 (24 hour)
'h' => 'hh', // Hour with leading 0 (12 hour)
'g' => 'h', // Hour with no leading 0 (12 hour),
'i' => 'mm', // Minute with leading 0,
's' => 'ss', // Second with leading 0,
'a' => 'tt', // am/pm
'A' => 'TT',// AM/PM
);

foreach ( $supported_options as $php => $js ) {
// replaces every instance of a supported option, but skips escaped characters
$format = preg_replace( "~(?<!\\\\)$php~", $js, $format );
}

$supported_options = array(
'l' => 'DD', // Day name full, idem before
'F' => 'MM', // Month, full name,
);

if ( isset( $supported_options[ $format ] ) ) {
$format = $supported_options[ $format ];
}

$format = preg_replace_callback( '~(?:\\\.)+~', array( __CLASS__, 'wrap_escaped_chars' ), $format );

return $format;
Expand Down
30 changes: 30 additions & 0 deletions tests/test-cmb-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,36 @@ public function test_normalize_if_numeric() {

}

/**
* @group failing
*
*/
public function test_php_to_js_dateformat() {
$tests = array(
array( 'l F j, Y', 'DD MM d, yy' ),
array( 'Y-m-d', 'yy-mm-dd' ),
array( 'm-d-Y', 'mm-dd-yy' ),
array( 'F', 'MM' ),
array( 'l', 'DD' ),
array( 'F', 'MM' ),
array( 'l jS \of F Y h:i:s A', 'DD dS &#39;o&#39;f MM yy hh:mm:ss TT' ),
array( 'm.d.y', 'mm.dd.y' ),
array( 'j, n, Y', 'd, m, yy' ),
array( 'Ymd', 'yymmdd' ),
// @todo Fix these:
array( 'j-m-y, \it \is w Day', 'd-mm-y, &#39;i&#39;t &#39;i&#39;ss w Dtty' ),
array( '\i\t \i\s \t\h\e jS \d\a\y.', '&#39;it&#39; &#39;is&#39; &#39;the&#39; dS &#39;day&#39;.' ),
);
foreach ( $tests as $index => $test ) {
$this->assertSame(
$test[1],
CMB2_Utils::php_to_js_dateformat( $test[0] ),
"Test index: $index"
);
}

}

}

class Test_CMB2_Utils_WIN extends CMB2_Utils {
Expand Down

0 comments on commit 7212e87

Please sign in to comment.