@@ -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}
0 commit comments