From edd04cdc26b462ee410e009e1c23b526fb94d030 Mon Sep 17 00:00:00 2001 From: Bilge Date: Sat, 6 Apr 2024 10:29:13 +0100 Subject: [PATCH] Added 'ago' suffix for adaptive past dates. Fixed tests for PHP 8. Removed Travis CI in lieu of GitHub Actions workflow. --- .github/workflows/Tests.yaml | 44 ++++++++++++++++++++++++++++++++++++ .gitignore | 1 + .travis.yml | 36 ----------------------------- composer.json | 4 ++-- src/Date.php | 26 +++++++++------------ test/DateTest.php | 4 ++-- test/phpunit.xml | 11 +++++---- 7 files changed, 66 insertions(+), 60 deletions(-) create mode 100644 .github/workflows/Tests.yaml delete mode 100644 .travis.yml diff --git a/.github/workflows/Tests.yaml b/.github/workflows/Tests.yaml new file mode 100644 index 0000000..d624d78 --- /dev/null +++ b/.github/workflows/Tests.yaml @@ -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 diff --git a/.gitignore b/.gitignore index 74fbacf..6413333 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor/ /.*/ +!/.github/ /composer.lock diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ce8ad05..0000000 --- a/.travis.yml +++ /dev/null @@ -1,36 +0,0 @@ -notifications: - email: false - -sudo: false - -language: php - -php: - - 7.2 - - 7.3 - - 7.4 - - 8.0 - -env: - matrix: - - - - DEPENDENCIES=--prefer-lowest - -matrix: - fast_finish: true - -cache: - directories: - - vendor - -install: - - alias composer=composer\ -n && composer selfupdate - - composer validate - - composer update --no-progress --no-suggest $DEPENDENCIES - -script: - - XDEBUG_MODE=coverage composer test -- --coverage-clover=build/logs/clover.xml - -after_success: - - composer require php-coveralls/php-coveralls:^2 - - vendor/bin/php-coveralls -v diff --git a/composer.json b/composer.json index 19d073f..44b5a2c 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Date.php b/src/Date.php index 1b63432..73afcd9 100644 --- a/src/Date.php +++ b/src/Date.php @@ -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' : ''), + }; } } diff --git a/test/DateTest.php b/test/DateTest.php index 6ea3b7a..ebbfcee 100644 --- a/test/DateTest.php +++ b/test/DateTest.php @@ -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 diff --git a/test/phpunit.xml b/test/phpunit.xml index 9d80b47..e140a3a 100644 --- a/test/phpunit.xml +++ b/test/phpunit.xml @@ -1,12 +1,13 @@ - + . - - + + ../src - - + +