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) %>