Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Carbon objects #235

Merged
merged 2 commits into from
Mar 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,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)) {
Copy link
Contributor Author

@Propaganistas Propaganistas Feb 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $time is an integer, it's a Unix timestamp. By not sending it through translateTimeString(), it remains an integer and is then correctly handled in the constructor.

$time = static::translateTimeString($time);
}

return new static($time, $timezone);
}
Expand Down Expand Up @@ -274,8 +283,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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows Carbon objects to be passed to timespan().

}

$units = ['y' => 'year', 'm' => 'month', 'w' => 'week', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second'];
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;

class DateTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -60,6 +61,13 @@ public function testMake()
$this->assertEquals($now1, $now2);
}

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

public function testManipulation()
{
$now = Date::now();
Expand Down