Skip to content

Commit

Permalink
ENH Add functionality to DBDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 12, 2024
1 parent 3d3a3a8 commit 52832dd
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/ORM/FieldType/DBDatetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\ORM\FieldType;

use DateTime;
use Exception;
use IntlDateFormatter;
use InvalidArgumentException;
Expand Down Expand Up @@ -195,6 +196,69 @@ public function scaffoldFormField($title = null, $params = null)
return $field;
}

/**
* Get the amount of time inbetween two datetimes.
*/
public static function getTimeBetween(DBDateTime $from, DBDateTime $to): string
{
$fromRaw = new DateTime();
$fromRaw->setTimestamp((int) $from->getTimestamp());
$toRaw = new DateTime();
$toRaw->setTimestamp((int) $to->getTimestamp());
$diff = $fromRaw->diff($toRaw);
$result = [];
if ($diff->y) {
$result[] = _t(
__CLASS__ . '.nYears',
'one year|{count} years',
['count' => $diff->y]
);
}
if ($diff->m) {
$result[] = _t(
__CLASS__ . '.nMonths',
'one month|{count} months',
['count' => $diff->m]
);
}
if ($diff->d) {
$result[] = _t(
__CLASS__ . '.nDays',
'one day|{count} days',
['count' => $diff->d]
);
}
if ($diff->h) {
$result[] = _t(
__CLASS__ . '.nHours',
'one hour|{count} hours',
['count' => $diff->h]
);
}
if ($diff->i) {
$result[] = _t(
__CLASS__ . '.nMinutes',
'one minute|{count} minutes',
['count' => $diff->i]
);
}
if ($diff->s) {
$result[] = _t(
__CLASS__ . '.nSeconds',
'one second|{count} seconds',
['count' => $diff->s]
);
}
if (empty($result)) {
return _t(
__CLASS__ . '.nSeconds',
'{count} seconds',
['count' => 0]
);
}
return implode(', ', $result);
}

/**
*
*/
Expand Down
46 changes: 46 additions & 0 deletions tests/php/ORM/DBDatetimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,50 @@ public function modifyProvider()
['-59 seconds', '2019-03-03 11:59:01'],
];
}

public function provideGetTimeBetween(): array
{
return [
'no time between' => [
'timeBefore' => '2019-03-03 12:00:00',
'timeAfter' => '2019-03-03 12:00:00',
'expected' => '0 seconds',
],
'one second between' => [
'timeBefore' => '2019-03-03 12:00:00',
'timeAfter' => '2019-03-03 12:00:01',
'expected' => 'one second',
],
'some seconds between' => [
'timeBefore' => '2019-03-03 12:00:00',
'timeAfter' => '2019-03-03 12:00:15',
'expected' => '15 seconds',
],
'days and minutes between' => [
'timeBefore' => '2019-03-03 12:00:00',
'timeAfter' => '2019-03-15 12:05:00',
'expected' => '12 days, 5 minutes',
],
'years, months, and hours between' => [
'timeBefore' => '2019-03-03 12:00:00',
'timeAfter' => '2028-01-03 17:00:00',
'expected' => '8 years, 10 months, 5 hours',
],
'backwards in time doesnt say "negative" or "-"' => [
'timeBefore' => '2019-03-03 12:00:00',
'timeAfter' => '2018-01-06 12:01:12',
'expected' => 'one year, one month, 27 days, 23 hours, 58 minutes, 48 seconds',
],
];
}

/**
* @dataProvider provideGetTimeBetween
*/
public function testGetTimeBetween(string $timeBefore, string $timeAfter, string $expected): void
{
$before = (new DBDateTime())->setValue($timeBefore);
$after = (new DBDateTime())->setValue($timeAfter);
$this->assertSame($expected, DBDatetime::getTimeBetween($before, $after));
}
}

0 comments on commit 52832dd

Please sign in to comment.