Просмотр исходного кода

Fix time zone inconsistency. Removing tests of "r" dateformat tag because these are time zone related and we don't know how to test time zones properly. Thanks to Liu Yubao.

Evan Miller 16 лет назад
Родитель
Сommit
38836b3c5a

+ 10 - 11
src/erlydtl/erlydtl_dateformat.erl

@@ -209,12 +209,11 @@ tag_to_value($N, {_,M,_}, _) ->
 % Difference to Greenwich time in hours; e.g. '+0200'
 tag_to_value($O, Date, Time) ->
    Diff = utc_diff(Date, Time),
-   Offset = case utc_diff(Date, Time) of
-      Diff when abs(Diff) > 2400 -> "+0000";
-      Diff when Diff < 0 ->
-          io_lib:format("-~4..0w", [trunc(abs(Diff))]);
-      _ ->
-          io_lib:format("+~4..0w", [trunc(abs(Diff))])
+   Offset = if
+      Diff < 0 ->
+          io_lib:format("-~4..0w", [abs(Diff)]);
+      true ->
+          io_lib:format("+~4..0w", [Diff])
    end,
    lists:flatten(Offset);
 
@@ -314,11 +313,11 @@ weeks_in_year(Y) ->
     if (D1 =:= 4 orelse D2 =:= 4) -> 53; true -> 52 end.
 
 utc_diff(Date, Time) ->
-   % Note: this code is plagarised from yaws_log
-   UTime = erlang:universaltime(),
-   DiffSecs = calendar:datetime_to_gregorian_seconds({Date,Time})
-       - calendar:datetime_to_gregorian_seconds(UTime),
-   ((DiffSecs/3600)*100).
+   LTime = {Date, Time},
+   UTime = erlang:localtime_to_universaltime(LTime),
+   DiffSecs = calendar:datetime_to_gregorian_seconds(LTime) - 
+       calendar:datetime_to_gregorian_seconds(UTime),
+   trunc((DiffSecs / 3600) * 100).
 
 dayname(1) -> "monday";
 dayname(2) -> "tuesday";

+ 4 - 4
src/tests/erlydtl_dateformat_tests.erl

@@ -18,13 +18,13 @@ run_tests() ->
           {"n", "7"}, {"N", "July"}, {"P", "midnight"},
           {"s", "00"}, {"S", "th"}, {"t", "31"},
           {"w", "0"}, {"W", "27"}, {"y", "79"}, {"z", "189"},
-          {"r", "Sun, 8 Jul 1979 00:00:00 +0000"},
           {"jS F Y H:i", "8th July 1979 00:00"},
           {"jS o\\f F", "8th of July"},
           % We expect these to come back verbatim
           {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
 
           % TODO : timzeone related tests.
+          %{"r", "Sun, 8 Jul 1979 00:00:00 +0000"},
           %{"O", "0000"},
           %{"T", "CET"},
           %{"U", "300531600"},
@@ -42,12 +42,12 @@ run_tests() ->
           {"n", "7"}, {"N", "July"}, {"P", "10:07 p.m."},
           {"s", "12"}, {"S", "th"}, {"t", "31"},
           {"w", "0"}, {"W", "27"}, {"y", "79"}, {"z", "189"},
-          {"r", "Sun, 8 Jul 1979 22:07:12 +0000"},
           {"jS F Y H:i", "8th July 1979 22:07"},
           {"jS o\\f F", "8th of July"},
           % We expect these to come back verbatim
           {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
           % TODO : timzeone related tests.
+          %{"r", "Sun, 8 Jul 1979 22:07:12 +0000"},
           %{"O", "0000"},
           %{"T", "CET"},
           %{"U", "300531600"},
@@ -65,12 +65,12 @@ run_tests() ->
           {"n", "12"}, {"N", "Dec."}, {"P", "7 a.m."},
           {"s", "09"}, {"S", "th"}, {"t", "31"},
           {"w", "4"}, {"W", "52"}, {"y", "08"}, {"z", "360"},
-          {"r", "Thu, 25 Dec 2008 07:00:09 +0000"},
           {"jS F Y H:i", "25th December 2008 07:00"},
           {"jS o\\f F", "25th of December"},
           % We expect these to come back verbatim
           {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
           % TODO : timzeone related tests.
+          %{"r", "Thu, 25 Dec 2008 07:00:09 +0000"},
           %{"O", "0000"},
           %{"T", "CET"},
           %{"U", "300531600"},
@@ -88,12 +88,12 @@ run_tests() ->
           {"n", "2"}, {"N", "Feb."}, {"P", "noon"},
           {"s", "59"}, {"S", "th"}, {"t", "29"},
           {"w", "0"}, {"W", "9"}, {"y", "04"}, {"z", "58"},
-          {"r", "Sun, 29 Feb 2004 12:00:59 +0000"},
           {"jS F Y H:i", "29th February 2004 12:00"},
           {"jS o\\f F", "29th of February"},
           % We expect these to come back verbatim
           {"x", "x"}, {"C", "C"}, {";", ";"}, {"%", "%"}
           % TODO : timzeone related tests.
+          %{"r", "Sun, 29 Feb 2004 12:00:59 +0000"},
           %{"O", "0000"},
           %{"T", "CET"},
           %{"U", "300531600"},

+ 4 - 4
src/tests/erlydtl_unittests.erl

@@ -225,13 +225,13 @@ tests() ->
                     <<"{{ var1|center:1 }}">>, [{var1, "KBR"}], 
                     <<"B">>},
                 {"|date 1",
-                   <<"{{ var1|date:\"r\" }}">>,
+                   <<"{{ var1|date:\"jS F Y H:i\" }}">>,
                    [{var1, {1975,7,24}}],
-                   <<"Thu, 24 Jul 1975 00:00:00 +0000">>},
+                   <<"24th July 1975 00:00">>},
                 {"|date 2",
-                   <<"{{ var1|date:\"r\" }}">>,
+                   <<"{{ var1|date:\"jS F Y H:i\" }}">>,
                    [{var1, {{1975,7,24}, {7,13,1}}}],
-                   <<"Thu, 24 Jul 1975 07:13:01 +0000">>},
+                   <<"24th July 1975 07:13">>},
                 {"|escapejs",
                     <<"{{ var1|escapejs }}">>, [{var1, "Skip's \"Old-Timey\" Diner"}], 
                     <<"Skip\\'s \\\"Old-Timey\\\" Diner">>},