From f43a6b4698be4a239998f65413b56a08fe34fc15 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Fri, 14 Feb 2025 13:42:42 +0800 Subject: [PATCH] feat: add formatted time method for weather_art - Introduce a new method `formatted_time` in the `WeatherArt` model - Update various views to use this new method for date and time display - Support formatting in local time zones or UTC This update enhances the time representation for weather data, ensuring that displayed times can reflect the user's local timezone or remain fixed at UTC. This improves the usability of the application for users in different regions. --- app/models/weather_art.rb | 29 ++++++++++++++++++++++++++++ app/views/arts/index.html.erb | 4 ++-- app/views/cities/_city.html.erb | 2 +- app/views/cities/index.html.erb | 2 +- app/views/cities/show.html.erb | 4 ++-- app/views/home/_arts.html.erb | 2 +- app/views/weather_arts/show.html.erb | 4 ++-- 7 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/models/weather_art.rb b/app/models/weather_art.rb index f624037..6c37484 100644 --- a/app/models/weather_art.rb +++ b/app/models/weather_art.rb @@ -68,6 +68,35 @@ class WeatherArt < ApplicationRecord end end + def formatted_time(type = :date, use_local_timezone = false) + # 获取时区 + timezone_info = city&.country&.timezones.present? ? + eval(self.city.country.timezones).first : + { "zoneName" => "UTC", "gmtOffsetName" => "UTC+00:00" } + + # 设置时区对象 + time_zone = ActiveSupport::TimeZone[timezone_info["zoneName"]] || + ActiveSupport::TimeZone["UTC"] + + case type + when :date + # 格式化日期 + self&.weather_date&.strftime("%B %d, %Y") + when :time + # 获取时间 + time = updated_at + + if use_local_timezone + # 使用本地时区 + local_time = time.in_time_zone(time_zone) + "#{local_time.strftime('%H:%M')} #{timezone_info['gmtOffsetName']}" + else + # 使用 UTC + "#{time.utc.strftime('%H:%M')} UTC" + end + end + end + def image_url image.attached? ? image.blob : nil end diff --git a/app/views/arts/index.html.erb b/app/views/arts/index.html.erb index 42a55b1..5499093 100644 --- a/app/views/arts/index.html.erb +++ b/app/views/arts/index.html.erb @@ -28,7 +28,7 @@
Latest from <%= featured_art.city.name %>, <%= featured_art.city.country.name %> - <%= featured_art.weather_date.strftime("%B %d, %Y") %> + <%= featured_art.formatted_time(:date) %>
<% end %> @@ -139,7 +139,7 @@ <%= art.city.name %>

- <%= art.weather_date.strftime("%B %d, %Y") %> + <%= art.formatted_time(:date, true) %>

diff --git a/app/views/cities/_city.html.erb b/app/views/cities/_city.html.erb index 438d472..06e2753 100644 --- a/app/views/cities/_city.html.erb +++ b/app/views/cities/_city.html.erb @@ -51,7 +51,7 @@ - <%= city.latest_weather_art.weather_date.strftime("%b %d, %Y") %> + <%= city.latest_weather_art.formatted_time(:date) %>
diff --git a/app/views/cities/index.html.erb b/app/views/cities/index.html.erb index 3706f00..b5aae1c 100644 --- a/app/views/cities/index.html.erb +++ b/app/views/cities/index.html.erb @@ -29,7 +29,7 @@ <%= featured_art.city.name %>, <%= featured_art.city.country.name %> - <%= featured_art.weather_date.strftime("%B %d, %Y") %> + <%= featured_art.formatted_time(:date) %> <% end %> <%= render 'cities/search_city' %> diff --git a/app/views/cities/show.html.erb b/app/views/cities/show.html.erb index 9040c6f..c26d5c9 100644 --- a/app/views/cities/show.html.erb +++ b/app/views/cities/show.html.erb @@ -110,8 +110,8 @@
<%= art.temperature %>°C
-
UTC <%= art.created_at.strftime("%H:%M") %>
-
<%= art.weather_date.strftime("%B %d, %Y") %>
+
<%= art.formatted_time(:date) %>
+
<%= art.formatted_time(:time, true) %>
diff --git a/app/views/home/_arts.html.erb b/app/views/home/_arts.html.erb index bf432bb..bb127f0 100644 --- a/app/views/home/_arts.html.erb +++ b/app/views/home/_arts.html.erb @@ -11,7 +11,7 @@

<%= art.city.name %>

- <%= art.weather_date.strftime("%B %d, %Y") %> + <%= art.formatted_time(:date) %>

diff --git a/app/views/weather_arts/show.html.erb b/app/views/weather_arts/show.html.erb index 9c46497..7e6547f 100644 --- a/app/views/weather_arts/show.html.erb +++ b/app/views/weather_arts/show.html.erb @@ -52,10 +52,10 @@
- <%= @weather_art.weather_date.strftime("%B %d, %Y") %> + <%= @weather_art.formatted_time(:date) %>
- UTC <%= @weather_art.created_at.strftime("%H:%M") %> + <%= @weather_art.formatted_time(:time, true) %>