Skip to content

Commit

Permalink
Fix issues with timezone calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
dshanske committed Jul 12, 2020
1 parent aa47c3a commit 52ecf8d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
5 changes: 4 additions & 1 deletion includes/class-astronomical-calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}


Expand Down
3 changes: 3 additions & 0 deletions includes/class-geo-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
1 change: 0 additions & 1 deletion includes/class-rest-geo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
22 changes: 16 additions & 6 deletions includes/class-weather-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 );
}
Expand Down

0 comments on commit 52ecf8d

Please sign in to comment.