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

Commit

Permalink
Merge pull request #675 from bishopb/master
Browse files Browse the repository at this point in the history
Adds time zone support for provider methods returning DateTime instance
  • Loading branch information
fzaninotto committed Apr 29, 2016
2 parents 2ad583f + edcad4f commit c392ecd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,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')
Expand Down
52 changes: 41 additions & 11 deletions src/Faker/Provider/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,36 @@ 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`
* @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')
public static function dateTime($max = 'now', $timezone = null)
{
return new \DateTime('@' . static::unixTime($max));
return static::setTimezone(
new \DateTime('@' . static::unixTime($max)),
(null === $timezone ? date_default_timezone_get() : $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`
* @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')
public static function dateTimeAD($max = 'now', $timezone = null)
{
return new \DateTime('@' . mt_rand(-62135597361, static::getMaxTimestamp($max)));
return static::setTimezone(
new \DateTime('@' . mt_rand(-62135597361, static::getMaxTimestamp($max))),
(null === $timezone ? date_default_timezone_get() : $timezone)
);
}

/**
Expand Down Expand Up @@ -100,10 +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 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')
public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = null)
{
$startTimestamp = $startDate instanceof \DateTime ? $startDate->getTimestamp() : strtotime($startDate);
$endTimestamp = static::getMaxTimestamp($endDate);
Expand All @@ -114,10 +129,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),
(null === $timezone ? date_default_timezone_get() : $timezone)
);
}

/**
Expand All @@ -127,10 +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 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')
public static function dateTimeInInterval($date = '-30 years', $interval = '+5 days', $timezone = null)
{
$intervalObject = \DateInterval::createFromDateString($interval);
$datetime = $date instanceof \DateTime ? $date : new \DateTime($date);
Expand All @@ -140,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);
return static::dateTimeBetween(
$begin,
$end,
(null === $timezone ? date_default_timezone_get() : $timezone)
);
}

/**
Expand Down Expand Up @@ -260,4 +282,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));
}
}
27 changes: 24 additions & 3 deletions test/Faker/Provider/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -20,6 +32,13 @@ public function testDateTime()
$this->assertInstanceOf('\DateTime', $date);
$this->assertGreaterThanOrEqual(new \DateTime('@0'), $date);
$this->assertLessThanOrEqual(new \DateTime(), $date);
$this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
}

public function testDateTimeWithTimezone()
{
$date = DateTimeProvider::dateTime('now', 'America/New_York');
$this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
}

public function testDateTimeAD()
Expand All @@ -28,6 +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(new \DateTimeZone($this->defaultTz), $date->getTimezone());
}

public function testIso8601()
Expand Down Expand Up @@ -62,6 +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(new \DateTimeZone($this->defaultTz), $date->getTimezone());
}

public function providerDateTimeBetween()
Expand All @@ -78,17 +99,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);
}
Expand Down

0 comments on commit c392ecd

Please sign in to comment.