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.
This commit is contained in:
songtianlun 2025-02-14 13:42:42 +08:00
parent 681ad5320f
commit f43a6b4698
7 changed files with 38 additions and 9 deletions

View File

@ -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

View File

@ -28,7 +28,7 @@
<div class="text-sm text-base-content/60 pt-4">
Latest from <%= featured_art.city.name %>, <%= featured_art.city.country.name %>
<span class="mx-2">•</span>
<%= featured_art.weather_date.strftime("%B %d, %Y") %>
<%= featured_art.formatted_time(:date) %>
</div>
<% end %>
</div>
@ -139,7 +139,7 @@
<%= art.city.name %>
</h3>
<p class="text-sm text-base-content/70">
<%= art.weather_date.strftime("%B %d, %Y") %>
<%= art.formatted_time(:date, true) %>
</p>
</div>
<div class="text-right">

View File

@ -51,7 +51,7 @@
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<%= city.latest_weather_art.weather_date.strftime("%b %d, %Y") %>
<%= city.latest_weather_art.formatted_time(:date) %>
</div>
</div>

View File

@ -29,7 +29,7 @@
<span class="font-semibold"><%= featured_art.city.name %></span>,
<%= featured_art.city.country.name %>
<span class="mx-2">•</span>
<%= featured_art.weather_date.strftime("%B %d, %Y") %>
<%= featured_art.formatted_time(:date) %>
</div>
<% end %>
<%= render 'cities/search_city' %>

View File

@ -110,8 +110,8 @@
<div class="flex items-center justify-between text-white">
<div class="text-2xl font-bold"><%= art.temperature %>°C</div>
<div class="text-right">
<div class="font-medium">UTC <%= art.created_at.strftime("%H:%M") %></div>
<div class="text-sm opacity-80"><%= art.weather_date.strftime("%B %d, %Y") %></div>
<div class="font-medium"><%= art.formatted_time(:date) %></div>
<div class="text-sm opacity-80"><%= art.formatted_time(:time, true) %></div>
</div>
</div>
</div>

View File

@ -11,7 +11,7 @@
<div>
<h3 class="card-title font-display"><%= art.city.name %></h3>
<p class="text-base-content/70">
<%= art.weather_date.strftime("%B %d, %Y") %>
<%= art.formatted_time(:date) %>
</p>
</div>
<div class="text-right">

View File

@ -52,10 +52,10 @@
<div class="flex flex-wrap gap-4 mb-6">
<div class="badge badge-lg badge-primary">
<%= @weather_art.weather_date.strftime("%B %d, %Y") %>
<%= @weather_art.formatted_time(:date) %>
</div>
<div class="badge badge-lg badge-secondary">
UTC <%= @weather_art.created_at.strftime("%H:%M") %>
<%= @weather_art.formatted_time(:time, true) %>
</div>
</div>