Skip to content

Commit

Permalink
Merge pull request #316 from kylekatarnls/master
Browse files Browse the repository at this point in the history
Provide minimal support of Carbon 2
  • Loading branch information
jenssegers committed Mar 10, 2019
2 parents d395ca4 + ff4a92b commit 58393b0
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": ">=5.6",
"nesbot/carbon": "^1.0",
"nesbot/carbon": "^1.0|^2.0",
"symfony/translation": "^2.7|^3.0|^4.0"
},
"require-dev": {
Expand Down
42 changes: 32 additions & 10 deletions src/Date.php
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use DateInterval;
use DateTimeZone;
use ReflectionMethod;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\TranslatorInterface;
Expand Down Expand Up @@ -103,23 +104,30 @@ public static function createFromFormat($format, $time, $timezone = null)
* Alias for diffForHumans.
*
* @param Date $since
* @param bool $absolute Removes time difference modifiers ago, after, etc
* @param bool $syntax Removes time difference modifiers ago, after, etc
* @param bool $short (Carbon 2 only) displays short format of time units
* @param int $parts (Carbon 2 only) maximum number of parts to display (default value: 1: single unit)
* @param int $options (Carbon 2 only) human diff options
* @return string
*/
public function ago($since = null, $absolute = false)
public function ago($since = null, $syntax = null, $short = false, $parts = 1, $options = null)
{
return $this->diffForHumans($since, $absolute);
return $this->diffForHumans($since, $syntax, $short, $parts, $options);
}

/**
* Alias for diffForHumans.
*
* @param Date $since
* @param bool $syntax Removes time difference modifiers ago, after, etc
* @param bool $short (Carbon 2 only) displays short format of time units
* @param int $parts (Carbon 2 only) maximum number of parts to display (default value: 1: single unit)
* @param int $options (Carbon 2 only) human diff options
* @return string
*/
public function until($since = null)
public function until($since = null, $syntax = null, $short = false, $parts = 1, $options = null)
{
return $this->ago($since);
return $this->ago($since, $syntax, $short, $parts, $options);
}

/**
Expand Down Expand Up @@ -247,9 +255,11 @@ public function timespan($time = null, $timezone = null)
* Adds an amount of days, months, years, hours, minutes and seconds to a Date object.
*
* @param DateInterval|string $interval
* @param int $value (only effective if using Carbon 2)
* @param bool|null $overflow (only effective if using Carbon 2)
* @return Date|bool
*/
public function add($interval)
public function add($interval, $value = 1, $overflow = null)
{
if (is_string($interval)) {
// Check for ISO 8601
Expand All @@ -260,16 +270,23 @@ public function add($interval)
}
}

return parent::add($interval) ? $this : false;
$method = new ReflectionMethod(parent::class, 'add');
$result = $method->getNumberOfRequiredParameters() === 1
? parent::add($interval)
: parent::add($interval, $value, $overflow);

return $result ? $this : false;
}

/**
* Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object.
*
* @param DateInterval|string $interval
* @param int $value (only effective if using Carbon 2)
* @param bool|null $overflow (only effective if using Carbon 2)
* @return Date|bool
*/
public function sub($interval)
public function sub($interval, $value = 1, $overflow = null)
{
if (is_string($interval)) {
// Check for ISO 8601
Expand All @@ -280,7 +297,12 @@ public function sub($interval)
}
}

return parent::sub($interval) ? $this : false;
$method = new ReflectionMethod(parent::class, 'sub');
$result = $method->getNumberOfRequiredParameters() === 1
? parent::sub($interval)
: parent::sub($interval, $value, $overflow);

return $result ? $this : false;
}

/**
Expand Down Expand Up @@ -377,7 +399,7 @@ public static function setTranslator(TranslatorInterface $translator)
* @param string $time
* @return string
*/
public static function translateTimeString($time)
public static function translateTimeString($time, $from = null, $to = null, $mode = -1)
{
// Don't run translations for english.
if (static::getLocale() == 'en') {
Expand Down
1 change: 1 addition & 0 deletions tests/AutomaticTest.php
Expand Up @@ -8,6 +8,7 @@ class AutomaticTest extends TestCase
{
public function setUp()
{
Date::setTestNow(Date::now());
$this->languages = array_slice(scandir('src/Lang'), 2);
}

Expand Down
12 changes: 8 additions & 4 deletions tests/DateTest.php
Expand Up @@ -6,9 +6,13 @@

class DateTest extends TestCase
{
protected $time;

public function setUp()
{
date_default_timezone_set('UTC');
Date::setTestNow(Date::now());
$this->time = Date::now()->getTimestamp();
Date::setLocale('en');
}

Expand All @@ -22,7 +26,7 @@ public function testStaticNow()
{
$date = Date::now();
$this->assertInstanceOf('Jenssegers\Date\Date', $date);
$this->assertEquals(time(), $date->getTimestamp());
$this->assertEquals($this->time, $date->getTimestamp());
}

public function testConstructFromString()
Expand All @@ -31,20 +35,20 @@ public function testConstructFromString()
$this->assertSame(1359590400, $date->getTimestamp());

$date = new Date('1 day ago');
$this->assertSame(time() - 86400, $date->getTimestamp());
$this->assertSame($this->time - 86400, $date->getTimestamp());
}

public function testConstructWithTimezone()
{
$date = new Date('now', 'Europe/Paris');
date_default_timezone_set('Europe/Paris');
$this->assertSame(time(), $date->getTimestamp());
$this->assertSame($this->time, $date->getTimestamp());

date_default_timezone_set('Europe/Brussels');

$date = new Date(null, 'Europe/Paris');
date_default_timezone_set('Europe/Paris');
$this->assertSame(time(), $date->getTimestamp());
$this->assertSame($this->time, $date->getTimestamp());
}

public function testConstructTimestamp()
Expand Down
1 change: 1 addition & 0 deletions tests/TranslationElTest.php
Expand Up @@ -8,6 +8,7 @@ class TranslationElTest extends TestCase
public function setUp()
{
date_default_timezone_set('UTC');
Date::setTestNow(Date::now());
Date::setLocale('el');
}

Expand Down
1 change: 1 addition & 0 deletions tests/TranslationHuTest.php
Expand Up @@ -10,6 +10,7 @@ class TranslationHuTest extends TestCase
public function setUp()
{
date_default_timezone_set('UTC');
Date::setTestNow(Date::now());
Date::setLocale('hu');
}

Expand Down
1 change: 1 addition & 0 deletions tests/TranslationJaTest.php
Expand Up @@ -11,6 +11,7 @@ class TranslationJaTest extends TestCase
public function setUp()
{
date_default_timezone_set('UTC');
Date::setTestNow(Date::now());
Date::setLocale('ja');
}

Expand Down
1 change: 1 addition & 0 deletions tests/TranslationKaTest.php
Expand Up @@ -8,6 +8,7 @@ class TranslationKaTest extends TestCase
public function setUp()
{
date_default_timezone_set('UTC');
Date::setTestNow(Date::now());
Date::setLocale('ka');
}

Expand Down
1 change: 1 addition & 0 deletions tests/TranslationTaTest.php
Expand Up @@ -8,6 +8,7 @@ class TranslationTaTest extends TestCase
public function setUp()
{
date_default_timezone_set('UTC');
Date::setTestNow(Date::now());
Date::setLocale('ta');
}

Expand Down
1 change: 1 addition & 0 deletions tests/TranslationTest.php
Expand Up @@ -10,6 +10,7 @@ class TranslationTest extends TestCase
public function setUp()
{
date_default_timezone_set('UTC');
Date::setTestNow(Date::now());
}

public function testGetsAndSetsTranslator()
Expand Down
1 change: 1 addition & 0 deletions tests/TranslationThTest.php
Expand Up @@ -8,6 +8,7 @@ class TranslationThTest extends TestCase
public function setUp()
{
date_default_timezone_set('UTC');
Date::setTestNow(Date::now());
Date::setLocale('th');
}

Expand Down

0 comments on commit 58393b0

Please sign in to comment.