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

[6.x] Drop Carbon 2 support #11500

Merged
merged 7 commits into from
Feb 25, 2025
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"league/glide": "^2.3",
"maennchen/zipstream-php": "^3.1",
"michelf/php-smartypants": "^1.8.1",
"nesbot/carbon": "^2.62.1 || ^3.0",
"nesbot/carbon": "^3.0",
"pixelfear/composer-dist-plugin": "^0.1.4",
"rebing/graphql-laravel": "^9.7",
"rhukster/dom-sanitizer": "^1.0.6",
Expand Down
9 changes: 0 additions & 9 deletions src/Http/Middleware/Localize.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ private function getToStringFormat(): ?string
{
$reflection = new ReflectionClass($date = Date::now());

// Carbon 2.x
if ($reflection->hasProperty('toStringFormat')) {
$format = $reflection->getProperty('toStringFormat');
$format->setAccessible(true);

return $format->getValue();
}

// Carbon 3.x
$factory = $reflection->getMethod('getFactory');
$factory->setAccessible(true);

Expand Down
12 changes: 6 additions & 6 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public function dashify($value)
*/
public function daysAgo($value, $params)
{
return (int) abs($this->carbon($value)->diffInDays(Arr::get($params, 0)));
return $this->carbon($value)->diffInDays(Arr::get($params, 0));
}

/**
Expand Down Expand Up @@ -1055,7 +1055,7 @@ public function hexToRgb($value)
*/
public function hoursAgo($value, $params)
{
return (int) abs($this->carbon($value)->diffInHours(Arr::get($params, 0)));
return $this->carbon($value)->diffInHours(Arr::get($params, 0));
}

/**
Expand Down Expand Up @@ -1617,7 +1617,7 @@ public function md5($value)
*/
public function minutesAgo($value, $params)
{
return (int) abs($this->carbon($value)->diffInMinutes(Arr::get($params, 0)));
return $this->carbon($value)->diffInMinutes(Arr::get($params, 0));
}

/**
Expand Down Expand Up @@ -1652,7 +1652,7 @@ public function modifyDate($value, $params)
*/
public function monthsAgo($value, $params)
{
return (int) abs($this->carbon($value)->diffInMonths(Arr::get($params, 0)));
return $this->carbon($value)->diffInMonths(Arr::get($params, 0));
}

/**
Expand Down Expand Up @@ -2234,7 +2234,7 @@ public function segment($value, $params, $context)
*/
public function secondsAgo($value, $params)
{
return (int) abs($this->carbon($value)->diffInSeconds(Arr::get($params, 0)));
return $this->carbon($value)->diffInSeconds(Arr::get($params, 0));
}

/**
Expand Down Expand Up @@ -2933,7 +2933,7 @@ public function values($value)
*/
public function weeksAgo($value, $params)
{
return (int) abs($this->carbon($value)->diffInWeeks(Arr::get($params, 0)));
return $this->carbon($value)->diffInWeeks(Arr::get($params, 0));
}

/**
Expand Down
7 changes: 1 addition & 6 deletions tests/Feature/GraphQL/Fieldtypes/DateFieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ public function setUp(): void
parent::setUp();

Carbon::macro('getToStringFormat', function () {
// Carbon 2.x
if (property_exists(static::this(), 'toStringFormat')) {
return static::$toStringFormat;
}

// Carbon 3.x
$reflection = new ReflectionClass(self::this());

$factory = $reflection->getMethod('getFactory');
$factory->setAccessible(true);

Expand Down
21 changes: 8 additions & 13 deletions tests/Modifiers/DaysAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@ public function it_outputs_days_ago($input, $expected)
{
Carbon::setTestNow(Carbon::parse('2025-02-20 00:00'));

$this->assertSame($expected, $this->modify(Carbon::parse($input)));
$this->assertSame($expected, round($this->modify(Carbon::parse($input)), 2));
}

public static function dateProvider()
{
return [
// Carbon 3 would return floats but to preserve backwards compatibility
// with Carbon 2 we will cast to integers.
'same time' => ['2025-02-20 00:00', 0],
'less than a day ago' => ['2025-02-19 11:00', 0],
'1 day ago' => ['2025-02-19 00:00', 1],
'2 days ago' => ['2025-02-18 00:00', 2],

// Future dates would return negative numbers in Carbon 3 but to preserve
// backwards compatibility with Carbon 2, we keep them positive.
'one day from now' => ['2025-02-21 00:00', 1],
'less than a day from now' => ['2025-02-20 13:00', 0],
'more than a day from now' => ['2025-02-21 13:00', 1],
'same time' => ['2025-02-20 00:00', 0.0],
'less than a day ago' => ['2025-02-19 11:00', 0.54],
'1 day ago' => ['2025-02-19 00:00', 1.0],
'2 days ago' => ['2025-02-18 00:00', 2.0],
'one day from now' => ['2025-02-21 00:00', -1.0],
'less than a day from now' => ['2025-02-20 13:00', -0.54],
'more than a day from now' => ['2025-02-21 13:00', -1.54],
];
}

Expand Down
21 changes: 8 additions & 13 deletions tests/Modifiers/HoursAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@ public function it_outputs_minutes_ago($input, $expected)
{
Carbon::setTestNow(Carbon::parse('2025-02-20 13:10:00'));

$this->assertSame($expected, $this->modify(Carbon::parse($input)));
$this->assertSame($expected, round($this->modify(Carbon::parse($input)), 2));
}

public static function dateProvider()
{
return [
// Carbon 3 would return floats but to preserve backwards compatibility
// with Carbon 2 we will cast to integers.
'same time' => ['2025-02-20 13:10:00', 0], // 0.0
'less than a hour ago' => ['2025-02-20 13:00:00', 0], // 0.17
'1 hour ago' => ['2025-02-20 12:10:00', 1], // 1.0
'2 hours ago' => ['2025-02-20 11:10:00', 2], // 2.0

// Future dates would return negative numbers in Carbon 3 but to preserve
// backwards compatibility with Carbon 2, we keep them positive.
'one hour from now' => ['2025-02-20 14:10:00', 1], // -1.0
'less than a hour from now' => ['2025-02-20 13:30:00', 0], // -0.33
'more than a hour from now' => ['2025-02-20 15:10:00', 2], // -2.0
'same time' => ['2025-02-20 13:10:00', 0.0],
'less than a hour ago' => ['2025-02-20 13:00:00', 0.17],
'1 hour ago' => ['2025-02-20 12:10:00', 1.0],
'2 hours ago' => ['2025-02-20 11:10:00', 2.0],
'one hour from now' => ['2025-02-20 14:10:00', -1.0],
'less than a hour from now' => ['2025-02-20 13:30:00', -0.33],
'more than a hour from now' => ['2025-02-20 15:10:00', -2.0],
];
}

Expand Down
21 changes: 8 additions & 13 deletions tests/Modifiers/MinutesAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@ public function it_outputs_minutes_ago($input, $expected)
{
Carbon::setTestNow(Carbon::parse('2025-02-20 13:10:00'));

$this->assertSame($expected, $this->modify(Carbon::parse($input)));
$this->assertSame($expected, round($this->modify(Carbon::parse($input)), 2));
}

public static function dateProvider()
{
return [
// Carbon 3 would return floats but to preserve backwards compatibility
// with Carbon 2 we will cast to integers.
'same time' => ['2025-02-20 13:10:00', 0], // 0.0
'less than a minute ago' => ['2025-02-20 13:09:30', 0], // 0.5
'1 minute ago' => ['2025-02-20 13:09:00', 1], // 1.0
'2 minutes ago' => ['2025-02-20 13:08:00', 2], // 2.0

// Future dates would return negative numbers in Carbon 3 but to preserve
// backwards compatibility with Carbon 2, we keep them positive.
'one minute from now' => ['2025-02-20 13:11:00', 1], // -1.0
'less than a minute from now' => ['2025-02-20 13:10:30', 0], // -0.5
'more than a minute from now' => ['2025-02-20 13:11:30', 1], // -1.5
'same time' => ['2025-02-20 13:10:00', 0.0],
'less than a minute ago' => ['2025-02-20 13:09:30', 0.5],
'1 minute ago' => ['2025-02-20 13:09:00', 1.0],
'2 minutes ago' => ['2025-02-20 13:08:00', 2.0],
'one minute from now' => ['2025-02-20 13:11:00', -1.0],
'less than a minute from now' => ['2025-02-20 13:10:30', -0.5],
'more than a minute from now' => ['2025-02-20 13:11:30', -1.5],
];
}

Expand Down
21 changes: 8 additions & 13 deletions tests/Modifiers/MonthsAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@ public function it_outputs_months_ago($input, $expected)
{
Carbon::setTestNow(Carbon::parse('2025-02-20'));

$this->assertSame($expected, $this->modify(Carbon::parse($input)));
$this->assertSame($expected, round($this->modify(Carbon::parse($input)), 2));
}

public static function dateProvider()
{
return [
// Carbon 3 would return floats but to preserve backwards compatibility
// with Carbon 2 we will cast to integers.
'same month' => ['2025-02-20', 0], // 0.0
'less than a month ago' => ['2025-02-10', 0], // 0.36
'1 month ago' => ['2025-01-20', 1], // 1.0
'2 months ago' => ['2024-12-20', 2], // 2.0

// Future dates would return negative numbers in Carbon 3 but to preserve
// backwards compatibility with Carbon 2, we keep them positive.
'one month from now' => ['2025-03-20', 1], // -1.0
'less than a month from now' => ['2025-02-25', 0], // -0.18
'more than a month from now' => ['2025-04-20', 2], // -2.0
'same month' => ['2025-02-20', 0.0],
'less than a month ago' => ['2025-02-10', 0.36],
'1 month ago' => ['2025-01-20', 1.0],
'2 months ago' => ['2024-12-20', 2.0],
'one month from now' => ['2025-03-20', -1.0],
'less than a month from now' => ['2025-02-25', -0.18],
'more than a month from now' => ['2025-04-20', -2.0],
];
}

Expand Down
17 changes: 6 additions & 11 deletions tests/Modifiers/SecondsAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,17 @@ public function it_outputs_seconds_ago($input, $expected)
{
Carbon::setTestNow(Carbon::parse('2025-02-20 13:10:30'));

$this->assertSame($expected, $this->modify(Carbon::parse($input)));
$this->assertSame($expected, round($this->modify(Carbon::parse($input)), 2));
}

public static function dateProvider()
{
return [
// Carbon 3 would return floats but to preserve backwards compatibility
// with Carbon 2 we will cast to integers.
'same second' => ['2025-02-20 13:10:30', 0], // 0.0
'1 second ago' => ['2025-02-20 13:10:29', 1], // 1.0
'2 seconds ago' => ['2025-02-20 13:10:28', 2], // 2.0

// Future dates would return negative numbers in Carbon 3 but to preserve
// backwards compatibility with Carbon 2, we keep them positive.
'one second from now' => ['2025-02-20 13:10:31', 1], // -1.0
'two seconds from now' => ['2025-02-20 13:10:32', 2], // -2.0
'same second' => ['2025-02-20 13:10:30', 0.0],
'1 second ago' => ['2025-02-20 13:10:29', 1.0],
'2 seconds ago' => ['2025-02-20 13:10:28', 2.0],
'one second from now' => ['2025-02-20 13:10:31', -1.0],
'two seconds from now' => ['2025-02-20 13:10:32', -2.0],
];
}

Expand Down
23 changes: 9 additions & 14 deletions tests/Modifiers/WeeksAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,20 @@ public function it_outputs_weeks_ago($input, $expected)
{
Carbon::setTestNow(Carbon::parse('2025-02-20'));

$this->assertSame($expected, $this->modify(Carbon::parse($input)));
$this->assertSame($expected, round($this->modify(Carbon::parse($input)), 2));
}

public static function dateProvider()
{
return [
// Carbon 3 would return floats but to preserve backwards compatibility
// with Carbon 2 we will cast to integers.
'same day' => ['2025-02-20', 0], // 0.0
'same week' => ['2025-02-19', 0], // 0.14
'less than a week ago' => ['2025-02-17', 0], // 0.43
'1 week ago' => ['2025-02-13', 1], // 1.0
'2 weeks ago' => ['2025-02-06', 2], // 2.0

// Future dates would return negative numbers in Carbon 3 but to preserve
// backwards compatibility with Carbon 2, we keep them positive.
'one week from now' => ['2025-02-27', 1], // -1.0
'less than a week from now' => ['2025-02-22', 0], // -0.29
'more than a week from now' => ['2025-03-08', 2], // -2.29
'same day' => ['2025-02-20', 0.0],
'same week' => ['2025-02-19', 0.14],
'less than a week ago' => ['2025-02-17', 0.43],
'1 week ago' => ['2025-02-13', 1.0],
'2 weeks ago' => ['2025-02-06', 2.0],
'one week from now' => ['2025-02-27', -1.0],
'less than a week from now' => ['2025-02-22', -0.29],
'more than a week from now' => ['2025-03-08', -2.29],
];
}

Expand Down
Loading