diff --git a/readme.md b/readme.md index 70a59376f9..2a17932bbe 100644 --- a/readme.md +++ b/readme.md @@ -187,10 +187,10 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle 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') // 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') - dateTimeThisMonth($max = 'now') // DateTime('2011-10-23 13:46:23') + 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') amPm($max = 'now') // 'pm' dayOfMonth($max = 'now') // '04' dayOfWeek($max = 'now') // 'Friday' diff --git a/src/Faker/Provider/DateTime.php b/src/Faker/Provider/DateTime.php index bb13e06fd9..7b81ec9d14 100644 --- a/src/Faker/Provider/DateTime.php +++ b/src/Faker/Provider/DateTime.php @@ -168,42 +168,46 @@ public static function dateTimeInInterval($date = '-30 years', $interval = '+5 d /** * @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('1964-04-04 11:02:02') * @return \DateTime */ - public static function dateTimeThisCentury($max = 'now') + public static function dateTimeThisCentury($max = 'now', $timezone = null) { - return static::dateTimeBetween('-100 year', $max); + return static::dateTimeBetween('-100 year', $max, $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` * @example DateTime('2010-03-10 05:18:58') * @return \DateTime */ - public static function dateTimeThisDecade($max = 'now') + public static function dateTimeThisDecade($max = 'now', $timezone = null) { - return static::dateTimeBetween('-10 year', $max); + return static::dateTimeBetween('-10 year', $max, $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` * @example DateTime('2011-09-19 09:24:37') * @return \DateTime */ - public static function dateTimeThisYear($max = 'now') + public static function dateTimeThisYear($max = 'now', $timezone = null) { - return static::dateTimeBetween('-1 year', $max); + return static::dateTimeBetween('-1 year', $max, $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` * @example DateTime('2011-10-05 12:51:46') * @return \DateTime */ - public static function dateTimeThisMonth($max = 'now') + public static function dateTimeThisMonth($max = 'now', $timezone = null) { - return static::dateTimeBetween('-1 month', $max); + return static::dateTimeBetween('-1 month', $max, $timezone); } /** diff --git a/test/Faker/Provider/DateTimeTest.php b/test/Faker/Provider/DateTimeTest.php index 252662b062..91b17f818b 100644 --- a/test/Faker/Provider/DateTimeTest.php +++ b/test/Faker/Provider/DateTimeTest.php @@ -50,6 +50,66 @@ public function testDateTimeAD() $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone()); } + public function testDateTimeThisCentury() + { + $date = DateTimeProvider::dateTimeThisCentury(); + $this->assertInstanceOf('\DateTime', $date); + $this->assertGreaterThanOrEqual(new \DateTime('-100 year'), $date); + $this->assertLessThanOrEqual(new \DateTime(), $date); + $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone()); + } + + public function testDateTimeThisDecade() + { + $date = DateTimeProvider::dateTimeThisDecade(); + $this->assertInstanceOf('\DateTime', $date); + $this->assertGreaterThanOrEqual(new \DateTime('-10 year'), $date); + $this->assertLessThanOrEqual(new \DateTime(), $date); + $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone()); + } + + public function testDateTimeThisYear() + { + $date = DateTimeProvider::dateTimeThisYear(); + $this->assertInstanceOf('\DateTime', $date); + $this->assertGreaterThanOrEqual(new \DateTime('-1 year'), $date); + $this->assertLessThanOrEqual(new \DateTime(), $date); + $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone()); + } + + public function testDateTimeThisMonth() + { + $date = DateTimeProvider::dateTimeThisMonth(); + $this->assertInstanceOf('\DateTime', $date); + $this->assertGreaterThanOrEqual(new \DateTime('-1 month'), $date); + $this->assertLessThanOrEqual(new \DateTime(), $date); + $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone()); + } + + public function testDateTimeThisCenturyWithTimezone() + { + $date = DateTimeProvider::dateTimeThisCentury('now', 'America/New_York'); + $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York')); + } + + public function testDateTimeThisDecadeWithTimezone() + { + $date = DateTimeProvider::dateTimeThisDecade('now', 'America/New_York'); + $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York')); + } + + public function testDateTimeThisYearWithTimezone() + { + $date = DateTimeProvider::dateTimeThisYear('now', 'America/New_York'); + $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York')); + } + + public function testDateTimeThisMonthWithTimezone() + { + $date = DateTimeProvider::dateTimeThisMonth('now', 'America/New_York'); + $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York')); + } + public function testIso8601() { $date = DateTimeProvider::iso8601(); @@ -103,7 +163,7 @@ public function testDateTimeInInterval($start, $interval = "+5 days", $isInFutur { $date = DateTimeProvider::dateTimeInInterval($start, $interval); $this->assertInstanceOf('\DateTime', $date); - + $_interval = \DateInterval::createFromDateString($interval); $_start = new \DateTime($start); if ($isInFuture) {