Skip to content

Commit 96e95ef

Browse files
committed
Fix multiple timezone related errors, closes #73. (#75)
1 parent f4fc509 commit 96e95ef

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

Cmfcmf/OpenWeatherMap/Forecast.php

-3
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ class Forecast extends CurrentWeather
4646
*/
4747
public function __construct(\SimpleXMLElement $xml, $units)
4848
{
49-
$this->city = new City($xml->city['id'], $xml->city['name'], $xml->city->coord['lon'], $xml->city->coord['lat'], $xml->city->country);
50-
5149
if ($units == 'metric') {
5250
$temperatureUnit = "°C";
5351
} else {
@@ -70,7 +68,6 @@ public function __construct(\SimpleXMLElement $xml, $units)
7068
$this->wind = new Wind(new Unit($xml->windSpeed['mps'], $windSpeedUnit, $xml->windSpeed['name']), new Unit($xml->windDirection['deg'], $xml->windDirection['code'], $xml->windDirection['name']));
7169
$this->clouds = new Unit($xml->clouds['all'], $xml->clouds['unit'], $xml->clouds['value']);
7270
$this->precipitation = new Unit($xml->precipitation['value'], null, $xml->precipitation['type']);
73-
$this->sun = new Sun(new \DateTime($xml->city->sun['rise']), new \DateTime($xml->city->sun['set']));
7471
$this->weather = new WeatherObj($xml->symbol['number'], $xml->symbol['name'], $xml->symbol['var']);
7572
$this->lastUpdate = new \DateTime($xml->lastupdate['value']);
7673

Cmfcmf/OpenWeatherMap/WeatherForecast.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class WeatherForecast implements \Iterator
7575
public function __construct($xml, $units, $days)
7676
{
7777
$this->city = new City($xml->location->location['geobaseid'], $xml->location->name, $xml->location->location['longitude'], $xml->location->location['latitude'], $xml->location->country);
78-
$this->sun = new Sun(new \DateTime($xml->sun['rise']), new \DateTime($xml->sun['set']));
78+
$utctz = new \DateTimeZone('UTC');
79+
$this->sun = new Sun(new \DateTime($xml->sun['rise'], $utctz), new \DateTime($xml->sun['set'], $utctz));
7980
$this->lastUpdate = new \DateTime($xml->meta->lastupdate);
8081

8182
$today = new \DateTime();
@@ -90,6 +91,7 @@ public function __construct($xml, $units, $days)
9091
}
9192
$forecast = new Forecast($time, $units);
9293
$forecast->city = $this->city;
94+
$forecast->sun = $this->sun;
9395
$this->forecasts[] = $forecast;
9496

9597
$counter++;

Cmfcmf/OpenWeatherMap/WeatherHistory.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace Cmfcmf\OpenWeatherMap;
1919

2020
use Cmfcmf\OpenWeatherMap;
21+
use Cmfcmf\OpenWeatherMap\Util\City;
2122

2223
/**
2324
* Class WeatherHistory.
@@ -62,17 +63,40 @@ public function __construct($weatherHistory, $query)
6263
$population = null;
6364
}
6465

65-
$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);
66+
$this->city = new City(
67+
$weatherHistory['city_id'],
68+
(is_string($query)) ? $query : null,
69+
(isset($query['lon'])) ? $query['lon'] : null,
70+
(isset($query['lat'])) ? $query['lat'] : null,
71+
$country,
72+
$population
73+
);
6674
$this->calctime = $weatherHistory['calctime'];
6775

76+
$utctz = new \DateTimeZone('UTC');
6877
foreach ($weatherHistory['list'] as $history) {
6978
if (isset($history['rain'])) {
7079
$units = array_keys($history['rain']);
7180
} else {
7281
$units = array(0 => null);
7382
}
7483

75-
$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']));
84+
$this->histories[] = new History(
85+
$this->city,
86+
$history['weather'][0],
87+
array(
88+
'now' => $history['main']['temp'],
89+
'min' => $history['main']['temp_min'],
90+
'max' => $history['main']['temp_max']
91+
),
92+
$history['main']['pressure'],
93+
$history['main']['humidity'],
94+
$history['clouds']['all'],
95+
isset($history['rain']) ? array(
96+
'val' => $history['rain'][($units[0])],
97+
'unit' => $units[0]) : null,
98+
$history['wind'],
99+
\DateTime::createFromFormat('U', $history['dt'], $utctz));
76100
}
77101
}
78102

Examples/WeatherForecast.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
echo "<br />\n";
3737
echo "LastUpdate: " . $forecast->lastUpdate->format('d.m.Y H:i');
3838
echo "<br />\n";
39-
echo "Sunrise : " . $forecast->sun->rise->format("H:i:s") . " Sunset : " . $forecast->sun->set->format("H:i:s");
39+
echo "Sunrise : " . $forecast->sun->rise->format("H:i:s (e)") . " Sunset : " . $forecast->sun->set->format("H:i:s (e)");
4040
echo "<br />\n";
4141
echo "<br />\n";
4242

@@ -47,6 +47,8 @@
4747
echo "<br />\n";
4848
echo $weather->temperature;
4949
echo "<br />\n";
50+
echo "Sun rise: " . $weather->sun->rise->format('d.m.Y H:i (e)');
51+
echo "<br />\n";
5052
echo "---";
5153
echo "<br />\n";
5254
}
@@ -58,5 +60,8 @@
5860
foreach ($forecast as $weather) {
5961
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 />";
6062
echo $weather->temperature . "<br />\n";
63+
echo "<br />\n";
64+
echo "Sun rise: " . $weather->sun->rise->format('d.m.Y H:i (e)');
65+
echo "<br />\n";
6166
echo "---<br />\n";
6267
}

0 commit comments

Comments
 (0)