From 0ae5593dded95100cf1210217389527a3033ad19 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 9 Nov 2024 20:38:30 +0100 Subject: [PATCH] Playing around with local date time parsing. --- app/Casts/SeparateTimezoneCaster.php | 5 +---- public/v1/js/ff/firefly.js | 13 +++++++++++++ resources/lang/en_US/firefly.php | 1 + resources/views/javascript/variables.twig | 1 + resources/views/transactions/show.twig | 4 +++- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/Casts/SeparateTimezoneCaster.php b/app/Casts/SeparateTimezoneCaster.php index ef12471230..726a667c67 100644 --- a/app/Casts/SeparateTimezoneCaster.php +++ b/app/Casts/SeparateTimezoneCaster.php @@ -3,7 +3,6 @@ namespace FireflyIII\Casts; use Carbon\Carbon; -use Carbon\CarbonImmutable; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Database\Eloquent\Model; @@ -19,13 +18,11 @@ use Illuminate\Database\Eloquent\Model; class SeparateTimezoneCaster implements CastsAttributes { /** - * Cast the given value. - * * @param array $attributes */ public function get(Model $model, string $key, mixed $value, array $attributes): ?Carbon { - if('' === $value || null === $value) { + if ('' === $value || null === $value) { return null; } $timeZone = $attributes[sprintf('%s_tz', $key)] ?? config('app.timezone'); diff --git a/public/v1/js/ff/firefly.js b/public/v1/js/ff/firefly.js index 00a07b80ea..e7d8fa2041 100644 --- a/public/v1/js/ff/firefly.js +++ b/public/v1/js/ff/firefly.js @@ -26,6 +26,16 @@ $.ajaxSetup({ } }); +function parseToLocalDates() { + "use strict"; + $('span.date-time').each(function () { + var date = $(this).data('date'); + var obj = moment.utc(date).local(); + var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; + $(this).text(obj.format(date_time_js) + ' ('+ timeZone +')'); + }); +} + $(function () { "use strict"; @@ -96,6 +106,9 @@ $(function () { // trigger list thing listLengthInitial(); + // update dates: + parseToLocalDates(); + }); function currencySelect(e) { diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 7cf8cd83df..55e48a4bc1 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -26,6 +26,7 @@ declare(strict_types=1); return [ // general stuff: + 'stored_as_tz' => 'stored as ":timezone"', 'close' => 'Close', 'actions' => 'Actions', 'edit' => 'Edit', diff --git a/resources/views/javascript/variables.twig b/resources/views/javascript/variables.twig index a6676011ff..37c6ab8c13 100644 --- a/resources/views/javascript/variables.twig +++ b/resources/views/javascript/variables.twig @@ -44,6 +44,7 @@ var todayText = ' {{ trans('firefly.today')|escape('js') }}'; // some formatting stuff: var month_and_day_js = "{{ trans('config.month_and_day_js') }}"; +var date_time_js = "{{ trans('config.date_time_js') }}"; var acc_config_new = {format: accountingConfig}; // strings and translations used often: diff --git a/resources/views/transactions/show.twig b/resources/views/transactions/show.twig index 82307440a7..b4992e28a8 100644 --- a/resources/views/transactions/show.twig +++ b/resources/views/transactions/show.twig @@ -74,10 +74,12 @@ {{ trans('list.date') }} + {{ first.date.isoFormat(dateTimeFormat) }} {% if(first.date_tz != '') %} - ({{ first.date_tz }}) + ({{ trans('firefly.stored_as_tz', {timezone: first.date_tz }) }}) {% endif %} +