Skip to content

Commit 98b173f

Browse files
committed
Transform timezone offsets into DateTimeZones
1 parent f5bc322 commit 98b173f

File tree

3 files changed

+81
-11
lines changed

3 files changed

+81
-11
lines changed

Cmfcmf/OpenWeatherMap/Util/City.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,47 @@ class City extends Location
4444
public $population;
4545

4646
/**
47-
* @var int The shift in seconds from UTC
47+
* @var \DateTimeZone|null The shift in seconds from UTC
4848
*/
4949
public $timezone;
50+
5051
/**
5152
* Create a new city object.
5253
*
53-
* @param int $id The city id.
54-
* @param string $name The name of the city.
55-
* @param float $lat The latitude of the city.
56-
* @param float $lon The longitude of the city.
57-
* @param string $country The abbreviation of the country the city is located in
58-
* @param int $population The city's population.
59-
* @param int $timezone The shift in seconds from UTC.
54+
* @param int $id The city id.
55+
* @param string $name The name of the city.
56+
* @param float $lat The latitude of the city.
57+
* @param float $lon The longitude of the city.
58+
* @param string $country The abbreviation of the country the city is located in
59+
* @param int $population The city's population.
60+
* @param int $timezoneOffset The shift in seconds from UTC.
6061
*
6162
* @internal
6263
*/
63-
public function __construct($id, $name = null, $lat = null, $lon = null, $country = null, $population = null, $timezone = null)
64+
public function __construct($id, $name = null, $lat = null, $lon = null, $country = null, $population = null, $timezoneOffset = null)
6465
{
6566
$this->id = (int)$id;
6667
$this->name = isset($name) ? (string)$name : null;
6768
$this->country = isset($country) ? (string)$country : null;
6869
$this->population = isset($population) ? (int)$population : null;
69-
$this->timezone = isset($timezone) ? (int)$timezone : null;
70+
$this->timezone = isset($timezoneOffset) ? new \DateTimeZone(self::timezoneOffsetInSecondsToHours($timezoneOffset)) : null;
7071

7172
parent::__construct($lat, $lon);
7273
}
74+
75+
/**
76+
* @param int $offset The timezone offset in seconds from UTC.
77+
* @return int The timezone offset in +/-HH:MM form.
78+
*/
79+
private static function timezoneOffsetInSecondsToHours($offset)
80+
{
81+
$minutes = floor(abs($offset) / 60) % 60;
82+
$hours = floor(abs($offset) / 3600);
83+
84+
$result = $offset < 0 ? "-" : "+";
85+
$result .= str_pad($hours, 2, "0", STR_PAD_LEFT);
86+
$result .= str_pad($minutes, 2, "0", STR_PAD_LEFT);
87+
88+
return $result;
89+
}
7390
}

Examples/CurrentWeather.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
echo $lf;
146146

147147
// Example 6: Get information about a city.
148-
$weather = $owm->getWeather('Paris', $units, $lang);
148+
$weather = $owm->getWeather('Kathmandu', $units, $lang);
149149
echo "$lf$lf EXAMPLE 6$lf";
150150

151151
echo 'Id: '.$weather->city->id;
@@ -163,6 +163,9 @@
163163
echo 'Country: '.$weather->city->country;
164164
echo $lf;
165165

166+
echo 'Timezone offset to UTC: '.$weather->city->timezone->getOffset(new DateTime("now", new DateTimeZone("UTC")))." seconds";
167+
echo $lf;
168+
166169
// Example 7: Get wind information.
167170
echo "$lf$lf EXAMPLE 7$lf";
168171

tests/Util/CityTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5+
*
6+
* @license MIT
7+
*
8+
* Please see the LICENSE file distributed with this source code for further
9+
* information regarding copyright and licensing.
10+
*
11+
* Please visit the following links to read about the usage policies and the license of
12+
* OpenWeatherMap data before using this library:
13+
*
14+
* @see https://OpenWeatherMap.org/price
15+
* @see https://OpenWeatherMap.org/terms
16+
* @see https://OpenWeatherMap.org/appid
17+
*/
18+
19+
namespace Cmfcmf\OpenWeatherMap\Tests\Util;
20+
21+
use Cmfcmf\OpenWeatherMap\Util\City;
22+
23+
class CityTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @dataProvider timezoneDataProvider
27+
*/
28+
public function testTimezoneConversion($offsetString, $offsetSeconds)
29+
{
30+
$class = new \ReflectionClass(City::class);
31+
$method = $class->getMethod("timezoneOffsetInSecondsToHours");
32+
$method->setAccessible(true);
33+
34+
$this->assertSame($offsetString, $method->invoke(null, $offsetSeconds));
35+
$offsetTimezone = new \DateTimeZone($offsetString);
36+
$this->assertSame($offsetSeconds, $offsetTimezone->getOffset(new \DateTime("now", new \DateTimeZone("GMT"))));
37+
}
38+
39+
public function timezoneDataProvider()
40+
{
41+
return [
42+
["+0100", 3600],
43+
["+0100", 3600],
44+
["+0030", 1800],
45+
["+0015", 900],
46+
["+0000", 0],
47+
["+0545", 20700],
48+
];
49+
}
50+
}

0 commit comments

Comments
 (0)