Skip to content

Commit

Permalink
Merge pull request #235 from Propaganistas/parse-carbon
Browse files Browse the repository at this point in the history
Parse Carbon objects
  • Loading branch information
jenssegers committed Mar 12, 2017
2 parents 304a2c1 + 4d6cb34 commit 6a1376e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,16 @@ public static function make($time = null, $timezone = null)
*/
public static function parse($time = null, $timezone = null)
{
$time = static::translateTimeString($time);
if ($time instanceof Carbon) {
return new static(
$time->toDateTimeString(),
$timezone ?: $time->getTimezone()
);
}

if (! is_int($time)) {
$time = static::translateTimeString($time);
}

return new static($time, $timezone);
}
Expand Down Expand Up @@ -260,8 +269,8 @@ public function timespan($time = null, $timezone = null)
$lang = $this->getTranslator();

// Create Date instance if needed
if (! $time instanceof self) {
$time = new static($time, $timezone);
if (! $time instanceof static) {
$time = Date::parse($time, $timezone);
}

$units = [
Expand Down
8 changes: 8 additions & 0 deletions tests/DateTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Carbon\Carbon;
use Jenssegers\Date\Date;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -59,6 +60,13 @@ public function testMake()
$this->assertEquals($date1, $date2);
}

public function testCreateFromCarbon()
{
$date = Date::make(Carbon::createFromFormat('U', 1367186296));
$this->assertInstanceOf('Jenssegers\Date\Date', $date);
$this->assertEquals(1367186296, $date->getTimestamp());
}

public function testManipulation()
{
$this->assertInstanceOf('Jenssegers\Date\Date', Date::now()->add('1 day'));
Expand Down

0 comments on commit 6a1376e

Please sign in to comment.