Skip to content

Commit

Permalink
Add clarifying comments on "with" or "in" time_zone
Browse files Browse the repository at this point in the history
Frontend->Backend: We don't have timezone so we have to attach timezone from browser
Backend->Frontend: We have timezone UTC and need to convert it to the
browser timezone before displaying
  • Loading branch information
albertchae committed Jun 1, 2024
1 parent 2619a57 commit d77fa38
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/models/banner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions app/values/banner_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ 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)
end

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

Expand Down

0 comments on commit d77fa38

Please sign in to comment.