From 5bf056fd3f913d040f132661cd64fddb5ac0ace6 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Tue, 23 Apr 2013 10:41:47 +0200 Subject: [PATCH] Added timezone parameter to now() and none() --- lib/datetime.php | 55 +++++++++++++++++++++++++++++++++++------- tests/DateTimeTest.php | 39 ++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/lib/datetime.php b/lib/datetime.php index 2cdf9f1..fd26a1a 100644 --- a/lib/datetime.php +++ b/lib/datetime.php @@ -187,11 +187,23 @@ class DateTime extends \DateTime /** * Creates a {@link DateTime} instance from a source. * + *
+	 * 
+	 *
 	 * @param mixed $source
+	 * @param mixed $timezone The time zone to use to create the time. The value is ignored if the
+	 * source is an instance of {@link \DateTime}.
 	 *
 	 * @return \ICanBoogie\DateTime
 	 */
-	static public function from($source)
+	static public function from($source, $timezone=null)
 	{
 		if ($source instanceof self)
 		{
@@ -199,14 +211,14 @@ static public function from($source)
 		}
 		else if ($source instanceof \DateTime)
 		{
-			return new static($source->getTimestamp(), $source->getTimezone());
+			return new static($source->format(self::DB), $source->getTimezone());
 		}
 
-		return new static($source);
+		return new static($source, $timezone);
 	}
 
 	/**
-	 * Returns an instance with the current time and the local time zone.
+	 * Returns an instance with the current local time and the local time zone.
 	 *
 	 * @return \ICanBoogie\DateTime
 	 */
@@ -218,20 +230,45 @@ static public function now()
 	/**
 	 * Returns an instance representing an empty date ("0000-00-00").
 	 *
-	 * The instance is created in the "UTC" time zone.
+	 * 
+	 * is_empty;                      // true
+	 * $d->zone->name;                    // "UTC"
+	 *
+	 * $d = DateTime::none('Asia/Tokyo');
+	 * $d->is_empty;                      // true
+	 * $d->zone->name;                    // "Asia/Tokio"
+	 * 
+ * + * @param \DateTimeZone|string $timezone The time zone in which the empty date is created. + * Defaults to "UTC". * * @return \ICanBoogie\DateTime */ - static public function none() + static public function none($timezone='utc') { - return new static('0000-00-00', 'utc'); + return new static('0000-00-00', $timezone); } /** * If the time zone is specified as a string a {@link \DateTimeZone} instance is created and * used instead. * - * @param string $time + *
+	 * 
+	 *
+	 * @param string $time Defaults to "now".
 	 * @param \DateTimeZone|string|null $timezone
 	 */
 	public function __construct($time='now', $timezone=null)
@@ -466,7 +503,7 @@ public function __toString()
 	 * If the timezone is `local` the timezone returned by {@link date_default_timezone_get()} is
 	 * used instead.
 	 */
-	public function setTimezone(/*\DateTimeZone*/ $timezone)
+	public function setTimezone($timezone)
 	{
 		if ($timezone === 'local')
 		{
diff --git a/tests/DateTimeTest.php b/tests/DateTimeTest.php
index eb7a27d..29cf687 100644
--- a/tests/DateTimeTest.php
+++ b/tests/DateTimeTest.php
@@ -40,6 +40,45 @@ public function test_none()
 		$this->assertEquals(-1, $d->year);
 		$this->assertEquals(11, $d->month);
 		$this->assertEquals(30, $d->day);
+
+		$d = DateTime::none('Asia/Tokyo');
+		$this->assertEquals('Asia/Tokyo', $d->zone->name);
+		$this->assertTrue($d->is_empty);
+
+		$d = DateTime::none(new \DateTimeZone('Asia/Tokyo'));
+		$this->assertEquals('Asia/Tokyo', $d->zone->name);
+		$this->assertTrue($d->is_empty);
+	}
+
+	public function test_from()
+	{
+		$d = DateTime::from(new \DateTime('2001-01-01 01:01:01', new \DateTimeZone('Europe/Paris')));
+		$this->assertEquals('Europe/Paris', $d->zone->name);
+		$this->assertEquals('2001-01-01 01:01:01', $d->as_db);
+
+		$d = DateTime::from(new \DateTime('2001-01-01 01:01:01', new \DateTimeZone('Europe/Paris')), new \DateTimeZone('UTC'));
+		$this->assertEquals('Europe/Paris', $d->zone->name);
+		$this->assertEquals('2001-01-01 01:01:01', $d->as_db);
+
+		$d = DateTime::from(new \DateTime('2001-01-01 01:01:01', new \DateTimeZone('UTC')));
+		$this->assertEquals('UTC', $d->zone->name);
+		$this->assertEquals('2001-01-01 01:01:01', $d->as_db);
+
+		$d = DateTime::from(new \DateTime('2001-01-01 01:01:01', new \DateTimeZone('UTC')), new \DateTimeZone('Europe/Paris'));
+		$this->assertEquals('UTC', $d->zone->name);
+		$this->assertEquals('2001-01-01 01:01:01', $d->as_db);
+
+		$d = DateTime::from('2001-01-01 01:01:01', new \DateTimeZone('UTC'));
+		$this->assertEquals('UTC', (string) $d->zone);
+		$this->assertEquals('2001-01-01 01:01:01', $d->as_db);
+
+		$d = DateTime::from('2001-01-01 01:01:01', new \DateTimeZone('Europe/Paris'));
+		$this->assertEquals('Europe/Paris', $d->zone->name);
+		$this->assertEquals('2001-01-01 01:01:01', $d->as_db);
+
+		$d = DateTime::from('2001-01-01 01:01:01');
+		$this->assertEquals(date_default_timezone_get(), $d->zone->name);
+		$this->assertEquals('2001-01-01 01:01:01', $d->as_db);
 	}
 
 	public function test_change()