diff --git a/app/models/banner.rb b/app/models/banner.rb index 999ab2af09..155bc69974 100644 --- a/app/models/banner.rb +++ b/app/models/banner.rb @@ -12,6 +12,8 @@ def expired? expires_at && Time.current > expires_at end + # `expires_at` is stored in the database as UTC, but timezone information will be stripped before displaying on frontend + # so this method converts the time to the user's timezone before displaying it def expires_at_in_time_zone(timezone) expires_at&.in_time_zone(timezone) end diff --git a/app/values/banner_parameters.rb b/app/values/banner_parameters.rb index b6ffc5a293..9d8e631a83 100644 --- a/app/values/banner_parameters.rb +++ b/app/values/banner_parameters.rb @@ -4,7 +4,7 @@ def initialize(params, user, timezone) new_params = params.require(:banner).permit(:active, :content, :name, :expires_at).merge(user: user) if params.dig(:banner, :expires_at) - new_params[:expires_at] = convert_expires_at_in_user_time_zone(params, timezone) + new_params[:expires_at] = convert_expires_at_with_user_time_zone(params, timezone) end super(new_params) @@ -12,7 +12,10 @@ def initialize(params, user, timezone) private - def convert_expires_at_in_user_time_zone(params, timezone) + # `expires_at` comes from the frontend without any timezone information, so we use `in_time_zone` to attach + # timezone information to it before saving to the database. If we don't do this, the time will be stored at UTC + # by default. + def convert_expires_at_with_user_time_zone(params, timezone) params[:banner][:expires_at].in_time_zone(timezone) end