Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Introduced the ability to specify a default timezone... #1316

Merged
merged 2 commits into from
Nov 30, 2017
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
18 changes: 10 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,17 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
### `Faker\Provider\DateTime`

unixTime($max = 'now') // 58781813
dateTime($max = 'now', $timezone = date_default_timezone_get()) // DateTime('2008-04-25 08:37:17', 'UTC')
dateTimeAD($max = 'now', $timezone = date_default_timezone_get()) // DateTime('1800-04-29 20:38:49', 'Europe/Paris')
dateTime($max = 'now', $timezone = null) // DateTime('2008-04-25 08:37:17', 'UTC')
dateTimeAD($max = 'now', $timezone = null) // DateTime('1800-04-29 20:38:49', 'Europe/Paris')
iso8601($max = 'now') // '1978-12-09T10:10:29+0000'
date($format = 'Y-m-d', $max = 'now') // '1979-06-09'
time($format = 'H:i:s', $max = 'now') // '20:49:42'
dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = date_default_timezone_get()) // DateTime('2003-03-15 02:00:49', 'Africa/Lagos')
dateTimeInInterval($startDate = '-30 years', $interval = '+ 5 days', $timezone = date_default_timezone_get()) // DateTime('2003-03-15 02:00:49', 'Antartica/Vostok')
dateTimeThisCentury($max = 'now', $timezone = date_default_timezone_get()) // DateTime('1915-05-30 19:28:21', 'UTC')
dateTimeThisDecade($max = 'now', $timezone = date_default_timezone_get()) // DateTime('2007-05-29 22:30:48', 'Europe/Paris')
dateTimeThisYear($max = 'now', $timezone = date_default_timezone_get()) // DateTime('2011-02-27 20:52:14', 'Africa/Lagos')
dateTimeThisMonth($max = 'now', $timezone = date_default_timezone_get()) // DateTime('2011-10-23 13:46:23', 'Antarctica/Vostok')
dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = null) // DateTime('2003-03-15 02:00:49', 'Africa/Lagos')
dateTimeInInterval($startDate = '-30 years', $interval = '+ 5 days', $timezone = null) // DateTime('2003-03-15 02:00:49', 'Antartica/Vostok')
dateTimeThisCentury($max = 'now', $timezone = null) // DateTime('1915-05-30 19:28:21', 'UTC')
dateTimeThisDecade($max = 'now', $timezone = null) // DateTime('2007-05-29 22:30:48', 'Europe/Paris')
dateTimeThisYear($max = 'now', $timezone = null) // DateTime('2011-02-27 20:52:14', 'Africa/Lagos')
dateTimeThisMonth($max = 'now', $timezone = null) // DateTime('2011-10-23 13:46:23', 'Antarctica/Vostok')
amPm($max = 'now') // 'pm'
dayOfMonth($max = 'now') // '04'
dayOfWeek($max = 'now') // 'Friday'
Expand All @@ -200,6 +200,8 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
century // 'VI'
timezone // 'Europe/Paris'

Methods accepting a `$timezone` argument default to `date_default_timezone_get()`. You can pass a custom timezone string to each method, or define a custom timezone for all time methods at once using `$faker::setDefaultTimezone($timezone)`.

### `Faker\Provider\Internet`

email // 'tkshlerin@collins.com'
Expand Down
55 changes: 42 additions & 13 deletions src/Faker/Provider/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class DateTime extends Base
{
protected static $century = array('I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII','XIII','XIV','XV','XVI','XVII','XVIII','XIX','XX','XXI');

protected static $defaultTimezone = null;

protected static function getMaxTimestamp($max = 'now')
{
if (is_numeric($max)) {
Expand Down Expand Up @@ -36,7 +38,7 @@ public static function unixTime($max = 'now')
* Get a datetime object for a date between January 1, 1970 and now
*
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get`
* @param string $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
* @example DateTime('2005-08-16 20:39:21')
* @return \DateTime
* @see http://php.net/manual/en/timezones.php
Expand All @@ -46,15 +48,15 @@ public static function dateTime($max = 'now', $timezone = null)
{
return static::setTimezone(
new \DateTime('@' . static::unixTime($max)),
(null === $timezone ? date_default_timezone_get() : $timezone)
$timezone
);
}

/**
* Get a datetime object for a date between January 1, 001 and now
*
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get`
* @param string $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
* @example DateTime('1265-03-22 21:15:52')
* @return \DateTime
* @see http://php.net/manual/en/timezones.php
Expand All @@ -65,7 +67,7 @@ public static function dateTimeAD($max = 'now', $timezone = null)
$min = (PHP_INT_SIZE>4 ? -62135597361 : -PHP_INT_MAX);
return static::setTimezone(
new \DateTime('@' . mt_rand($min, static::getMaxTimestamp($max))),
(null === $timezone ? date_default_timezone_get() : $timezone)
$timezone
);
}

Expand Down Expand Up @@ -113,7 +115,7 @@ public static function time($format = 'H:i:s', $max = 'now')
*
* @param \DateTime|string $startDate Defaults to 30 years ago
* @param \DateTime|string $endDate Defaults to "now"
* @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get`
* @param string $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
* @example DateTime('1999-02-02 11:42:52')
* @return \DateTime
* @see http://php.net/manual/en/timezones.php
Expand All @@ -132,7 +134,7 @@ public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now

return static::setTimezone(
new \DateTime('@' . $timestamp),
(null === $timezone ? date_default_timezone_get() : $timezone)
$timezone
);
}

Expand All @@ -143,7 +145,7 @@ public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now
*
* @param string $date Defaults to 30 years ago
* @param string $interval Defaults to 5 days after
* @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get`
* @param string $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
* @example dateTimeInInterval('1999-02-02 11:42:52', '+ 5 days')
* @return \DateTime
* @see http://php.net/manual/en/timezones.php
Expand All @@ -162,13 +164,13 @@ public static function dateTimeInInterval($date = '-30 years', $interval = '+5 d
return static::dateTimeBetween(
$begin,
$end,
(null === $timezone ? date_default_timezone_get() : $timezone)
$timezone
);
}

/**
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get`
* @param string $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
* @example DateTime('1964-04-04 11:02:02')
* @return \DateTime
*/
Expand All @@ -179,7 +181,7 @@ public static function dateTimeThisCentury($max = 'now', $timezone = null)

/**
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get`
* @param string $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
* @example DateTime('2010-03-10 05:18:58')
* @return \DateTime
*/
Expand All @@ -190,7 +192,7 @@ public static function dateTimeThisDecade($max = 'now', $timezone = null)

/**
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get`
* @param string $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
* @example DateTime('2011-09-19 09:24:37')
* @return \DateTime
*/
Expand All @@ -201,7 +203,7 @@ public static function dateTimeThisYear($max = 'now', $timezone = null)

/**
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get`
* @param string $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
* @example DateTime('2011-10-05 12:51:46')
* @return \DateTime
*/
Expand Down Expand Up @@ -298,6 +300,33 @@ public static function timezone()
*/
private static function setTimezone(\DateTime $dt, $timezone)
{
return $dt->setTimezone(new \DateTimeZone($timezone));
return $dt->setTimezone(new \DateTimeZone(static::resolveTimezone($timezone)));
}

/**
* Sets default time zone.
*
* @param string $timezone
*
* @return void
*/
public static function setDefaultTimezone($timezone = null)
{
static::$defaultTimezone = $timezone;
}

/**
* Gets default time zone.
*
* @return string
*/
public static function getDefaultTimezone()
{
return static::$defaultTimezone;
}

private static function resolveTimezone($timezone)
{
return ((null === $timezone) ? ((null === static::$defaultTimezone) ? date_default_timezone_get() : static::$defaultTimezone) : $timezone);
}
}
55 changes: 52 additions & 3 deletions test/Faker/Provider/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,63 @@ class DateTimeTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->originalTz = date_default_timezone_get();
$this->defaultTz = 'UTC';
date_default_timezone_set($this->defaultTz);
DateTimeProvider::setDefaultTimezone($this->defaultTz);
}

public function tearDown()
{
date_default_timezone_set($this->originalTz);
DateTimeProvider::setDefaultTimezone();
}

public function testPreferDefaultTimezoneOverSystemTimezone()
{
/**
* Set the system timezone to something *other* than the timezone used
* in setUp().
*/
$originalSystemTimezone = date_default_timezone_get();
$systemTimezone = 'Antarctica/Vostok';
date_default_timezone_set($systemTimezone);

/**
* Get a new date/time value and assert that it prefers the default
* timezone over the system timezone.
*/
$date = DateTimeProvider::dateTime();
$this->assertNotSame($systemTimezone, $date->getTimezone()->getName());
$this->assertSame($this->defaultTz, $date->getTimezone()->getName());

/**
* Restore the system timezone.
*/
date_default_timezone_set($originalSystemTimezone);
}

public function testUseSystemTimezoneWhenDefaultTimezoneIsNotSet()
{
/**
* Set the system timezone to something *other* than the timezone used
* in setUp() *and* reset the default timezone.
*/
$originalSystemTimezone = date_default_timezone_get();
$originalDefaultTimezone = DateTimeProvider::getDefaultTimezone();
$systemTimezone = 'Antarctica/Vostok';
date_default_timezone_set($systemTimezone);
DateTimeProvider::setDefaultTimezone();

/**
* Get a new date/time value and assert that it uses the system timezone
* and not the system timezone.
*/
$date = DateTimeProvider::dateTime();
$this->assertSame($systemTimezone, $date->getTimezone()->getName());
$this->assertNotSame($this->defaultTz, $date->getTimezone()->getName());

/**
* Restore the system timezone.
*/
date_default_timezone_set($originalSystemTimezone);
}

public function testUnixTime()
Expand Down
7 changes: 2 additions & 5 deletions test/Faker/Provider/ro_RO/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,19 @@ class PersonTest extends \PHPUnit_Framework_TestCase
*
*/
protected $faker;
protected $originalTz;

public function setUp()
{
$this->originalTz = @date_default_timezone_get();
date_default_timezone_set('Europe/Bucharest');

$faker = new Generator();
$faker->addProvider(new DateTime($faker));
$faker->addProvider(new Person($faker));
$faker->setDefaultTimezone('Europe/Bucharest');
$this->faker = $faker;
}

public function tearDown()
{
date_default_timezone_set($this->originalTz);
$this->faker->setDefaultTimezone();
}

public function invalidGenderProvider()
Expand Down