Skip to content

Commit

Permalink
[TASK] Add local date function (#13)
Browse files Browse the repository at this point in the history
* [TASK] Add no time function

* [TASK] Add template

* [TASK] enable function

* [TASK] Rename to localdatetime

* [TASK] Readme
  • Loading branch information
Woeler authored Aug 6, 2020
1 parent b50c907 commit 9926a54
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 10 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ This package is used across various TYPO3 Symfony Applications to ensure a strea
- [`template_function_call`](#template_function_call)
- [DateTimeExtension](#datetimeextension)
- [`to_datetime`](#to_datetime)
- [`localtime`](#localtime)
- [`localdate`](#localdate)
- [`localdatetime`](#localdatetime)
- [`relativetime`](#relativetime)
- [Twig Tags](#twig-tags)
- [`frame`](#frame)
Expand Down Expand Up @@ -476,12 +477,21 @@ Converts a unix timestamp to datetime object.
{{ to_datetime(timestamp) }}
```

### `localtime`
### `localdate`

Returns a localized string representing this date.

```twig
{{ localtime(datetimeObject) }}
{{ localdate(datetimeObject) }}
```


### `localdatetime`

Returns a localized string representing this datetime.

```twig
{{ localdatetime(datetimeObject) }}
```

### `relativetime`
Expand Down
15 changes: 13 additions & 2 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import 'prismjs/components/prism-rest';

import {DateTime} from 'luxon';

function convertDates() {
Array.from(document.querySelectorAll('[data-processor="localdate"]')).forEach(function (element) {
function convertDateTimes() {
Array.from(document.querySelectorAll('[data-processor="localdatetime"]')).forEach(function (element) {
const value = element.dataset.value;
element.textContent = DateTime.fromISO(value).toLocaleString({
month: '2-digit',
Expand All @@ -37,6 +37,16 @@ function convertDates() {
});
});
}
function convertDates() {
Array.from(document.querySelectorAll('[data-processor="localdate"]')).forEach(function (element) {
const value = element.dataset.value;
element.textContent = DateTime.fromISO(value).toLocaleString({
month: '2-digit',
day: '2-digit',
year: '2-digit',
});
});
}
function convertRelativeTime() {
Array.from(document.querySelectorAll('[data-processor="relativetime"]')).forEach(function (element) {
const value = element.dataset.value;
Expand Down Expand Up @@ -67,6 +77,7 @@ function initializeFileInput() {
document.onreadystatechange = function () {
if (document.readyState == "interactive") {
convertDates();
convertDateTimes();
convertRelativeTime();
initializeExpander();
initializeFileInput();
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"knplabs/knp-paginator-bundle": "^3.0 || ^4.0 || ^5.0",
"symfony/asset": "^4.4 || ^5.0",
"symfony/dependency-injection": "^4.4 || ^5.0",
"symfony/deprecation-contracts": "^2.1",
"symfony/framework-bundle": "^4.4 || ^5.0",
"symfony/http-kernel": "^4.4 || ^5.0",
"symfony/security-bundle": "^4.4 || ^5.0",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Resources/public/entrypoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"/bundles/template/app.90c58c6a.css"
],
"js": [
"/bundles/template/app.84f327d7.js"
"/bundles/template/app.93e4d5ee.js"
]
},
"webfont": {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"bundles/template/0.5d481e02.js": "/bundles/template/0.5d481e02.js",
"bundles/template/1.495a8cee.js": "/bundles/template/1.495a8cee.js",
"bundles/template/app.css": "/bundles/template/app.90c58c6a.css",
"bundles/template/app.js": "/bundles/template/app.84f327d7.js",
"bundles/template/app.js": "/bundles/template/app.93e4d5ee.js",
"bundles/template/webfont.css": "/bundles/template/webfont.adda838f.css",
"bundles/template/fonts/fa-brands-400.woff2": "/bundles/template/fonts/fa-brands-400.31015e86.woff2",
"bundles/template/fonts/fa-brands-400.ttf": "/bundles/template/fonts/fa-brands-400.5818bbd0.ttf",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if datetime %}
<span data-processor="localdate" data-value="{{ datetime|date('Y-m-d\\TH:i:sP', "UTC") }}">
{{ datetime|date('m/d/y, H:i', "UTC") }}
{{ datetime|date('m/d/y', "UTC") }}
</span>
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% if datetime %}
<span data-processor="localdatetime" data-value="{{ datetime|date('Y-m-d\\TH:i:sP', "UTC") }}">
{{ datetime|date('m/d/y, H:i', "UTC") }}
</span>
{% endif %}
18 changes: 17 additions & 1 deletion src/Twig/Extension/DateTimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function getFunctions(): array
return [
new TwigFunction('to_datetime', [$this, 'toDateTime']),
new TwigFunction('localtime', [$this, 'localtime'], ['needs_environment' => true, 'is_safe' => ['html']]),
new TwigFunction('localdatetime', [$this, 'localdatetime'], ['needs_environment' => true, 'is_safe' => ['html']]),
new TwigFunction('localdate', [$this, 'localdate'], ['needs_environment' => true, 'is_safe' => ['html']]),
new TwigFunction('relativetime', [$this, 'relativetime'], ['needs_environment' => true, 'is_safe' => ['html']]),
];
}
Expand All @@ -31,9 +33,23 @@ public function toDateTime(string $date, string $format = 'Y/m/d H:i:s'): \DateT
return $datetime;
}

/**
* @deprecated use localdatetime instead
*/
public function localtime(Environment $environment, \DateTimeInterface $datetime = null): string
{
return $environment->render('@Template/extension/datetime/localtime.html.twig', ['datetime' => $datetime]);
trigger_deprecation('t3g/symfony-template-bundle', '2.11.2', 'localtime is deprecated, use localdatetime instead');
return $this->localdatetime($environment, $datetime);
}

public function localdatetime(Environment $environment, \DateTimeInterface $datetime = null): string
{
return $environment->render('@Template/extension/datetime/localdatetime.html.twig', ['datetime' => $datetime]);
}

public function localdate(Environment $environment, \DateTimeInterface $datetime = null): string
{
return $environment->render('@Template/extension/datetime/localdate.html.twig', ['datetime' => $datetime]);
}

public function relativetime(Environment $environment, \DateTimeInterface $datetime = null): string
Expand Down

0 comments on commit 9926a54

Please sign in to comment.