Skip to content

Commit

Permalink
Support filtering timezones by country code (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
aprat84 authored Jun 22, 2022
1 parent 9bd9d03 commit 135f6e9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/Faker/Core/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 3 additions & 1 deletion src/Faker/Extension/DateTimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
*
* @property string $timezone
*
* @method string timezone()
* @method string timezone($countryCode = null)
*
* @property void $setDefaultTimezone
*
Expand Down
3 changes: 3 additions & 0 deletions test/Faker/Core/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit 135f6e9

Please sign in to comment.