Paul Robert Lloyd
3 years ago
committed by
GitHub
7 changed files with 87 additions and 11 deletions
@ -0,0 +1,41 @@
|
||||
Time::DATE_FORMATS[:govuk_date] = "%-d %B %Y" |
||||
Time::DATE_FORMATS[:govuk_date_short_month] = "%-d %b %Y" |
||||
Date::DATE_FORMATS[:govuk_date] = "%-d %B %Y" |
||||
Date::DATE_FORMATS[:govuk_date_short_month] = "%-d %b %Y" |
||||
|
||||
Time::DATE_FORMATS[:month_and_year] = "%B %Y" |
||||
Date::DATE_FORMATS[:month_and_year] = "%B %Y" |
||||
|
||||
Time::DATE_FORMATS[:short_month_and_year] = "%b %Y" |
||||
Date::DATE_FORMATS[:short_month_and_year] = "%b %Y" |
||||
|
||||
Time::DATE_FORMATS[:day_and_month] = "%-d %B" |
||||
Date::DATE_FORMATS[:day_and_month] = "%-d %B" |
||||
|
||||
Time::DATE_FORMATS[:govuk_date_and_time] = lambda do |time| |
||||
format = if time >= time.midday && time <= time.midday.end_of_minute |
||||
"%e %B %Y at %l%P (midday)" |
||||
elsif time >= time.midnight && time <= time.midnight.end_of_minute |
||||
"%e %B %Y at %l%P (midnight)" |
||||
elsif time.min.zero? |
||||
"%e %B %Y at %l%P" |
||||
else |
||||
"%e %B %Y at %l:%M%P" |
||||
end |
||||
|
||||
time.strftime(format).squish |
||||
end |
||||
|
||||
Time::DATE_FORMATS[:govuk_time] = lambda do |time| |
||||
format = if time >= time.midday && time <= time.midday.end_of_minute |
||||
"%l%P (midday)" |
||||
elsif time >= time.midnight && time <= time.midnight.end_of_minute |
||||
"%l%P (midnight)" |
||||
elsif time.min.zero? |
||||
"%l%P" |
||||
else |
||||
"%l:%M%P" |
||||
end |
||||
|
||||
time.strftime(format).squish |
||||
end |
@ -0,0 +1,35 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe "Time::DATE_FORMATS" do |
||||
describe ":govuk_date_and_time, :govuk_time formats" do |
||||
it "formats time to indicate midday" do |
||||
date_and_time = Time.zone.local(2020, 12, 25, 12, 0, 59) |
||||
expect(date_and_time.to_formatted_s(:govuk_date_and_time)).to eq("25 December 2020 at 12pm (midday)") |
||||
expect(date_and_time.to_formatted_s(:govuk_time)).to eq("12pm (midday)") |
||||
end |
||||
|
||||
it "formats time to indicate midnight" do |
||||
date_and_time = Time.zone.local(2020, 12, 25, 0, 0, 59) |
||||
expect(date_and_time.to_formatted_s(:govuk_date_and_time)).to eq("25 December 2020 at 12am (midnight)") |
||||
expect(date_and_time.to_formatted_s(:govuk_time)).to eq("12am (midnight)") |
||||
end |
||||
|
||||
it "formats time with minutes if the time is not on the hour" do |
||||
date_and_time = Time.zone.local(2020, 12, 25, 12, 1, 0) |
||||
expect(date_and_time.to_formatted_s(:govuk_date_and_time)).to eq("25 December 2020 at 12:01pm") |
||||
expect(date_and_time.to_formatted_s(:govuk_time)).to eq("12:01pm") |
||||
end |
||||
|
||||
it "formats time without minutes if the time is on the hour" do |
||||
date_and_time = Time.zone.local(2020, 12, 25, 15) |
||||
expect(date_and_time.to_formatted_s(:govuk_date_and_time)).to eq("25 December 2020 at 3pm") |
||||
expect(date_and_time.to_formatted_s(:govuk_time)).to eq("3pm") |
||||
end |
||||
|
||||
it "does not pad single-digit day and hour with whitespace" do |
||||
date_and_time = Time.zone.local(2020, 12, 5, 6, 10, 0) |
||||
expect(date_and_time.to_formatted_s(:govuk_date_and_time)).to eq("5 December 2020 at 6:10am") |
||||
expect(date_and_time.to_formatted_s(:govuk_time)).to eq("6:10am") |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue