From fb76ee46cc21cbd409695dcfebb282e91cebdff9 Mon Sep 17 00:00:00 2001 From: bishop Date: Wed, 19 Aug 2015 14:45:59 -0400 Subject: [PATCH 1/3] Adds time zone support for provider methods returning DateTime instance --- src/Faker/Provider/DateTime.php | 44 +++++++++++++++++++++------- test/Faker/Provider/DateTimeTest.php | 15 ++++++++-- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/Faker/Provider/DateTime.php b/src/Faker/Provider/DateTime.php index 37e1180e16..a682972484 100644 --- a/src/Faker/Provider/DateTime.php +++ b/src/Faker/Provider/DateTime.php @@ -36,24 +36,34 @@ 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 'UTC' * @example DateTime('2005-08-16 20:39:21') * @return \DateTime + * @see http://php.net/manual/en/timezones.php */ - public static function dateTime($max = 'now') + public static function dateTime($max = 'now', $timezone = 'UTC') { - return new \DateTime('@' . static::unixTime($max)); + return static::setTimezone( + new \DateTime('@' . static::unixTime($max)), + $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 'UTC' * @example DateTime('1265-03-22 21:15:52') * @return \DateTime + * @see http://php.net/manual/en/timezones.php */ - public static function dateTimeAD($max = 'now') + public static function dateTimeAD($max = 'now', $timezone = 'UTC') { - return new \DateTime('@' . mt_rand(-62135597361, static::getMaxTimestamp($max))); + return static::setTimezone( + new \DateTime('@' . mt_rand(-62135597361, static::getMaxTimestamp($max))), + $timezone + ); } /** @@ -100,10 +110,12 @@ 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 'UTC' * @example DateTime('1999-02-02 11:42:52') * @return \DateTime + * @see http://php.net/manual/en/timezones.php */ - public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now') + public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = 'UTC') { $startTimestamp = $startDate instanceof \DateTime ? $startDate->getTimestamp() : strtotime($startDate); $endTimestamp = static::getMaxTimestamp($endDate); @@ -114,10 +126,10 @@ public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now $timestamp = mt_rand($startTimestamp, $endTimestamp); - $ts = new \DateTime('@' . $timestamp); - $ts->setTimezone(new \DateTimeZone(date_default_timezone_get())); - - return $ts; + return static::setTimezone( + new \DateTime('@' . $timestamp), + $timezone + ); } /** @@ -127,10 +139,12 @@ 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 'UTC' * @example dateTimeInInterval('1999-02-02 11:42:52', '+ 5 days') * @return \DateTime + * @see http://php.net/manual/en/timezones.php */ - public static function dateTimeInInterval($date = '-30 years', $interval = '+5 days') + public static function dateTimeInInterval($date = '-30 years', $interval = '+5 days', $timezone = 'UTC') { $intervalObject = \DateInterval::createFromDateString($interval); $datetime = $date instanceof \DateTime ? $date : new \DateTime($date); @@ -140,7 +154,7 @@ public static function dateTimeInInterval($date = '-30 years', $interval = '+5 d $begin = $datetime > $otherDatetime ? $otherDatetime : $datetime; $end = $datetime===$begin ? $otherDatetime : $datetime; - return static::dateTimeBetween($begin, $end); + return static::dateTimeBetween($begin, $end, $timezone); } /** @@ -260,4 +274,12 @@ public static function timezone() { return static::randomElement(\DateTimeZone::listIdentifiers()); } + + /** + * Internal method to set the time zone on a DateTime. + */ + private static function setTimezone(\DateTime $dt, $timezone) + { + return $dt->setTimezone(new \DateTimeZone($timezone)); + } } diff --git a/test/Faker/Provider/DateTimeTest.php b/test/Faker/Provider/DateTimeTest.php index 667dd8b069..2c1f8f8630 100644 --- a/test/Faker/Provider/DateTimeTest.php +++ b/test/Faker/Provider/DateTimeTest.php @@ -20,6 +20,13 @@ public function testDateTime() $this->assertInstanceOf('\DateTime', $date); $this->assertGreaterThanOrEqual(new \DateTime('@0'), $date); $this->assertLessThanOrEqual(new \DateTime(), $date); + $this->assertEquals($date->getTimezone(), new \DateTimeZone('UTC')); + } + + public function testDateTimeWithTimezone() + { + $date = DateTimeProvider::dateTime('now', 'America/New_York'); + $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York')); } public function testDateTimeAD() @@ -28,6 +35,7 @@ public function testDateTimeAD() $this->assertInstanceOf('\DateTime', $date); $this->assertGreaterThanOrEqual(new \DateTime('0000-01-01 00:00:00'), $date); $this->assertLessThanOrEqual(new \DateTime(), $date); + $this->assertEquals($date->getTimezone(), new \DateTimeZone('UTC')); } public function testIso8601() @@ -62,6 +70,7 @@ public function testDateTimeBetween($start, $end) $this->assertInstanceOf('\DateTime', $date); $this->assertGreaterThanOrEqual(new \DateTime($start), $date); $this->assertLessThanOrEqual(new \DateTime($end), $date); + $this->assertEquals($date->getTimezone(), new \DateTimeZone('UTC')); } public function providerDateTimeBetween() @@ -78,17 +87,17 @@ public function providerDateTimeBetween() * * @dataProvider providerDateTimeInInterval */ - public function testDateTimeInInterval($start, $interval = "+5 days", $isInFutur) + public function testDateTimeInInterval($start, $interval = "+5 days", $isInFuture) { $date = DateTimeProvider::dateTimeInInterval($start, $interval); $this->assertInstanceOf('\DateTime', $date); $_interval = \DateInterval::createFromDateString($interval); $_start = new \DateTime($start); - if($isInFutur){ + if ($isInFuture) { $this->assertGreaterThanOrEqual($_start, $date); $this->assertLessThanOrEqual($_start->add($_interval), $date); - }else{ + } else { $this->assertLessThanOrEqual($_start, $date); $this->assertGreaterThanOrEqual($_start->add($_interval), $date); } From ccc37a3f15b9bc3b4bec18e5d866dd06c852e7ba Mon Sep 17 00:00:00 2001 From: Bishop Bettini Date: Sat, 27 Feb 2016 18:57:00 -0500 Subject: [PATCH 2/3] Addresses BC break mentioned by @fzaninotto. Now defaults timezone to result of date_default_timezone_get() for all methods which return a \DateTime. --- src/Faker/Provider/DateTime.php | 32 +++++++++++++++++----------- test/Faker/Provider/DateTimeTest.php | 18 +++++++++++++--- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/Faker/Provider/DateTime.php b/src/Faker/Provider/DateTime.php index a682972484..3e0377f2f1 100644 --- a/src/Faker/Provider/DateTime.php +++ b/src/Faker/Provider/DateTime.php @@ -36,16 +36,17 @@ 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 'UTC' + * @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get` * @example DateTime('2005-08-16 20:39:21') * @return \DateTime * @see http://php.net/manual/en/timezones.php + * @see http://php.net/manual/en/function.date-default-timezone-get.php */ - public static function dateTime($max = 'now', $timezone = 'UTC') + public static function dateTime($max = 'now', $timezone = null) { return static::setTimezone( new \DateTime('@' . static::unixTime($max)), - $timezone + (null === $timezone ? date_default_timezone_get() : $timezone) ); } @@ -53,16 +54,17 @@ public static function dateTime($max = 'now', $timezone = 'UTC') * 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 'UTC' + * @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get` * @example DateTime('1265-03-22 21:15:52') * @return \DateTime * @see http://php.net/manual/en/timezones.php + * @see http://php.net/manual/en/function.date-default-timezone-get.php */ - public static function dateTimeAD($max = 'now', $timezone = 'UTC') + public static function dateTimeAD($max = 'now', $timezone = null) { return static::setTimezone( new \DateTime('@' . mt_rand(-62135597361, static::getMaxTimestamp($max))), - $timezone + (null === $timezone ? date_default_timezone_get() : $timezone) ); } @@ -110,12 +112,13 @@ 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 'UTC' + * @param string $timezone time zone in which the date time should be set, default to result of `date_default_timezone_get` * @example DateTime('1999-02-02 11:42:52') * @return \DateTime * @see http://php.net/manual/en/timezones.php + * @see http://php.net/manual/en/function.date-default-timezone-get.php */ - public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = 'UTC') + public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = null) { $startTimestamp = $startDate instanceof \DateTime ? $startDate->getTimestamp() : strtotime($startDate); $endTimestamp = static::getMaxTimestamp($endDate); @@ -128,7 +131,7 @@ public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now return static::setTimezone( new \DateTime('@' . $timestamp), - $timezone + (null === $timezone ? date_default_timezone_get() : $timezone) ); } @@ -139,12 +142,13 @@ 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 'UTC' + * @param string $timezone time zone in which the date time should be set, default to 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 + * @see http://php.net/manual/en/function.date-default-timezone-get.php */ - public static function dateTimeInInterval($date = '-30 years', $interval = '+5 days', $timezone = 'UTC') + public static function dateTimeInInterval($date = '-30 years', $interval = '+5 days', $timezone = null) { $intervalObject = \DateInterval::createFromDateString($interval); $datetime = $date instanceof \DateTime ? $date : new \DateTime($date); @@ -154,7 +158,11 @@ public static function dateTimeInInterval($date = '-30 years', $interval = '+5 d $begin = $datetime > $otherDatetime ? $otherDatetime : $datetime; $end = $datetime===$begin ? $otherDatetime : $datetime; - return static::dateTimeBetween($begin, $end, $timezone); + return static::dateTimeBetween( + $begin, + $end, + (null === $timezone ? date_default_timezone_get() : $timezone) + ); } /** diff --git a/test/Faker/Provider/DateTimeTest.php b/test/Faker/Provider/DateTimeTest.php index 2c1f8f8630..252662b062 100644 --- a/test/Faker/Provider/DateTimeTest.php +++ b/test/Faker/Provider/DateTimeTest.php @@ -6,6 +6,18 @@ class DateTimeTest extends \PHPUnit_Framework_TestCase { + public function setUp() + { + $this->originalTz = date_default_timezone_get(); + $this->defaultTz = 'UTC'; + date_default_timezone_set($this->defaultTz); + } + + public function tearDown() + { + date_default_timezone_set($this->originalTz); + } + public function testUnixTime() { $timestamp = DateTimeProvider::unixTime(); @@ -20,7 +32,7 @@ public function testDateTime() $this->assertInstanceOf('\DateTime', $date); $this->assertGreaterThanOrEqual(new \DateTime('@0'), $date); $this->assertLessThanOrEqual(new \DateTime(), $date); - $this->assertEquals($date->getTimezone(), new \DateTimeZone('UTC')); + $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone()); } public function testDateTimeWithTimezone() @@ -35,7 +47,7 @@ public function testDateTimeAD() $this->assertInstanceOf('\DateTime', $date); $this->assertGreaterThanOrEqual(new \DateTime('0000-01-01 00:00:00'), $date); $this->assertLessThanOrEqual(new \DateTime(), $date); - $this->assertEquals($date->getTimezone(), new \DateTimeZone('UTC')); + $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone()); } public function testIso8601() @@ -70,7 +82,7 @@ public function testDateTimeBetween($start, $end) $this->assertInstanceOf('\DateTime', $date); $this->assertGreaterThanOrEqual(new \DateTime($start), $date); $this->assertLessThanOrEqual(new \DateTime($end), $date); - $this->assertEquals($date->getTimezone(), new \DateTimeZone('UTC')); + $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone()); } public function providerDateTimeBetween() From edcad4f047ad7eeea3a7f86bc647331af24c8f7f Mon Sep 17 00:00:00 2001 From: Bishop Bettini Date: Mon, 29 Feb 2016 09:31:41 -0500 Subject: [PATCH 3/3] Updates readme per comment from @fzaninotto --- readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 3ee496c7ed..7af97b07ee 100644 --- a/readme.md +++ b/readme.md @@ -180,13 +180,13 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle ### `Faker\Provider\DateTime` unixTime($max = 'now') // 58781813 - dateTime($max = 'now') // DateTime('2008-04-25 08:37:17') - dateTimeAD($max = 'now') // DateTime('1800-04-29 20:38:49') + 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') 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') // DateTime('2003-03-15 02:00:49') - dateTimeInInterval($startDate = '-30 years', $interval = '+ 5 days') // DateTime('2003-03-15 02:00:49') + 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') // DateTime('1915-05-30 19:28:21') dateTimeThisDecade($max = 'now') // DateTime('2007-05-29 22:30:48') dateTimeThisYear($max = 'now') // DateTime('2011-02-27 20:52:14')