Skip to content

Commit 5c49bfa

Browse files
committed
fix for local time issue and spec
1 parent 721a80e commit 5c49bfa

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

app/controllers/admin/notifications_controller.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class Admin::NotificationsController < AdminController
44
def index
55
markdown_parser = Redcarpet::Markdown.new(CustomMarkdownRenderer)
6-
@published_notification = Notification.published.first
6+
@published_notification = Notification.currently_active.first
77
if @published_notification
88
@published_notification_message = markdown_parser.render(@published_notification[:notification_message])
99
end
@@ -62,6 +62,14 @@ def create_notifications
6262
user: current_user['email'],
6363
published: true,
6464
published_at: Time.zone.now,
65-
stop_datetime: notification_params[:stop_datetime])
65+
stop_datetime: parsed_stop_datetime)
66+
end
67+
68+
def parsed_stop_datetime
69+
return nil if notification_params[:stop_datetime].blank?
70+
71+
Time.use_zone('Europe/London') do
72+
Time.zone.parse(notification_params[:stop_datetime])
73+
end
6674
end
6775
end

app/views/admin/notifications/new.html.haml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
= @notification.errors[:stop_datetime].join(', ')
2828
2929
- current_date = @notification.stop_datetime || @published_notification&.stop_datetime
30+
- current_date_london = current_date&.in_time_zone('Europe/London')
3031
= form.datetime_local_field :stop_datetime,
3132
class: "govuk-input",
32-
value: current_date&.strftime("%Y-%m-%dT%H:%M")
33+
value: current_date_london&.strftime("%Y-%m-%dT%H:%M")
3334
%button.govuk-button.govuk-button--secondary{ type: "button", onclick: "document.getElementById('notification_stop_datetime').value = ''", class: "govuk-!-margin-top-1" }
3435
Clear date
3536

spec/requests/admin/notifications_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,33 @@
99
get '/auth/google_oauth2/callback'
1010
end
1111

12+
describe '#create' do
13+
around do |example|
14+
travel_to(Time.utc(2026, 4, 20, 12, 0, 0)) { example.run }
15+
end
16+
17+
it 'stores stop_datetime as Europ/London local time converted to UTC' do
18+
london_input = '2026-04-20T14:15:00' # 2:15 PM London time on April 20, 2026
19+
20+
expect do
21+
post admin_notifications_path, params: {
22+
notification: {
23+
summary: 'Test Notification',
24+
notification_message: 'This is a test notification.',
25+
stop_datetime: london_input
26+
}
27+
}
28+
end.to change(Notification, :count).by(1)
29+
30+
notification = Notification.order(published_at: :desc).first
31+
32+
expected_time = ActiveSupport::TimeZone['Europe/London'].parse(london_input)
33+
34+
expect(notification.stop_datetime).to eq(expected_time)
35+
expect(notification.stop_datetime.utc).to eq(expected_time.utc)
36+
end
37+
end
38+
1239
describe '#preview' do
1340
it 'renders the Markdown content as HTML' do
1441
markdown_content = '**Bold Text**'

0 commit comments

Comments
 (0)