-
-
Save johana-star/fc4cf51c0464008f38ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support/all" | |
def format_date_range(from, to = false) | |
to = from if to.blank? | |
return '' if from.nil? && to.nil? | |
from, to = [to, from].sort | |
if from == to | |
month_day_year(from) | |
elsif from.year == to.year | |
format_date_range_in_the_same_year(from, to) | |
elsif from.beginning_of_month == from && to.end_of_month == to | |
"#{month_year(from)} – #{month_year(to)}" | |
else | |
"#{month_day_year(from)} – #{month_day_year(to)}" | |
end.squish | |
end | |
def format_date_range_in_the_same_year(from, to) | |
if from.month == to.month && from.beginning_of_month == from && to.end_of_month == to | |
month_year(from) | |
elsif from.beginning_of_month == from && to.end_of_month == to | |
"#{from.strftime('%B')}–#{month_year(to)}" | |
elsif from.month == to.month | |
"#{from.strftime('%B %e')}–#{to.strftime('%e, %Y')}" | |
else | |
"#{from.strftime('%B %e')} – #{month_day_year(to)}" | |
end | |
end | |
def month_year(date) | |
date.strftime('%B %Y') | |
end | |
def month_day_year(date) | |
date.strftime('%B %e, %Y') | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "./format_date_range" | |
require "date" | |
require "minitest/autorun" | |
describe "#format_date_range" do | |
describe "date ranges, including year threshold and full month" do | |
it "formats dates in the same month" do | |
assert_equal "April 2–17, 2014", | |
format_date_range(Date.new(2014,4,2),Date.new(2014,4,17)) | |
end | |
it "formats dates in different months and years." do | |
assert_equal "December 13, 2013 – January 4, 2014", | |
format_date_range(Date.new(2013,12,13),Date.new(2014,1,4)) | |
end | |
it "formats dates at the beginning and end of different months" do | |
assert_equal "May–June 2014", | |
format_date_range(Date.new(2014,5,1),Date.new(2014,6,30)) | |
end | |
it "formats dates at the beginning and end of different months in different years" do | |
assert_equal "October 2013 – June 2014", | |
format_date_range(Date.new(2013,10,1),Date.new(2014,6,30)) | |
end | |
it "formats the first and last of a month to the month itself" do | |
assert_equal "April 2014", | |
format_date_range(Date.new(2014,4,1),Date.new(2014,4,30)) | |
end | |
end | |
describe "dates in reverse order" do | |
it "formats dates in the same month" do | |
assert_equal "April 2–17, 2014", | |
format_date_range(Date.new(2014,4,17),Date.new(2014,4,2)) | |
end | |
it "formats dates in different months and years" do | |
assert_equal "December 13, 2013 – January 4, 2014", | |
format_date_range(Date.new(2014,1,4),Date.new(2013,12,13)) | |
end | |
it "formats the last and first date of the month to be the month" do | |
assert_equal "April 2014", | |
format_date_range(Date.new(2014,4,30),Date.new(2014,4,1)) | |
end | |
end | |
describe "same day" do | |
it "responds with that date" do | |
assert_equal "April 4, 2014", | |
format_date_range(Date.new(2014,4,4),Date.new(2014,4,4)) | |
end | |
end | |
describe "alternate arguments" do | |
it "takes nil as its second argument" do | |
assert_equal format_date_range(Date.new(2014,4,4), nil), "April 4, 2014" | |
end | |
it "takes one argument" do | |
assert_equal format_date_range(Date.new(2014,4,4)), "April 4, 2014" | |
end | |
it "takes nil as its first argument" do | |
assert_raises(ArgumentError) { format_date_range(nil, Date.new(2014,4,4)) } | |
end | |
it "takes nil as both arguments" do | |
assert_equal format_date_range(nil, nil), "" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment