diff --git a/app/controllers/landing_page_controller.rb b/app/controllers/landing_page_controller.rb index 968cbd9..516fa1c 100644 --- a/app/controllers/landing_page_controller.rb +++ b/app/controllers/landing_page_controller.rb @@ -18,7 +18,7 @@ class LandingPageController < ApplicationController def index @hours = Settings.hours_locations.map { |hours_config| LibraryHours.from_config(hours_config) } @collection_count = site_collection_count - @next_half_hour = TimeService.next_half_hour + @seconds_until_next_half_hour = TimeService.seconds_until_next_half_hour end private diff --git a/app/services/time_service.rb b/app/services/time_service.rb index 8d13beb..4611d7b 100644 --- a/app/services/time_service.rb +++ b/app/services/time_service.rb @@ -4,10 +4,15 @@ class TimeService def self.next_half_hour now = Time.current - next30 = now.at_beginning_of_hour + (now.min < 30 ? 30.minutes : 60.minutes) + now.at_beginning_of_hour + (now.min < 30 ? 30.minutes : 60.minutes) + end + + def self.seconds_until_next_half_hour + delta = next_half_hour - Time.current + return 5.seconds if delta < 5.seconds - return next30 + 5.seconds if next30 - now < 5.seconds + return 30.minutes if delta > 30.minutes - next30 + delta end end diff --git a/app/views/landing_page/_cards.html.erb b/app/views/landing_page/_cards.html.erb index c4452af..87154be 100644 --- a/app/views/landing_page/_cards.html.erb +++ b/app/views/landing_page/_cards.html.erb @@ -4,7 +4,7 @@ <%= image_tag 'locations.png', alt: '', class: 'card-img-top' %>

<%= t('.location.title') %>

- <% cache('location_hours', expires_at: @next_half_hour) do %> + <% cache('location_hours', expires_in: @seconds_until_next_half_hour) do %>