Merge pull request #1014 from mojombo/string-times
Parse strings into Time objects for date-related parsing
This commit is contained in:
commit
dcf3911176
|
@ -30,7 +30,7 @@ module Jekyll
|
|||
#
|
||||
# Returns the formatting String.
|
||||
def date_to_string(date)
|
||||
date.strftime("%d %b %Y")
|
||||
time(date).strftime("%d %b %Y")
|
||||
end
|
||||
|
||||
# Format a date in long format e.g. "27 January 2011".
|
||||
|
@ -39,7 +39,7 @@ module Jekyll
|
|||
#
|
||||
# Returns the formatted String.
|
||||
def date_to_long_string(date)
|
||||
date.strftime("%d %B %Y")
|
||||
time(date).strftime("%d %B %Y")
|
||||
end
|
||||
|
||||
# Format a date for use in XML.
|
||||
|
@ -53,7 +53,7 @@ module Jekyll
|
|||
#
|
||||
# Returns the formatted String.
|
||||
def date_to_xmlschema(date)
|
||||
date.xmlschema
|
||||
time(date).xmlschema
|
||||
end
|
||||
|
||||
# Format a date according to RFC-822
|
||||
|
@ -67,7 +67,7 @@ module Jekyll
|
|||
#
|
||||
# Returns the formatted String.
|
||||
def date_to_rfc822(date)
|
||||
date.rfc822
|
||||
time(date).rfc822
|
||||
end
|
||||
|
||||
# XML escape a string for use. Replaces any special characters with
|
||||
|
@ -137,5 +137,18 @@ module Jekyll
|
|||
"#{array[0...-1].join(', ')}, #{connector} #{array[-1]}"
|
||||
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
|
||||
|
|
|
@ -14,6 +14,7 @@ class TestFilters < Test::Unit::TestCase
|
|||
setup do
|
||||
@filter = JekyllFilter.new
|
||||
@sample_time = Time.utc(2013, 03, 27, 11, 22, 33)
|
||||
@time_as_string = "September 11, 2001 12:46:30 -0000"
|
||||
end
|
||||
|
||||
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"])
|
||||
end
|
||||
|
||||
context "date filters" do
|
||||
context "with Time object" do
|
||||
should "format a date with short format" do
|
||||
assert_equal "27 Mar 2013", @filter.date_to_string(@sample_time)
|
||||
end
|
||||
|
@ -58,6 +61,26 @@ class TestFilters < Test::Unit::TestCase
|
|||
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)
|
||||
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
|
||||
assert_equal "AT&T", @filter.xml_escape("AT&T")
|
||||
|
|
Loading…
Reference in New Issue