From 7ed3a0c2b3e1b3003cb88c3d153c85da370724ce Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Wed, 20 Sep 2023 14:02:13 +0200 Subject: [PATCH] Add isNotAfter --- src/DateTime.php | 5 ++ tests/DateTime/IsNotAfterTest.php | 86 +++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 tests/DateTime/IsNotAfterTest.php diff --git a/src/DateTime.php b/src/DateTime.php index 5db4649..1588a37 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -112,6 +112,11 @@ public function isAfter(self $dateTime): bool return $this->dateTime > $dateTime->dateTime; } + public function isNotAfter(self $dateTime): bool + { + return $this->dateTime < $dateTime->dateTime; + } + public function isAfterOrEqualTo(self $dateTime): bool { return $this->dateTime >= $dateTime->dateTime; diff --git a/tests/DateTime/IsNotAfterTest.php b/tests/DateTime/IsNotAfterTest.php new file mode 100644 index 0000000..8c96833 --- /dev/null +++ b/tests/DateTime/IsNotAfterTest.php @@ -0,0 +1,86 @@ +isNotAfter($comparator)); + } + + /** + * @return array + */ + public function dataProvider(): array + { + return [ + 'previous year' => [ + false, + DateTime::fromString('2022-10-08 15:00:00'), + DateTime::fromString('2021-10-08 15:00:00'), + ], + 'is equal' => [ + false, + DateTime::fromString('2022-10-08 15:00:00'), + DateTime::fromString('2022-10-08 15:00:00'), + ], + 'next year' => [ + true, + DateTime::fromString('2022-10-08 15:00:00'), + DateTime::fromString('2023-10-08 15:00:00'), + ], + 'next month' => [ + true, + DateTime::fromString('2022-10-08 15:00:00'), + DateTime::fromString('2022-11-08 15:00:00'), + ], + 'next day' => [ + true, + DateTime::fromString('2022-10-08 15:00:00'), + DateTime::fromString('2022-10-09 15:00:00'), + ], + 'next hour' => [ + true, + DateTime::fromString('2022-10-08 15:00:00'), + DateTime::fromString('2022-10-08 16:00:00'), + ], + 'next minute' => [ + true, + DateTime::fromString('2022-10-08 15:00:00'), + DateTime::fromString('2022-10-08 15:01:00'), + ], + 'next second' => [ + true, + DateTime::fromString('2022-10-08 15:00:00'), + DateTime::fromString('2022-10-08 15:00:01'), + ], + 'next millisecond' => [ + true, + DateTime::fromString('2022-10-08 15:00:00'), + DateTime::fromString('2022-10-08 15:00:00.000001'), + ], + ]; + } +}