[wp-trac] [WordPress Trac] #24730: Introduce a timezone-retrieval method
WordPress Trac
noreply at wordpress.org
Fri Jun 29 11:37:05 UTC 2018
#24730: Introduce a timezone-retrieval method
-------------------------+------------------------------
Reporter: rmccue | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Date/Time | Version:
Severity: normal | Resolution:
Keywords: has-patch | Focuses:
-------------------------+------------------------------
Comment (by remcotolsma):
Good point, just added timezones in the tests. That part was indeed broken
;), but i think it's fixed now:
- https://github.com/pronamic/wp-
datetime/commit/7c33fe9acf26d308616c788af7eb71518de83d97
But there is an issue on PHP 5.3:
- https://travis-ci.org/pronamic/wp-datetime/jobs/398202665
{{{#!php
<?php
$date_1 = new DateTime( '+12:45' );
$timezone = $date_1->getTimezone();
$date_2 = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
$date_2->setTimezone( $timezone );
}}}
Will result in the following error:
{{{
DateTime::setTimezone(): Can only do this for zones with ID for now
}}}
You can easily test it with PHP <= 5.3 on
http://sandbox.onlinephpfunctions.com/.
Side Note: I'm not a big fan of all the `preg_replace` calls in the
`date_i18n` function:
- https://github.com/WordPress/WordPress/blob/4.9.6/wp-
includes/functions.php#L103-L119
- https://github.com/WordPress/WordPress/blob/4.9.6/wp-
includes/functions.php#L120-L137
Maybe we could rewrite this to something like this:
{{{#!php
<?php
private function format_i18n_translate( $format ) {
global $wp_locale;
if ( empty( $wp_locale->month ) || empty( $wp_locale->weekday ) )
{
return $format;
}
$month = $wp_locale->get_month( $this->format( 'm' ) );
$weekday = $wp_locale->get_weekday( $this->format( 'w' ) );
$format_length = strlen( $format );
$format_new = '';
for ( $i = 0; $i < $format_length; $i++ ) {
switch ( $format[ $i ] ) {
case 'D':
$format_new .= backslashit(
$wp_locale->get_weekday_abbrev( $weekday ) );
break;
case 'F':
$format_new .= backslashit( $month );
break;
case 'l':
$format_new .= backslashit( $weekday );
break;
case 'M':
$format_new .= backslashit(
$wp_locale->get_month_abbrev( $month ) );
break;
case 'a':
$format_new .= backslashit(
$wp_locale->get_meridiem( $this->format( 'a' ) ) );
break;
case 'A':
$format_new .= backslashit(
$wp_locale->get_meridiem( $this->format( 'A' ) ) );
break;
case '\\':
$format_new .= $format[ $i ];
if ( $i < $format_length ) {
$i++;
}
// no break
default:
$format_new .= $format[ $i ];
break;
}
}
return $format_new;
}
}}}
I do something similar to prevent `date_i18n` to format the timezones
characters:
{{{#!php
<?php
private function format_i18n_timezone( $format ) {
$format_length = strlen( $format );
$format_new = '';
for ( $i = 0; $i < $format_length; $i++ ) {
switch ( $format[ $i ] ) {
case 'P':
case 'I':
case 'O':
case 'T':
case 'Z':
case 'e':
$format_new .= backslashit( $this->format(
$format[ $i ] ) );
break;
case '\\':
$format_new .= $format[ $i ];
if ( $i < $format_length ) {
$i++;
}
// no break
default:
$format_new .= $format[ $i ];
break;
}
}
return $format_new;
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/24730#comment:11>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list