Browse Source

Added new time format string: o
o – the ISO 8601 year number
https://docs.djangoproject.com/en/dev/releases/1.4/#two-new-date-format-strings

Drew Gulino 11 years ago
parent
commit
a62101b0cc

BIN
src/filter_lib/.erlydtl_dateformat.erl.swo


+ 66 - 4
src/filter_lib/erlydtl_dateformat.erl

@@ -1,5 +1,5 @@
 -module(erlydtl_dateformat).
--export([format/1, format/2]).
+-export([format/1, format/2, weeknum_year/3, year_weeknum/3]).
 
 -define(TAG_SUPPORTED(C),
     C =:= $a orelse
@@ -37,7 +37,9 @@
     C =:= $y orelse
     C =:= $Y orelse
     C =:= $z orelse
-    C =:= $Z
+    C =:= $Z orelse
+    C =:= $o orelse
+    C =:= $e
 ).
 
 %
@@ -288,6 +290,14 @@ tag_to_value($z, {Y,M,D}, _) ->
 tag_to_value($Z, _, _) ->
    "TODO";
 
+%% e – the name of the timezone of the given datetime object
+tag_to_value($e, _, _) ->
+    "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]),
@@ -295,7 +305,7 @@ tag_to_value(C, Date, Time) ->
 
 % Date helper functions
 day_of_year(Y,M,D) ->
-   day_of_year(Y,M,D,0).
+       day_of_year(Y,M,D,0).
 day_of_year(_Y,M,D,Count) when M =< 1 ->
    D + Count;
 day_of_year(Y,M,D,Count) when M =< 12 ->
@@ -320,7 +330,59 @@ 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.
+            
+
+%%weeknum_year(Year,Month,Date) ->
+%%   FirstMondayy = first_monday(Year),
+%%   LastyeSunday = last_sunday(Year),
+%%   % if Today < FirstMonday but still in Year, then WeekNumYear = Year - 1;
+%%    % if Today > LastSunday but still in Year, then WeekNumYear = Year + 1;
+%%    % else WeekNumYear = Year;
+%%    % WeekNumYear.
+%%
+%%    end.
+%%
+%%first_monday(Year) ->
+%%    First = (calendar:day_of_the_week(Y, 1, 1),
+%%    case First >= 1 of
+%%        true -> 
+%%            case First == 1 of
+%%                true -> 1;
+%%                false -> 
+%%        false ->
+%%            case First > 1 
+%%        
+
+        
+
+%Dates in January    Effect
+%M   T   W   T   F   S   S   Week number     Week assigned to
+%1   2   3   4   5   6   7   1   New year
+%    1   2   3   4   5   6   1   New year
+%        1   2   3   4   5   1   New year
+%            1   2   3   4   1   New year
+%                1   2   3   53  Previous year
+%                    1   2   53 or 52    Previous year
+%                        1   52  Previous year
+
 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",