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

Merge branch 'null-and-int' of git://github.com/gwitmond/erlydtl into gwitmond-null-and-int

Conflicts:
	src/erlydtl_filters.erl
Andreas Stenius 11 лет назад
Родитель
Сommit
decf00c0e2
1 измененных файлов с 24 добавлено и 2 удалено
  1. 24 2
      src/erlydtl_filters.erl

+ 24 - 2
src/erlydtl_filters.erl

@@ -56,6 +56,7 @@
         cut/2,
         date/1,
         date/2,
+        date_unix_epoch/1,
         default/2,
         default_if_none/2,
         dictsort/2,
@@ -239,8 +240,12 @@ date(Input) ->
     date(Input, "F j, Y").
 
 %% @doc Formats a date according to the given format.
+date(undefined, _) -> "undefined";
 date(Input, FormatStr) when is_binary(Input) ->
     list_to_binary(date(binary_to_list(Input), FormatStr));
+%% @doc format calendar:gregorian_seconds to string (year starts at year 0, not 1970. Use date_unix_epoch for latter).
+date(Input, FormatStr) when is_integer(Input) ->
+    date(calendar:gregorian_seconds_to_datetime(Input), FormatStr);
 date({{_,_,_} = Date,{_,_,_} = Time}, FormatStr) ->
     erlydtl_dateformat:format({Date, Time}, FormatStr);
 date({_,_,_} = Date, FormatStr) ->
@@ -248,6 +253,12 @@ date({_,_,_} = Date, FormatStr) ->
 date(Input, _FormatStr) when is_list(Input) ->
     io:format("Unexpected date parameter : ~p~n", [Input]),
     "".
+%% @doc format a date given in unix-epoch seconds.
+%% Input is seconds since 1-1-1970. 
+%%% offset is calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}), the difference between 
+%%% gregorian calendar and unix epoch.
+date_unix_epoch(Input) ->
+    date(Input + 62167219200).
 
 %% @doc If value evaluates to `false', use given default. Otherwise, use the value.
 default(Input, Default) ->
@@ -333,8 +344,10 @@ fix_ampersands(Input) when is_list(Input) ->
 floatformat(Number) ->
     floatformat(Number, []).
 
-floatformat(Number, Place) ->
-    floatformat_io(cast_to_float(Number), cast_to_integer(Place)).
+floatformat(Number, Place)
+  when is_number(Number); is_binary(Number); is_list(Number) ->
+    floatformat_io(cast_to_float(Number), cast_to_integer(Place));
+floatformat(_, _) -> "".
 
 floatformat_io(Number, []) ->
     floatformat_io(Number, -1);
@@ -497,6 +510,7 @@ phone2numeric(Input) when is_list(Input) ->
 %% @doc Returns a plural suffix if the value is not 1. By default, this suffix is 's'.
 pluralize(Number, Suffix) when is_binary(Suffix) ->
     pluralize_io(Number, binary_to_list(Suffix) );
+
 pluralize(Number, Suffix) when is_list(Suffix) ->
     pluralize_io(Number, Suffix).
 
@@ -810,6 +824,10 @@ title(Input) when is_list(Input) ->
 %% @doc Truncates a string after a certain number of characters.
 truncatechars(_Input, Max) when Max =< 0 ->
     "";
+truncatechars(null, Max) ->
+    truncatechars("null", Max);
+truncatechars(Input, Max) when is_integer(Input) ->
+    truncatechars(integer_to_list(Input), Max);
 truncatechars(Input, Max) when is_binary(Input) ->
     list_to_binary(truncatechars(binary_to_list(Input), Max));
 truncatechars(Input, Max) ->
@@ -818,6 +836,10 @@ truncatechars(Input, Max) ->
 %% @doc Truncates a string after a certain number of words.
 truncatewords(_Input, Max) when Max =< 0 ->
     "";
+truncatewords(null, Max) ->
+    truncatewords("null", Max);
+truncatewords(Input, Max) when is_integer(Input) ->
+    truncatewords(integer_to_list(Input), Max);
 truncatewords(Input, Max) when is_binary(Input) ->
     list_to_binary(truncatewords(binary_to_list(Input), Max));
 truncatewords(Input, Max) ->