Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
}
}