[wp-trac] [WordPress Trac] #24730: Introduce a timezone-retrieval method
WordPress Trac
noreply at wordpress.org
Mon May 27 20:41:45 UTC 2019
#24730: Introduce a timezone-retrieval method
--------------------------------------+-----------------------------
Reporter: rmccue | Owner: SergeyBiryukov
Type: enhancement | Status: reviewing
Priority: normal | Milestone: 5.3
Component: Date/Time | Version:
Severity: normal | Resolution:
Keywords: has-patch has-unit-tests | Focuses:
--------------------------------------+-----------------------------
Comment (by johnjamesjacoby):
> There is no string involved here, gmt_offset is a float value.
`gmt_offset` is only a float when `wp_timezone_override_offset()` finds a
matching `timezone_string` value that can be successfully opened via
`timezone_open()`.
When it does not find a match, or when using UTC offset values (like
`+07:30`), it will be returned as a string (even `sanitize_option()` calls
`preg_replace()` on the value, expecting it to be a string.)
> I don't follow your suggestion, would you mind expressing it in code?
{{{
// Prepare
$offset = (float) get_option( 'gmt_offset' );
$hours = floor( $offset );
$minutes = ( $offset - (int) $hours );
// Calculate
$sign = ( $offset < 0 ) ? '-' : '+';
$abs_hour = abs( $hours );
$abs_mins = abs( $minutes * 60 );
$offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
}}}
Recommend splitting code into 2 chunks, to improve code understandability
and avoid variable reuse on `$offset`.
Recommend `floor()` over `(int)` because casting with `(int)` here
expects others to think `gmt_offset` is always a `float`, and know the
`int` of a `float` is never rounded up.
Recommend moving `abs()` calls out of`sprintf()` so the most important
math is less hidden & nested.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/24730#comment:21>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list