@@ -43,25 +43,48 @@ class City extends Location
4343 */
4444 public $ population ;
4545
46+ /**
47+ * @var \DateTimeZone|null The shift in seconds from UTC
48+ */
49+ public $ timezone ;
50+
4651 /**
4752 * Create a new city object.
4853 *
49- * @param int $id The city id.
50- * @param string $name The name of the city.
51- * @param float $lat The latitude of the city.
52- * @param float $lon The longitude of the city.
53- * @param string $country The abbreviation of the country the city is located in
54- * @param int $population The city's population.
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.
5561 *
5662 * @internal
5763 */
58- public function __construct ($ id , $ name = null , $ lat = null , $ lon = null , $ country = null , $ population = null )
64+ public function __construct ($ id , $ name = null , $ lat = null , $ lon = null , $ country = null , $ population = null , $ timezoneOffset = null )
5965 {
6066 $ this ->id = (int )$ id ;
6167 $ this ->name = isset ($ name ) ? (string )$ name : null ;
6268 $ this ->country = isset ($ country ) ? (string )$ country : null ;
6369 $ this ->population = isset ($ population ) ? (int )$ population : null ;
70+ $ this ->timezone = isset ($ timezoneOffset ) ? new \DateTimeZone (self ::timezoneOffsetInSecondsToHours ($ timezoneOffset )) : null ;
6471
6572 parent ::__construct ($ lat , $ lon );
6673 }
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+ }
6790}
0 commit comments