-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d757962
commit 46843e1
Showing
7 changed files
with
292 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
### Age | ||
|
||
Calculates the time difference (age) between a date and the current date. | ||
|
||
Calculates and returns the time difference (age) between a date and the current date. | ||
|
||
You use an accuracy: | ||
|
||
| symbol | accuracy | | ||
| ------ | -------- | | ||
| y | Year | | ||
| d | Day | | ||
| h | Hour | | ||
| i | Minute | | ||
| s | Seconds | | ||
|
||
The default is `y`. Symbols are case insensitive. | ||
|
||
#### Examples | ||
|
||
```Twig | ||
{# today is new \DateTime() #} | ||
{{ today|date_modify("-36 hours")|age('d') }} day. | ||
{# 1.5 day. #} | ||
{{ today|date_modify("180 minutes")|age('h') }} hours. | ||
{# -3 hours. note the "minus" "#} | ||
{{ today|date_modify("-180 minutes")|age('i') }} minutes. | ||
{# 180 minutes. #} | ||
{{ today|date_modify("-180 minutes")|age('s') }} seconds. | ||
{# 10800 seconds. #} | ||
``` | ||
|
||
Pass a timezone as second argument to set for the date passed. \*<br/> | ||
Pass a timezone as third argument for creating the current date. \* | ||
|
||
<sub>* Pass `null` to use the default, `false` to leave unchanged.</sub> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the GeckoPackages. | ||
* | ||
* (c) GeckoPackages https://github.com/GeckoPackages | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace GeckoPackages\Twig\Filters; | ||
|
||
use Twig_Environment; | ||
|
||
/** | ||
* Calculate the time diff (age) between a date and the current date. | ||
* | ||
* @api | ||
* | ||
* @author SpacePossum | ||
*/ | ||
class AgeFilter extends \Twig_SimpleFilter | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct( | ||
'age', | ||
/** | ||
* @param Twig_Environment $env | ||
* @param \DateTime|\DateTimeInterface|string|null $date | ||
* @param string $acc accuracy; any of 'y, d, h, i, s', default 'y' | ||
* @param \DateTimeZone|string|null|false $timezoneDate timezone of date passed, null to use the default, false to leave unchanged | ||
* @param \DateTimeZone|string|null|false $timezoneNow timezone of the current date, null to use the default, false to leave unchanged | ||
* | ||
* @return string | ||
*/ | ||
function (Twig_Environment $env, $date, $acc = 'y', $timezoneDate = null, $timezoneNow = null) { | ||
$now = time(); | ||
|
||
if (!is_string($acc)) { | ||
throw new \Twig_Error_Runtime(sprintf( | ||
'Accuracy must be string, got %s.', | ||
is_object($acc) ? get_class($acc) : gettype($acc).(is_resource($acc) ? '' : '#'.$acc) | ||
)); | ||
} | ||
|
||
$date = twig_date_converter($env, $date, $timezoneDate); | ||
$now = twig_date_converter($env, $now, $timezoneNow); | ||
$diff = $now->diff($date, false); | ||
|
||
switch (strtolower($acc)) { | ||
case 'y': | ||
$v = | ||
$diff->y | ||
+ ($diff->m / 12) | ||
+ (($diff->d + (($diff->h + (($diff->i + ($diff->s / 60)) / 60)) / 24)) / 365) | ||
; | ||
|
||
break; | ||
// case 'm' is not supported by design | ||
case 'd': | ||
$v = | ||
(int) $diff->format('%a') | ||
+ (($diff->h + (($diff->i + ($diff->s / 60)) / 60)) / 24) | ||
; | ||
|
||
break; | ||
case 'h': | ||
$v = | ||
24 * (int) $diff->format('%a') | ||
+ $diff->h | ||
+ (($diff->i + ($diff->s / 60)) / 60) | ||
; | ||
|
||
break; | ||
case 'i': | ||
$v = | ||
60 * 24 * (int) $diff->format('%a') | ||
+ 60 * $diff->h | ||
+ $diff->i | ||
+ ($diff->s / 60) | ||
; | ||
|
||
break; | ||
case 's': | ||
return (int) $now->format('U') - (int) $date->format('U'); | ||
default: | ||
throw new \Twig_Error_Runtime(sprintf('Accuracy must be any of "y, d, h, i, s", got "%s".', $acc)); | ||
} | ||
|
||
// (int) cast for HHVM =< 3.9.10 | ||
// https://github.com/facebook/hhvm/pull/6134 / https://github.com/facebook/hhvm/issues/5537 | ||
return 1 === (int) $diff->invert ? $v : -1 * $v; | ||
}, | ||
['needs_environment' => true] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the GeckoPackages. | ||
* | ||
* (c) GeckoPackages https://github.com/GeckoPackages | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* @author SpacePossum | ||
* | ||
* @internal | ||
*/ | ||
final class AgeTest extends AbstractFilterTest | ||
{ | ||
/** | ||
* @expectedException Twig_Error_Runtime | ||
* @expectedExceptionMessageRegExp #^Accuracy must be string, got NULL\#.$# | ||
*/ | ||
public function testFilterInvalidAccType() | ||
{ | ||
$this->callFilter($this->getEnvironment(), time(), null); | ||
} | ||
|
||
/** | ||
* @expectedException Twig_Error_Runtime | ||
* @expectedExceptionMessageRegExp #^Accuracy must be any of \"y, d, h, i, s\", got "invalid".$# | ||
*/ | ||
public function testFilterInvalidAccValue() | ||
{ | ||
$this->callFilter($this->getEnvironment(), time(), 'invalid'); | ||
} | ||
|
||
/** | ||
* @param int $expected | ||
* @param string $acc @see AgeFilter | ||
* @param string $input Input for \DateTime constructor | ||
* | ||
* @dataProvider provideCases | ||
*/ | ||
public function testFilter($expected, $acc, $input) | ||
{ | ||
$this->assertSame($expected, $this->callFilter($this->getEnvironment(), new \DateTime($input), $acc)); | ||
} | ||
|
||
public function provideCases() | ||
{ | ||
return [ | ||
[3, 'h', '-180 minutes'], | ||
[180, 'i', '-180 minutes'], | ||
[180 * 60, 's', '-180 minutes'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters