diff --git a/src/Faker/Core/DateTime.php b/src/Faker/Core/DateTime.php index f14b540f3c..1487cfdb1f 100644 --- a/src/Faker/Core/DateTime.php +++ b/src/Faker/Core/DateTime.php @@ -217,8 +217,14 @@ public function century(): string return Helper::randomElement($this->centuries); } - public function timezone(): string + public function timezone(string $countryCode = null): string { - return Helper::randomElement(\DateTimeZone::listIdentifiers()); + if ($countryCode) { + $timezones = \DateTimeZone::listIdentifiers(\DateTimeZone::PER_COUNTRY, $countryCode); + } else { + $timezones = \DateTimeZone::listIdentifiers(); + } + + return Helper::randomElement($timezones); } } diff --git a/src/Faker/Extension/DateTimeExtension.php b/src/Faker/Extension/DateTimeExtension.php index 4cd6c38655..9a27cce0e7 100644 --- a/src/Faker/Extension/DateTimeExtension.php +++ b/src/Faker/Extension/DateTimeExtension.php @@ -234,7 +234,9 @@ public function century(): string; /** * Get a random timezone, uses `\DateTimeZone::listIdentifiers()` internally. * + * @param string|null $countryCode two-letter ISO 3166-1 compatible country code + * * @example 'Europe/Rome' */ - public function timezone(): string; + public function timezone(string $countryCode = null): string; } diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index ce93aea280..758399a3e6 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -255,7 +255,7 @@ * * @property string $timezone * - * @method string timezone() + * @method string timezone($countryCode = null) * * @property void $setDefaultTimezone * diff --git a/test/Faker/Core/DateTimeTest.php b/test/Faker/Core/DateTimeTest.php index 4b4e476bbb..4b7da9fd76 100644 --- a/test/Faker/Core/DateTimeTest.php +++ b/test/Faker/Core/DateTimeTest.php @@ -221,8 +221,11 @@ public function testCentury() public function testTimezone() { $timezone = $this->extension->timezone(); + $countryTimezone = $this->extension->timezone('US'); self::assertIsString($timezone); self::assertContains($timezone, \DateTimeZone::listIdentifiers()); + self::assertIsString($countryTimezone); + self::assertContains($countryTimezone, \DateTimeZone::listIdentifiers(\DateTimeZone::PER_COUNTRY, 'US')); } }