Browse Source

Merge pull request #95 from dgulino/master

New time format string 'o' – the ISO 8601 year number
Evan Miller 11 years ago
parent
commit
65193d1217

+ 25 - 2
src/filter_lib/erlydtl_dateformat.erl

@@ -37,7 +37,8 @@
     C =:= $y orelse
     C =:= $Y orelse
     C =:= $z orelse
-    C =:= $Z
+    C =:= $Z orelse
+    C =:= $o
 ).
 
 %
@@ -288,6 +289,10 @@ tag_to_value($z, {Y,M,D}, _) ->
 tag_to_value($Z, _, _) ->
    "TODO";
 
+%% o – the ISO 8601 year number
+tag_to_value($o, {Y,M,D}, _) ->
+    integer_to_list(weeknum_year(Y,M,D));
+
 tag_to_value(C, Date, Time) ->
     io:format("Unimplemented tag : ~p [Date : ~p] [Time : ~p]",
         [C, Date, Time]),
@@ -320,7 +325,25 @@ year_weeknum(Y,M,D) ->
               _ -> Wk
             end
     end.
-   
+
+weeknum_year(Y,M,D) ->
+    WeekNum = year_weeknum(Y,M,D),
+    case M of
+        1 ->
+            case WeekNum of
+                53  -> Y - 1;
+                52  -> Y - 1;
+                _  -> Y
+        end;
+        12 ->
+            case WeekNum of
+                2  -> Y + 1;
+                1  -> Y + 1;
+                _  -> Y
+        end;
+        _ -> Y
+    end.
+            
 weeks_in_year(Y) ->
     D1 = calendar:day_of_the_week(Y, 1, 1),
     D2 = calendar:day_of_the_week(Y, 12, 31),

+ 37 - 5
tests/src/erlydtl_dateformat_tests.erl

@@ -18,7 +18,7 @@ run_tests() ->
           {"s", "00"}, {"S", "th"}, {"t", "31"},
           {"w", "0"}, {"W", "27"}, {"y", "79"}, {"Y", "1979"}, {"z", "189"},
           {"jS F Y H:i", "8th July 1979 00:00"},
-          {"jS o\\f F", "8th of July"},
+          {"jS \\o\\f F", "8th of July"},
           % We expect these to come back verbatim
           {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
 
@@ -43,7 +43,7 @@ run_tests() ->
           {"s", "12"}, {"S", "th"}, {"t", "31"},
           {"w", "0"}, {"W", "27"}, {"y", "79"}, {"Y", "1979"}, {"z", "189"},
           {"jS F Y H:i", "8th July 1979 22:07"},
-          {"jS o\\f F", "8th of July"},
+          {"jS \\o\\f F", "8th of July"},
           % We expect these to come back verbatim
           {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
           % TODO : timzeone related tests.
@@ -67,7 +67,7 @@ run_tests() ->
           {"s", "09"}, {"S", "th"}, {"t", "31"},
           {"w", "4"}, {"W", "52"}, {"y", "08"}, {"Y", "2008"}, {"z", "360"},
           {"jS F Y H:i", "25th December 2008 07:00"},
-          {"jS o\\f F", "25th of December"},
+          {"jS \\o\\f F", "25th of December"},
           % We expect these to come back verbatim
           {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
           % TODO : timzeone related tests.
@@ -91,7 +91,7 @@ run_tests() ->
           {"s", "59"}, {"S", "th"}, {"t", "29"},
           {"w", "0"}, {"W", "9"}, {"y", "04"}, {"Y", "2004"}, {"z", "58"},
           {"jS F Y H:i", "29th February 2004 12:00"},
-          {"jS o\\f F", "29th of February"},
+          {"jS \\o\\f F", "29th of February"},
           % We expect these to come back verbatim
           {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
           % TODO : timzeone related tests.
@@ -115,7 +115,7 @@ run_tests() ->
           {"s", "09"}, {"S", "th"}, {"t", "29"},
           {"w", "0"}, {"W", "9"}, {"y", "04"}, {"Y", "2004"}, {"z", "58"},
           {"jS F Y H:i", "29th February 2004 12:00"},
-          {"jS o\\f F", "29th of February"},
+          {"jS \\o\\f F", "29th of February"},
           % We expect these to come back verbatim
           {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
           % TODO : timzeone related tests.
@@ -158,6 +158,38 @@ run_tests() ->
       { "weeknum 4.1",  {2008,  2, 28}, [{"W", "9"}] },
       { "weeknum 4.2",  {1975,  7, 24}, [{"W","30"}] },
 
+      % Yearweek tests.  Largely based on examples from :
+      %   http://en.wikipedia.org/wiki/ISO_week_date
+      { "weeknum_year 1.1",  {2005,  1,  1}, [{"o", "2004"}] },
+      { "weeknum_year 1.2",  {2005,  1,  2}, [{"o", "2004"}] },
+      { "weeknum_year 1.3",  {2005, 12, 31}, [{"o", "2005"}] },
+      { "weeknum_year 1.4",  {2007,  1,  1}, [{"o", "2007"}]  },
+      { "weeknum_year 1.5",  {2007, 12, 30}, [{"o", "2007"}] },
+      { "weeknum_year 1.6",  {2007, 12, 31}, [{"o", "2008"}]  },
+      { "weeknum_year 1.6",  {2008,  1,  1}, [{"o", "2008"}]  },
+      { "weeknum_year 1.7",  {2008, 12, 29}, [{"o", "2009"}]  },
+      { "weeknum_year 1.8",  {2008, 12, 31}, [{"o", "2009"}]  },
+      { "weeknum_year 1.9",  {2009,  1,  1}, [{"o", "2009"}]  },
+      { "weeknum_year 1.10", {2009, 12, 31}, [{"o", "2009"}] },
+      { "weeknum_year 1.11", {2010,  1,  3}, [{"o", "2009"}] },
+      % Examples where the ISO year is three days into
+      % the next Gregorian year
+      { "weeknum_year 2.1",  {2009, 12, 31}, [{"o", "2009"}] },
+      { "weeknum_year 2.2",  {2010,  1,  1}, [{"o", "2009"}] },
+      { "weeknum_year 2.3",  {2010,  1,  2}, [{"o", "2009"}] },
+      { "weeknum_year 2.4",  {2010,  1,  3}, [{"o", "2009"}] },
+      { "weeknum_year 2.5",  {2010,  1,  5}, [{"o", "2010"}] },
+      % Example where the ISO year is three days into
+      % the previous Gregorian year
+      { "weeknum_year 3.1",  {2008, 12, 28}, [{"o", "2008"}] },
+      { "weeknum_year 3.2",  {2008, 12, 29}, [{"o", "2009"}] },
+      { "weeknum_year 3.3",  {2008, 12, 30}, [{"o", "2009"}] },
+      { "weeknum_year 3.4",  {2008, 12, 31}, [{"o", "2009"}] },
+      { "weeknum_year 3.5",  {2009,  1,  1}, [{"o", "2009"}] },
+      % freeform tests
+      { "weeknum_year 4.1",  {2008,  2, 28}, [{"o", "2008"}] },
+      { "weeknum_year 4.2",  {1975,  7, 24}, [{"o", "1975"}] },
+
       % Ordinal suffix tests.
       { "Ordinal suffix 1", {1984,1,1},  [{"S", "st"}] },
       { "Ordinal suffix 2", {1984,2,2},  [{"S", "nd"}] },

+ 1 - 1
tests/src/erlydtl_unittests.erl

@@ -85,7 +85,7 @@ tests() ->
 		  ]},
      {"now", [
 	      {"now functional",
-	       <<"It is the {% now \"jS o\\f F Y\" %}.">>, [{var1, ""}], generate_test_date()}
+	       <<"It is the {% now \"jS \\o\\f F Y\" %}.">>, [{var1, ""}], generate_test_date()}
 	     ]},
      {"if", [
 	     {"If/else",