Skip to content

Commit 135f6e9

Browse files
authored
Support filtering timezones by country code (#480)
1 parent 9bd9d03 commit 135f6e9

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

Diff for: src/Faker/Core/DateTime.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,14 @@ public function century(): string
217217
return Helper::randomElement($this->centuries);
218218
}
219219

220-
public function timezone(): string
220+
public function timezone(string $countryCode = null): string
221221
{
222-
return Helper::randomElement(\DateTimeZone::listIdentifiers());
222+
if ($countryCode) {
223+
$timezones = \DateTimeZone::listIdentifiers(\DateTimeZone::PER_COUNTRY, $countryCode);
224+
} else {
225+
$timezones = \DateTimeZone::listIdentifiers();
226+
}
227+
228+
return Helper::randomElement($timezones);
223229
}
224230
}

Diff for: src/Faker/Extension/DateTimeExtension.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ public function century(): string;
234234
/**
235235
* Get a random timezone, uses `\DateTimeZone::listIdentifiers()` internally.
236236
*
237+
* @param string|null $countryCode two-letter ISO 3166-1 compatible country code
238+
*
237239
* @example 'Europe/Rome'
238240
*/
239-
public function timezone(): string;
241+
public function timezone(string $countryCode = null): string;
240242
}

Diff for: src/Faker/Generator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@
255255
*
256256
* @property string $timezone
257257
*
258-
* @method string timezone()
258+
* @method string timezone($countryCode = null)
259259
*
260260
* @property void $setDefaultTimezone
261261
*

Diff for: test/Faker/Core/DateTimeTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,11 @@ public function testCentury()
221221
public function testTimezone()
222222
{
223223
$timezone = $this->extension->timezone();
224+
$countryTimezone = $this->extension->timezone('US');
224225

225226
self::assertIsString($timezone);
226227
self::assertContains($timezone, \DateTimeZone::listIdentifiers());
228+
self::assertIsString($countryTimezone);
229+
self::assertContains($countryTimezone, \DateTimeZone::listIdentifiers(\DateTimeZone::PER_COUNTRY, 'US'));
227230
}
228231
}

0 commit comments

Comments
 (0)