Skip to content

Commit

Permalink
Added timezone parameter to now() and none()
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Laviale committed Apr 23, 2013
1 parent 204f9a7 commit 5bf056f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
55 changes: 46 additions & 9 deletions lib/datetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,26 +187,38 @@ class DateTime extends \DateTime
/**
* Creates a {@link DateTime} instance from a source.
*
* <pre>
* <?php
*
* use ICanBoogie\DateTime;
*
* DateTime::from(new \DateTime('2001-01-01 01:01:01', new \DateTimeZone('Europe/Paris')));
* DateTime::from('2001-01-01 01:01:01', 'Europe/Paris');
* DateTime::from('now');
* </pre>
*
* @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)
{
return clone $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
*/
Expand All @@ -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.
* <pre>
* <?php
*
* use ICanBoogie\DateTime;
*
* $d = DateTime::none();
* $d->is_empty; // true
* $d->zone->name; // "UTC"
*
* $d = DateTime::none('Asia/Tokyo');
* $d->is_empty; // true
* $d->zone->name; // "Asia/Tokio"
* </pre>
*
* @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
* <pre>
* <?php
*
* use ICanBoogie\DateTime;
*
* new DateTime('2001-01-01 01:01:01', new \DateTimeZone('Europe/Paris')));
* new DateTime('2001-01-01 01:01:01', 'Europe/Paris');
* new DateTime;
* </pre>
*
* @param string $time Defaults to "now".
* @param \DateTimeZone|string|null $timezone
*/
public function __construct($time='now', $timezone=null)
Expand Down Expand Up @@ -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')
{
Expand Down
39 changes: 39 additions & 0 deletions tests/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5bf056f

Please sign in to comment.