From 056a14cee101a5c0ffbd085830d135ad2a01a576 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 28 Aug 2024 21:45:58 +0200 Subject: [PATCH] Render Datetime ingredient in local time zone. Datetime values are stored in UTC in the database. We need to render them in the local timezone. (cherry picked from commit 226c97fc26637918586a6762919819fe75bfcbb4) --- .../alchemy/ingredients/datetime_view.rb | 5 +++-- .../alchemy/ingredients/datetime_spec.rb | 6 +++++- .../alchemy/ingredients/datetime_view_spec.rb | 20 ++++++++++++++----- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/app/components/alchemy/ingredients/datetime_view.rb b/app/components/alchemy/ingredients/datetime_view.rb index d0890c89b2..290c5fd9c9 100644 --- a/app/components/alchemy/ingredients/datetime_view.rb +++ b/app/components/alchemy/ingredients/datetime_view.rb @@ -11,10 +11,11 @@ def initialize(ingredient, date_format: nil, html_options: {}) end def call + datetime = ingredient.value.in_time_zone(Rails.application.config.time_zone) if date_format == "rfc822" - ingredient.value.to_s(:rfc822) + datetime.to_s(:rfc822) else - ::I18n.l(ingredient.value, format: date_format) + ::I18n.l(datetime, format: date_format) end.html_safe end end diff --git a/spec/models/alchemy/ingredients/datetime_spec.rb b/spec/models/alchemy/ingredients/datetime_spec.rb index 4df72dd9c5..af542ef7a3 100644 --- a/spec/models/alchemy/ingredients/datetime_spec.rb +++ b/spec/models/alchemy/ingredients/datetime_spec.rb @@ -17,13 +17,17 @@ end describe "value" do - subject { datetime_ingredient.value } + subject(:value) { datetime_ingredient.value } it "returns a time object" do is_expected.to be_an(Time) is_expected.to eq("01.04.2021") end + it "timezone is UTC" do + expect(value.zone).to eq("UTC") + end + context "without value" do let(:datetime_ingredient) do described_class.new( diff --git a/spec/views/alchemy/ingredients/datetime_view_spec.rb b/spec/views/alchemy/ingredients/datetime_view_spec.rb index 8d7c003c66..d7ff20793c 100644 --- a/spec/views/alchemy/ingredients/datetime_view_spec.rb +++ b/spec/views/alchemy/ingredients/datetime_view_spec.rb @@ -3,7 +3,17 @@ require "rails_helper" describe "alchemy/ingredients/_datetime_view" do - let(:ingredient) { Alchemy::Ingredients::Datetime.new(value: "2013-10-27 21:14:16 +0100") } + around do |example| + time_zone = Rails.application.config.time_zone + Rails.application.config.time_zone = "Berlin" + example.run + Rails.application.config.time_zone = time_zone + end + + let(:ingredient) do + Alchemy::Ingredients::Datetime.new(value: "2024-08-29T10:00:00.000Z") + end + let(:options) { {} } before do @@ -13,8 +23,8 @@ context "with date value" do context "without date_format passed" do it "translates the date value with default format" do - render ingredient - expect(rendered).to have_content("Sun, 27 Oct 2013 20:14:16 +0000") + render ingredient, options: options + expect(rendered).to have_content("Thu, 29 Aug 2024 12:00:00 +0200") end end @@ -22,8 +32,8 @@ let(:options) { {date_format: "rfc822"} } it "renders the date rfc822 conform" do - render ingredient - expect(rendered).to have_content("Sun, 27 Oct 2013 20:14:16 +0000") + render ingredient, options: options + expect(rendered).to have_content("Thu, 29 Aug 2024 12:00:00 +0200") end end end