From 7554c4ce611df896cc7c5393ba70577bc10d25e0 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sat, 4 May 2013 14:01:44 +0200 Subject: [PATCH 1/2] Parse strings into Time objects for date-related parsing. Fixes #997. --- lib/jekyll/filters.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 37d4f1cb..1886194b 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -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 From c9bd1437a3f1f2ab37f9f4ae97a7faf351c7ad58 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sat, 4 May 2013 14:41:16 +0200 Subject: [PATCH 2/2] Testing parsing of String objects which represent times to Date filters --- test/test_filters.rb | 45 +++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/test/test_filters.rb b/test/test_filters.rb index 41bd242c..6af0d705 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -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,20 +44,42 @@ class TestFilters < Test::Unit::TestCase assert_equal "chunky, bacon, bits, and pieces", @filter.array_to_sentence_string(["chunky", "bacon", "bits", "pieces"]) end - should "format a date with short format" do - assert_equal "27 Mar 2013", @filter.date_to_string(@sample_time) - 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 - should "format a date with long format" do - assert_equal "27 March 2013", @filter.date_to_long_string(@sample_time) - end + should "format a date with long format" do + assert_equal "27 March 2013", @filter.date_to_long_string(@sample_time) + end - should "format a time with xmlschema" do - assert_equal "2013-03-27T11:22:33Z", @filter.date_to_xmlschema(@sample_time) - end + should "format a time with xmlschema" do + assert_equal "2013-03-27T11:22:33Z", @filter.date_to_xmlschema(@sample_time) + end - 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) + 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