Merge pull request #1014 from mojombo/string-times

Parse strings into Time objects for date-related parsing
This commit is contained in:
Parker Moore 2013-05-05 04:42:26 -07:00
commit dcf3911176
2 changed files with 51 additions and 15 deletions

View File

@ -30,7 +30,7 @@ module Jekyll
# #
# Returns the formatting String. # Returns the formatting String.
def date_to_string(date) def date_to_string(date)
date.strftime("%d %b %Y") time(date).strftime("%d %b %Y")
end end
# Format a date in long format e.g. "27 January 2011". # Format a date in long format e.g. "27 January 2011".
@ -39,7 +39,7 @@ module Jekyll
# #
# Returns the formatted String. # Returns the formatted String.
def date_to_long_string(date) def date_to_long_string(date)
date.strftime("%d %B %Y") time(date).strftime("%d %B %Y")
end end
# Format a date for use in XML. # Format a date for use in XML.
@ -53,7 +53,7 @@ module Jekyll
# #
# Returns the formatted String. # Returns the formatted String.
def date_to_xmlschema(date) def date_to_xmlschema(date)
date.xmlschema time(date).xmlschema
end end
# Format a date according to RFC-822 # Format a date according to RFC-822
@ -67,7 +67,7 @@ module Jekyll
# #
# Returns the formatted String. # Returns the formatted String.
def date_to_rfc822(date) def date_to_rfc822(date)
date.rfc822 time(date).rfc822
end end
# XML escape a string for use. Replaces any special characters with # XML escape a string for use. Replaces any special characters with
@ -137,5 +137,18 @@ module Jekyll
"#{array[0...-1].join(', ')}, #{connector} #{array[-1]}" "#{array[0...-1].join(', ')}, #{connector} #{array[-1]}"
end end
end end
private
def time(input)
case input
when Time
input
when String
Time.parse(input)
else
Jekyll::Logger.error "Invalid Date:", "'#{input}' is not a valid datetime."
exit(1)
end
end
end end
end end

View File

@ -14,6 +14,7 @@ class TestFilters < Test::Unit::TestCase
setup do setup do
@filter = JekyllFilter.new @filter = JekyllFilter.new
@sample_time = Time.utc(2013, 03, 27, 11, 22, 33) @sample_time = Time.utc(2013, 03, 27, 11, 22, 33)
@time_as_string = "September 11, 2001 12:46:30 -0000"
end end
should "textilize with simple string" do should "textilize with simple string" do
@ -43,6 +44,8 @@ class TestFilters < Test::Unit::TestCase
assert_equal "chunky, bacon, bits, and pieces", @filter.array_to_sentence_string(["chunky", "bacon", "bits", "pieces"]) assert_equal "chunky, bacon, bits, and pieces", @filter.array_to_sentence_string(["chunky", "bacon", "bits", "pieces"])
end end
context "date filters" do
context "with Time object" do
should "format a date with short format" do should "format a date with short format" do
assert_equal "27 Mar 2013", @filter.date_to_string(@sample_time) assert_equal "27 Mar 2013", @filter.date_to_string(@sample_time)
end end
@ -58,6 +61,26 @@ class TestFilters < Test::Unit::TestCase
should "format a time according to RFC-822" do should "format a time according to RFC-822" do
assert_equal "Wed, 27 Mar 2013 11:22:33 -0000", @filter.date_to_rfc822(@sample_time) assert_equal "Wed, 27 Mar 2013 11:22:33 -0000", @filter.date_to_rfc822(@sample_time)
end end
end
context "with String object" do
should "format a date with short format" do
assert_equal "11 Sep 2001", @filter.date_to_string(@time_as_string)
end
should "format a date with long format" do
assert_equal "11 September 2001", @filter.date_to_long_string(@time_as_string)
end
should "format a time with xmlschema" do
assert_equal "2001-09-11T12:46:30Z", @filter.date_to_xmlschema(@time_as_string)
end
should "format a time according to RFC-822" do
assert_equal "Tue, 11 Sep 2001 12:46:30 -0000", @filter.date_to_rfc822(@time_as_string)
end
end
end
should "escape xml with ampersands" do should "escape xml with ampersands" do
assert_equal "AT&amp;T", @filter.xml_escape("AT&T") assert_equal "AT&amp;T", @filter.xml_escape("AT&T")