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
13 changes: 11 additions & 2 deletions app/helpers/answer_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ 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)
end
formatted_time = answer.in_time_zone.to_formatted_s(:govuk_time_with_period)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do the case test before casting the time to a format


# Convert "12:00pm" to "midday" and "12:00am" to "midnight" as per GDS guidance
answer = case formatted_time
when "12:00pm"
"midday"
when "12:00am"
"midnight"
else
formatted_time
end
end
safe_format(answer.to_s, wrapper_tag: "span")
end

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