Skip to content

Commit

Permalink
Fix multiple timezone related errors, closes #73. (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmfcmf committed Apr 17, 2016
1 parent f4fc509 commit 96e95ef
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
3 changes: 0 additions & 3 deletions Cmfcmf/OpenWeatherMap/Forecast.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class Forecast extends CurrentWeather
*/
public function __construct(\SimpleXMLElement $xml, $units)
{
$this->city = new City($xml->city['id'], $xml->city['name'], $xml->city->coord['lon'], $xml->city->coord['lat'], $xml->city->country);

if ($units == 'metric') {
$temperatureUnit = "°C";
} else {
Expand All @@ -70,7 +68,6 @@ public function __construct(\SimpleXMLElement $xml, $units)
$this->wind = new Wind(new Unit($xml->windSpeed['mps'], $windSpeedUnit, $xml->windSpeed['name']), new Unit($xml->windDirection['deg'], $xml->windDirection['code'], $xml->windDirection['name']));
$this->clouds = new Unit($xml->clouds['all'], $xml->clouds['unit'], $xml->clouds['value']);
$this->precipitation = new Unit($xml->precipitation['value'], null, $xml->precipitation['type']);
$this->sun = new Sun(new \DateTime($xml->city->sun['rise']), new \DateTime($xml->city->sun['set']));
$this->weather = new WeatherObj($xml->symbol['number'], $xml->symbol['name'], $xml->symbol['var']);
$this->lastUpdate = new \DateTime($xml->lastupdate['value']);

Expand Down
4 changes: 3 additions & 1 deletion Cmfcmf/OpenWeatherMap/WeatherForecast.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class WeatherForecast implements \Iterator
public function __construct($xml, $units, $days)
{
$this->city = new City($xml->location->location['geobaseid'], $xml->location->name, $xml->location->location['longitude'], $xml->location->location['latitude'], $xml->location->country);
$this->sun = new Sun(new \DateTime($xml->sun['rise']), new \DateTime($xml->sun['set']));
$utctz = new \DateTimeZone('UTC');
$this->sun = new Sun(new \DateTime($xml->sun['rise'], $utctz), new \DateTime($xml->sun['set'], $utctz));
$this->lastUpdate = new \DateTime($xml->meta->lastupdate);

$today = new \DateTime();
Expand All @@ -90,6 +91,7 @@ public function __construct($xml, $units, $days)
}
$forecast = new Forecast($time, $units);
$forecast->city = $this->city;
$forecast->sun = $this->sun;
$this->forecasts[] = $forecast;

$counter++;
Expand Down
28 changes: 26 additions & 2 deletions Cmfcmf/OpenWeatherMap/WeatherHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Cmfcmf\OpenWeatherMap;

use Cmfcmf\OpenWeatherMap;
use Cmfcmf\OpenWeatherMap\Util\City;

/**
* Class WeatherHistory.
Expand Down Expand Up @@ -62,17 +63,40 @@ public function __construct($weatherHistory, $query)
$population = null;
}

$this->city = new OpenWeatherMap\Util\City($weatherHistory['city_id'], (is_string($query)) ? $query : null, (isset($query['lon'])) ? $query['lon'] : null, (isset($query['lat'])) ? $query['lat'] : null, $country, $population);
$this->city = new City(
$weatherHistory['city_id'],
(is_string($query)) ? $query : null,
(isset($query['lon'])) ? $query['lon'] : null,
(isset($query['lat'])) ? $query['lat'] : null,
$country,
$population
);
$this->calctime = $weatherHistory['calctime'];

$utctz = new \DateTimeZone('UTC');
foreach ($weatherHistory['list'] as $history) {
if (isset($history['rain'])) {
$units = array_keys($history['rain']);
} else {
$units = array(0 => null);
}

$this->histories[] = new History($this->city, $history['weather'][0], array('now' => $history['main']['temp'], 'min' => $history['main']['temp_min'], 'max' => $history['main']['temp_max']), $history['main']['pressure'], $history['main']['humidity'], $history['clouds']['all'], isset($history['rain']) ? array('val' => $history['rain'][($units[0])], 'unit' => $units[0]) : null, $history['wind'], \DateTime::createFromFormat('U', $history['dt']));
$this->histories[] = new History(
$this->city,
$history['weather'][0],
array(
'now' => $history['main']['temp'],
'min' => $history['main']['temp_min'],
'max' => $history['main']['temp_max']
),
$history['main']['pressure'],
$history['main']['humidity'],
$history['clouds']['all'],
isset($history['rain']) ? array(
'val' => $history['rain'][($units[0])],
'unit' => $units[0]) : null,
$history['wind'],
\DateTime::createFromFormat('U', $history['dt'], $utctz));
}
}

Expand Down
7 changes: 6 additions & 1 deletion Examples/WeatherForecast.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
echo "<br />\n";
echo "LastUpdate: " . $forecast->lastUpdate->format('d.m.Y H:i');
echo "<br />\n";
echo "Sunrise : " . $forecast->sun->rise->format("H:i:s") . " Sunset : " . $forecast->sun->set->format("H:i:s");
echo "Sunrise : " . $forecast->sun->rise->format("H:i:s (e)") . " Sunset : " . $forecast->sun->set->format("H:i:s (e)");
echo "<br />\n";
echo "<br />\n";

Expand All @@ -47,6 +47,8 @@
echo "<br />\n";
echo $weather->temperature;
echo "<br />\n";
echo "Sun rise: " . $weather->sun->rise->format('d.m.Y H:i (e)');
echo "<br />\n";
echo "---";
echo "<br />\n";
}
Expand All @@ -58,5 +60,8 @@
foreach ($forecast as $weather) {
echo "Weather forecast at " . $weather->time->day->format('d.m.Y') . " from " . $weather->time->from->format('H:i') . " to " . $weather->time->to->format('H:i') . "<br />";
echo $weather->temperature . "<br />\n";
echo "<br />\n";
echo "Sun rise: " . $weather->sun->rise->format('d.m.Y H:i (e)');
echo "<br />\n";
echo "---<br />\n";
}

0 comments on commit 96e95ef

Please sign in to comment.