From 056614f0f59f5ee5d633b0e4e48fe5edec6dd3cb Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Wed, 11 Jan 2023 14:44:52 +0100 Subject: Normalize double sign --/+-/-+/++ calculations in it::date for PHP 8.2 and add tests for it --- it.class | 2 +- test/it.t | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/it.class b/it.class index 8375b83..f302bce 100644 --- a/it.class +++ b/it.class @@ -1044,7 +1044,7 @@ static function date($format = "", $stamp = null) if (!isset($stamp)) $stamp = it::time(); else if (is_string($stamp) && !ctype_digit($stamp)) - $stamp = strtotime($stamp, it::time()); + $stamp = strtotime(it::replace(['\s(--|\+\+)\s*(\d)' => ' +\2', '\s(\+-|-\+)\s*(\d)' => ' -\2'], $stamp), it::time()); list($name, $language) = explode(":", $format); diff --git a/test/it.t b/test/it.t index 41758da..34279ce 100755 --- a/test/it.t +++ b/test/it.t @@ -388,7 +388,19 @@ _time("152715", it::date('', '15:27:15')); _time("yesterday", it::date('', 'yesterday')); is(it::date('date', '2011-10-25'), '25.10.2011', 'parse date string with strtotime'); -is(it::date('date', '2011-10-25 + 3 days'), '28.10.2011', 'some date arithmetic'); +is(it::date('date', '2011-10-25 + 3 days'), '28.10.2011', 'date arithmetic (add)'); +is(it::date('date', '2011-10-25 - 3 days'), '22.10.2011', 'date arithmetic (subtract)'); +is(it::date('date', '2011-10-25 +-3 days'), '22.10.2011', 'date arithmetic (negative value)'); +is(it::date('date', '2011-10-25 --3 days'), '28.10.2011', 'date arithmetic (double negative)'); +is(it::date('date', '2011-10-25 ++ 3 days'), '28.10.2011', 'date arithmetic (double positive with space)'); +is(it::date('date', '2011-10-25 -+ 3 days'), '22.10.2011', 'date arithmetic (negative positive with space)'); +is(it::date('datetime', '2011-10-25 +12 hours +30 minutes'), '25.10.2011 12:30', 'datetime arithmetic (hours minutes)'); +is(it::date('datetime', '2011-10-25 +24 hours +30 minutes'), '26.10.2011 00:30', 'datetime arithmetic (hours minutes over midnight)'); +is(it::date('datetime', '2011-10-25 8:00 +10 minutes'), '25.10.2011 08:10', 'time arithmetic (add)'); +is(it::date('datetime', '2011-10-25 8:00 -10 minutes'), '25.10.2011 07:50', 'time arithmetic (subtract)'); +is(it::date('datetime', '2011-10-25 8:00 +-10 minutes'), '25.10.2011 07:50', 'time arithmetic (negative value)'); +is(it::date('datetime', '2011-10-25 8:00 --10 minutes'), '25.10.2011 08:10', 'time arithmetic (double negative)'); +is(it::date('datetime', '2011-10-25 --1 month --3 days 8:00 +- 4 hours +-10 minutes'), '28.11.2011 03:50', 'date and time arithmetic (multiple signs)'); is(it::date('datetime', it::time()), it::date('datetime'), 'recognize int as timestamp'); is(it::date('datetime', it::time()*1.0), it::date('datetime'), 'recognize float as timestamp'); is(it::date('datetime', it::time() . ''), it::date('datetime'), 'recognize digit string as timestamp'); -- cgit v1.2.3