Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time format check answers #4394

Merged
merged 8 commits into from
Jan 20, 2025
9 changes: 8 additions & 1 deletion app/helpers/answer_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ def format_answer(answer)
when Date
answer = answer.to_formatted_s(:govuk_date)
when Time
answer = answer.in_time_zone.to_formatted_s(:govuk_time)
local_time = answer.in_time_zone
answer = if local_time.hour.zero? && local_time.min.zero?
"Midnight"
elsif local_time.hour == 12 && local_time.min.zero?
"Midday"
else
local_time.to_formatted_s(:govuk_time_with_period)
end
end

safe_format(answer.to_s, wrapper_tag: "span")
Expand Down
14 changes: 12 additions & 2 deletions spec/helpers/answer_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@

it "correctly formats a time" do
answer = Time.utc(2011, 1, 24, 10, 30)
expect(helper.format_answer(answer)).to eq("<span>10:30</span>")
expect(helper.format_answer(answer)).to eq("<span>10:30am</span>")
end

it "correctly formats a time of midday" do
answer = Time.utc(2011, 1, 24, 12, 0)
expect(helper.format_answer(answer)).to eq("<span>Midday</span>")
end

it "correctly formats a time of midnight" do
answer = Time.utc(2011, 1, 24, 0, 0)
expect(helper.format_answer(answer)).to eq("<span>Midnight</span>")
end

it "calls safe_format" do
Expand All @@ -26,7 +36,7 @@

it "correctly formats a time" do
answer = Time.utc(2011, 1, 24, 10, 30)
expect(helper.format_answer(answer)).to eq("<span>00:30</span>")
expect(helper.format_answer(answer)).to eq("<span>12:30am</span>")
end
end
end
Expand Down
Loading