From 52ecf8d47e94b2e3a030b44d67f889abbaffe7af Mon Sep 17 00:00:00 2001 From: David Shanske Date: Sun, 12 Jul 2020 03:53:28 +0000 Subject: [PATCH] Fix issues with timezone calculation --- includes/class-astronomical-calculator.php | 5 ++++- includes/class-geo-data.php | 3 +++ includes/class-rest-geo.php | 1 - includes/class-weather-provider.php | 22 ++++++++++++++++------ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/includes/class-astronomical-calculator.php b/includes/class-astronomical-calculator.php index 09567cd..8848108 100644 --- a/includes/class-astronomical-calculator.php +++ b/includes/class-astronomical-calculator.php @@ -76,7 +76,10 @@ public function __construct( $latitude, $longitude, $elevation = null ) { $this->longitude = $longitude; $this->elevation = intval( $elevation ); $this->zenith = $this->get_zenith(); - $this->timezone = new DateTimeZone( Loc_Timezone::timezone_for_location( $latitude, $longitude ) ); + $this->timezone = Loc_Timezone::timezone_for_location( $latitude, $longitude ); + if ( $this->timezone instanceof Timezone_Result ) { + $this->timezone = $this->timezone->timezone; + } } diff --git a/includes/class-geo-data.php b/includes/class-geo-data.php index 57f8c1a..356c692 100644 --- a/includes/class-geo-data.php +++ b/includes/class-geo-data.php @@ -810,6 +810,9 @@ public static function exif_data( $meta, $file, $image_type, $iptc = null, $exif if ( ! empty( $meta['location'] ) ) { // Try to get the right timezone from the location. $timezone = Loc_Timezone::timezone_for_location( $meta['location']['latitude'], $meta['location']['longitude'] ); + if ( $timezone instanceof Timezone_Result ) { + $timezone = $timezone->timezone; + } } else { $timezone = wp_timezone(); } diff --git a/includes/class-rest-geo.php b/includes/class-rest-geo.php index dc0bd9f..0c4f545 100644 --- a/includes/class-rest-geo.php +++ b/includes/class-rest-geo.php @@ -336,7 +336,6 @@ public static function weather( $request ) { $weather->set( array( 'station_id' => $params['station'] ) ); } elseif ( ! empty( $params['longitude'] ) && ! empty( $params['latitude'] ) ) { $weather->set( $params ); - $timezone = Loc_Timezone::timezone_for_location( $params['latitude'], $params['longitude'] ); $return = array( 'latitude' => $params['latitude'], 'longitude' => $params['longitude'], diff --git a/includes/class-weather-provider.php b/includes/class-weather-provider.php index 9ce2d49..53063a6 100644 --- a/includes/class-weather-provider.php +++ b/includes/class-weather-provider.php @@ -100,6 +100,8 @@ public function __construct( $args = array() ) { /** * Extra Parameters for location. * + * @param array $return Weather data. + * @param int $timestamp Unix timestamp. Optional. * @return array { * Arguments. * @type string $day 'true' if daytime, 'false' if night. @@ -110,14 +112,22 @@ public function __construct( $args = array() ) { * @type string $localtime Local time. * } */ - public function extra_data( $return ) { - $calc = new Astronomical_Calculator( $return['latitude'], $return['longitude'], $return['altitude'] ); - $return['sunrise'] = $calc->get_iso8601( null ); - $return['sunset'] = $calc->get_iso8601( null, 'sunset' ); + public function extra_data( $return, $timestamp = null ) { + $calc = new Astronomical_Calculator( $return['latitude'], $return['longitude'], $return['altitude'] ); + $return['sunrise'] = $calc->get_iso8601( null ); + $return['sunset'] = $calc->get_iso8601( null, 'sunset' ); $return['moonrise'] = $calc->get_iso8601( null, 'moonrise' ); $return['moonset'] = $calc->get_iso8601( null, 'moonset' ); - $return['day'] = $calc->is_daytime(); - $datetime = new DateTime( null, new DateTimeZone( Loc_Timezone::timezone_for_location( $return['latitude'], $return['longitude'] ) ) ); + $return['day'] = $calc->is_daytime(); + $timezone = Loc_Timezone::timezone_for_location( $return['latitude'], $return['longitude'] ); + if ( $timezone instanceof Timezone_Result ) { + $timezone = $timezone->timezone; + } + $datetime = new DateTime( null, $timezone ); + if ( ! is_null( $timestamp ) ) { + $datetime->setTimestamp( $timestamp ); + } + $return['localtime'] = $datetime->format( DATE_W3C ); return array_filter( $return ); }