Skip to content

Commit

Permalink
Break the "ago" date modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Feb 25, 2025
1 parent b4d126b commit c1b1fe0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 77 deletions.
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
19 changes: 7 additions & 12 deletions tests/Modifiers/DaysAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ public function it_outputs_days_ago($input, $expected)
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.5416666666666666],
'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.5416666666666666],
'more than a day from now' => ['2025-02-21 13:00', -1.5416666666666667],
];
}

Expand Down
19 changes: 7 additions & 12 deletions tests/Modifiers/HoursAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ public function it_outputs_minutes_ago($input, $expected)
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.16666666666666666],
'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.3333333333333333],
'more than a hour from now' => ['2025-02-20 15:10:00', -2.0],
];
}

Expand Down
19 changes: 7 additions & 12 deletions tests/Modifiers/MinutesAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ public function it_outputs_minutes_ago($input, $expected)
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
19 changes: 7 additions & 12 deletions tests/Modifiers/MonthsAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ public function it_outputs_months_ago($input, $expected)
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.35714285714285715],
'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.17857142857142858],
'more than a month from now' => ['2025-04-20', -2.0],
];
}

Expand Down
15 changes: 5 additions & 10 deletions tests/Modifiers/SecondsAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@ public function it_outputs_seconds_ago($input, $expected)
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
21 changes: 8 additions & 13 deletions tests/Modifiers/WeeksAgoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,14 @@ public function it_outputs_weeks_ago($input, $expected)
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.14285714285714285],
'less than a week ago' => ['2025-02-17', 0.42857142857142855],
'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.2857142857142857],
'more than a week from now' => ['2025-03-08', -2.2857142857142856],
];
}

Expand Down

0 comments on commit c1b1fe0

Please sign in to comment.