Skip to content

Commit 219469e

Browse files
authored
Add UTC timezone to all \DateTime instances (#120)
1 parent 9222160 commit 219469e

File tree

6 files changed

+27
-19
lines changed

6 files changed

+27
-19
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
/vendor
2-
.vagrant/
3-
.idea/
4-
ubuntu-*.log
2+
/.vagrant/
3+
/ubuntu-xenial-16.04-cloudimg-console.log

Cmfcmf/OpenWeatherMap/Forecast.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Forecast extends CurrentWeather
4343
*/
4444
public function __construct(\SimpleXMLElement $xml, $units)
4545
{
46+
$utctz = new \DateTimeZone('UTC');
47+
4648
if ($units == 'metric') {
4749
$temperatureUnit = "°C";
4850
} else {
@@ -69,7 +71,7 @@ public function __construct(\SimpleXMLElement $xml, $units)
6971
$this->clouds = new Unit($xml->clouds['all'], $xml->clouds['unit'], $xml->clouds['value']);
7072
$this->precipitation = new Unit($xml->precipitation['value'], null, $xml->precipitation['type']);
7173
$this->weather = new Weather($xml->symbol['number'], $xml->symbol['name'], $xml->symbol['var']);
72-
$this->lastUpdate = new \DateTime($xml->lastupdate['value']);
74+
$this->lastUpdate = new \DateTime($xml->lastupdate['value'], $utctz);
7375

7476
if (isset($xml['from'])) {
7577
$this->time = new Time($xml['from'], $xml['to']);

Cmfcmf/OpenWeatherMap/UVIndex.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class UVIndex
4848
*/
4949
public function __construct($data)
5050
{
51-
$this->time = new \DateTime($data->time);
51+
$utctz = new \DateTimeZone('UTC');
52+
$this->time = new \DateTime($data->time, $utctz);
5253
$this->location = new Location($data->location->latitude, $data->location->longitude);
5354
$this->uvIndex = (float)$data->data;
5455
}

Cmfcmf/OpenWeatherMap/Util/Time.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ class Time
4747
*/
4848
public function __construct($from, $to = null)
4949
{
50+
$utctz = new \DateTimeZone('UTC');
5051
if (isset($to)) {
51-
$from = ($from instanceof \DateTime) ? $from : new \DateTime((string)$from);
52-
$to = ($to instanceof \DateTime) ? $to : new \DateTime((string)$to);
53-
$day = new \DateTime($from->format('Y-m-d'));
52+
$from = ($from instanceof \DateTime) ? $from : new \DateTime((string)$from, $utctz);
53+
$to = ($to instanceof \DateTime) ? $to : new \DateTime((string)$to, $utctz);
54+
$day = new \DateTime($from->format('Y-m-d'), $utctz);
5455
} else {
55-
$from = ($from instanceof \DateTime) ? $from : new \DateTime((string)$from);
56-
$day = $from = new \DateTime($from->format('Y-m-d'));
56+
$from = ($from instanceof \DateTime) ? $from : new \DateTime((string)$from, $utctz);
57+
$day = $from = new \DateTime($from->format('Y-m-d'), $utctz);
5758
$to = clone $from;
5859
$to = $to->add(new \DateInterval('PT23H59M59S'));
5960
}

Cmfcmf/OpenWeatherMap/WeatherForecast.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ public function __construct($xml, $units, $days)
7676
$this->city = new City($xml->location->location['geobaseid'], $xml->location->name, $xml->location->location['latitude'], $xml->location->location['longitude'], $xml->location->country);
7777
$utctz = new \DateTimeZone('UTC');
7878
$this->sun = new Sun(new \DateTime($xml->sun['rise'], $utctz), new \DateTime($xml->sun['set'], $utctz));
79-
$this->lastUpdate = new \DateTime($xml->meta->lastupdate);
79+
$this->lastUpdate = new \DateTime($xml->meta->lastupdate, $utctz);
8080

81-
$today = new \DateTime();
81+
$today = new \DateTime('now', $utctz);
8282
$today->setTime(0, 0, 0);
8383
$counter = 0;
8484
foreach ($xml->forecast->time as $time) {
85-
$date = new \DateTime(isset($time['day']) ? $time['day'] : $time['to']);
85+
$date = new \DateTime(isset($time['day']) ? $time['day'] : $time['to'], $utctz);
8686
if ($date < $today) {
8787
// Sometimes OpenWeatherMap returns results which aren't real
8888
// forecasts. The best we can do is to ignore them.

tests/Util/TimeTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818

1919
class TimeTest extends \PHPUnit_Framework_TestCase
2020
{
21+
private function createDateTime($time)
22+
{
23+
return new \DateTime($time, new \DateTimeZone('UTC'));
24+
}
25+
2126
public function testFromTo()
2227
{
2328
$fromS = '2014-01-01 08:00:00';
2429
$toS = '2014-01-01 20:00:00';
25-
$from = new \DateTime($fromS);
26-
$to = new \DateTime($toS);
27-
$day = new \DateTime('2014-01-01');
30+
$from = $this->createDateTime($fromS);
31+
$to = $this->createDateTime($toS);
32+
$day = $this->createDateTime('2014-01-01');
2833

2934
$time = new Time($from, $to);
3035
$this->assertSame($from->format('c'), $time->from->format('c'));
@@ -39,9 +44,9 @@ public function testFromTo()
3944
public function testFrom()
4045
{
4146
$fromS = '2014-01-01 00:00:00';
42-
$from = new \DateTime($fromS);
43-
$day = new \DateTime('2014-01-01');
44-
$to = new \DateTime('2014-01-01 23:59:59');
47+
$from = $this->createDateTime($fromS);
48+
$day = $this->createDateTime('2014-01-01');
49+
$to = $this->createDateTime('2014-01-01 23:59:59');
4550

4651
$time = new Time($from);
4752
$this->assertSame($from->format('c'), $time->from->format('c'));

0 commit comments

Comments
 (0)