@@ -44,30 +44,47 @@ class City extends Location
44
44
public $ population ;
45
45
46
46
/**
47
- * @var int The shift in seconds from UTC
47
+ * @var \DateTimeZone|null The shift in seconds from UTC
48
48
*/
49
49
public $ timezone ;
50
+
50
51
/**
51
52
* Create a new city object.
52
53
*
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.
60
61
*
61
62
* @internal
62
63
*/
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 )
64
65
{
65
66
$ this ->id = (int )$ id ;
66
67
$ this ->name = isset ($ name ) ? (string )$ name : null ;
67
68
$ this ->country = isset ($ country ) ? (string )$ country : null ;
68
69
$ 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 ;
70
71
71
72
parent ::__construct ($ lat , $ lon );
72
73
}
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
+ }
73
90
}
0 commit comments