Skip to content

Commit

Permalink
Added 'ago' suffix for adaptive past dates.
Browse files Browse the repository at this point in the history
Fixed tests for PHP 8.
Removed Travis CI in lieu of GitHub Actions workflow.
  • Loading branch information
Bilge committed Apr 6, 2024
1 parent 63564d6 commit edd04cd
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 60 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/Tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Tests

on:
push:
pull_request:
workflow_dispatch:
schedule:
- cron: 0 6 * * *

jobs:
Test:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php:
- 8.1
- 8.2
- 8.3

steps:
- uses: actions/checkout@v3

- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Validate composer.json
run: composer validate

- name: Cache dependencies
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: php-${{ matrix.php }}

- name: Install dependencies
run: composer update --no-interaction --no-progress

- name: Run test suite
run: composer test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
/.*/
!/.github/
/composer.lock
36 changes: 0 additions & 36 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
}
],
"require": {
"php": "^7.2|^8",
"php": "^8.1",
"scriptfusion/static-class": "^1"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9"
"phpunit/phpunit": "^9"
},
"autoload": {
"psr-4": {
Expand Down
26 changes: 11 additions & 15 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,25 @@ final class Date
*
* @return string Absolute or relative date.
*/
public static function adapt($date): string
public static function adapt(string|\DateTimeInterface $date): string
{
$date = is_string($date) ? new \DateTimeImmutable($date) : $date;
$diff = (new \DateTime)->diff($date);
$days = $diff->days * ($diff->invert ? -1 : 1);
// If diff less than one whole second from next day, round up to whole day.
$absDays = ($diff->days + (int)(($diff->h * 60 ** 2 + $diff->i * 60 + $diff->s + ceil($diff->f)) / 86400));
$days = $absDays * ($diff->invert ? -1 : 1);

// Absolute.
if ($diff->days > 30) {
if ($absDays > 30) {
return $date->format('M Y');
}

// Relative.
switch ($days) {
case -1:
return 'yesterday';

case 0:
return 'today';

case 1:
return 'tomorrow';
}

return "$diff->days days";
return match ($days) {
-1 => 'yesterday',
0 => 'today',
1 => 'tomorrow',
default => "$absDays days" . ($days < 0 ? ' ago' : ''),
};
}
}
4 changes: 2 additions & 2 deletions test/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public function testYesterday(): void

public function testLastWeek(): void
{
self::assertSame('7 days', Date::adapt(new \DateTime('-7 day')));
self::assertSame('7 days ago', Date::adapt(new \DateTime('-7 day')));
}

public function test30DaysAgo(): void
{
self::assertSame('30 days', Date::adapt(new \DateTime('-30 day')));
self::assertSame('30 days ago', Date::adapt(new \DateTime('-30 day')));
}

public function test31DaysAgo(): void
Expand Down
11 changes: 6 additions & 5 deletions test/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<phpunit
beStrictAboutOutputDuringTests="true"
executionOrder="random"
>
<testsuite>
<testsuite name="all">
<directory>.</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<coverage processUncoveredFiles="true">
<include>
<directory>../src</directory>
</whitelist>
</filter>
</include>
</coverage>
</phpunit>

0 comments on commit edd04cd

Please sign in to comment.