Skip to content

Commit

Permalink
Add TimeBetween function
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Nov 16, 2017
1 parent fbff356 commit 71a75af
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
19 changes: 18 additions & 1 deletion demo/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
. DIRECTORY_SEPARATOR . 'Twig' . DIRECTORY_SEPARATOR . 'Extensions' . DIRECTORY_SEPARATOR . 'SameDayExtension.php';
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'JMBTechnologyLimited'
. DIRECTORY_SEPARATOR . 'Twig' . DIRECTORY_SEPARATOR . 'Extensions' . DIRECTORY_SEPARATOR . 'TimeZoneExtension.php';
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'JMBTechnologyLimited'
. DIRECTORY_SEPARATOR . 'Twig' . DIRECTORY_SEPARATOR . 'Extensions' . DIRECTORY_SEPARATOR . 'TimeBetweenExtension.php';

use JMBTechnologyLimited\Twig\Extensions\LinkifyExtension;
use JMBTechnologyLimited\Twig\Extensions\TimeBetweenExtension;


/**
Expand All @@ -30,7 +33,21 @@ class LinkifyExtensionInNewWindow extends LinkifyExtension {
$loader = new Twig_Loader_Filesystem(array(__DIR__ . DIRECTORY_SEPARATOR . 'templates'));
$twig = new Twig_Environment($loader, array(
));
$twig->addExtension(new TimeBetweenExtension());
$twig->addExtension(new LinkifyExtension());
$twig->addExtension(new LinkifyExtensionInNewWindow(array('attr'=>array('target'=>'_blank'))));
file_put_contents(__DIR__. DIRECTORY_SEPARATOR. 'out'. DIRECTORY_SEPARATOR. 'linkify.html', $twig->render('linkify.html.twig'));
file_put_contents(
__DIR__. DIRECTORY_SEPARATOR. 'out'. DIRECTORY_SEPARATOR. 'linkify.html',
$twig->render('linkify.html.twig')
);
file_put_contents(
__DIR__. DIRECTORY_SEPARATOR. 'out'. DIRECTORY_SEPARATOR. 'timebetween.html',
$twig->render(
'timebetween.html.twig',
array(
'date1' => new \DateTime('2017-10-01 10:00:00'),
'date2' => new \DateTime('2017-10-03 12:00:00'),
)
)
);

10 changes: 10 additions & 0 deletions demo/templates/timebetween.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>

</head>
<body>
<p>
{{ timebetween(date1, date2) }}
</p>
</body>
</html>
72 changes: 72 additions & 0 deletions src/JMBTechnologyLimited/Twig/Extensions/TimeBetweenExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace JMBTechnologyLimited\Twig\Extensions;

/**
* Takes two DateTime objects.
* Returns string of time between them.
*
* With a lot of inspiration from https://github.com/twigphp/Twig-extensions/blob/fe6f308b5aa35b64ae1687f1f6f510e4e59c033e/lib/Twig/Extensions/Extension/Date.php
* (License: https://github.com/twigphp/Twig-extensions/blob/e740ab2e3c97b54028f21f9f8314caf6fdc482d7/LICENSE )
*
* @license https://github.com/JMB-Technology-Limited/Twig-Extensions/blob/master/LICENSE.txt 3-clause BSD
* @copyright (c) JMB Technology Limited, http://jmbtechnology.co.uk/
*/
class TimeBetweenExtension extends \Twig_Extension
{

const FUNCTION_NAME_TIMEBETWEEN = 'timebetween';

public static $units = array(
'y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);

const LABEL_ONE = 'one';
const LABEL_PLURAL = 's';


public function getFilters()
{
return array();
}

public function getFunctions()
{
return array(
new \Twig_SimpleFunction(static::FUNCTION_NAME_TIMEBETWEEN, array($this, 'timebetween')),
);
}

public function timebetween($date1,$date2) {
if(get_class($date1) != 'DateTime') return '';
if(get_class($date2) != 'DateTime') return '';

$diff = $date1->diff($date2);

foreach (self::$units as $attribute => $unit) {
$count = $diff->$attribute;
if (0 !== $count) {
return $this->getPluralizedInterval($count, $diff->invert, $unit);
}
}
return '';
}

protected function getPluralizedInterval($count, $invert, $unit)
{
if (1 == $count) {
return self::LABEL_ONE . " " . $unit;
} else {
return $count . " " . $unit. self::LABEL_PLURAL;
}
}


}


40 changes: 40 additions & 0 deletions tests/TimeBetweenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use PHPUnit\Framework\TestCase;

/*
* @license https://github.com/JMB-Technology-Limited/Twig-Extensions/blob/master/LICENSE.txt 3-clause BSD
* @copyright (c) JMB Technology Limited, http://jmbtechnology.co.uk/
*/
class TimeBetweenTest extends TestCase {

function timeBetweenTestProvider() {
$utc = new \DateTimeZone('UTC');
return array(
array(new DateTime('2015-01-01 10:00:00', $utc), new DateTime('2015-01-01 12:00:00', $utc), '2 hours'),
array(new DateTime('2015-01-01 10:00:00', $utc), new DateTime('2015-01-02 12:00:00', $utc), 'one day'),
array(new DateTime('2015-01-01 10:00:00', $utc), new DateTime('2015-01-01 10:30:00', $utc), '30 minutes'),
array(new DateTime('2015-01-01 10:00:00', $utc), new DateTime('2015-02-01 10:30:00', $utc), 'one month'),
array(new DateTime('2015-01-01 10:00:00', $utc), new DateTime('2016-02-01 10:30:00', $utc), 'one year'),
);
}

/**
* @dataProvider timeBetweenTestProvider
*/
function testTimeBetween($in1, $in2, $out) {
$ext = new \JMBTechnologyLimited\Twig\Extensions\TimeBetweenExtension();
$this->assertEquals($out, $ext->timebetween($in1, $in2));
}

/**
*
* It shouldn't matter in which order you pass the dates.
* @dataProvider timeBetweenTestProvider
*/
function testTimeBetweenReversed($in1, $in2, $out) {
$ext = new \JMBTechnologyLimited\Twig\Extensions\TimeBetweenExtension();
$this->assertEquals($out, $ext->timebetween($in2, $in1));
}

}

0 comments on commit 71a75af

Please sign in to comment.