/modules/mod_base/filters/filter_datediff.erl

https://code.google.com/p/zotonic/ · Erlang · 54 lines · 31 code · 3 blank · 20 comment · 0 complexity · 57848e59ae6d35e7358df88c073606a5 MD5 · raw file

  1. %%----------------------------------------------------------------------------------------------------------------
  2. %% @author Jason Tanner <jt4websites@googlemail.com>
  3. %% @copyright 2010 Jason Tanner
  4. %% @doc 'datediff' filter, produce the difference between two dates selecting which date part is interesting.
  5. %%----------------------------------------------------------------------------------------------------------------
  6. %%----------------------------------------------------------------------------------------------------------------
  7. %% Copyright 2010 Jason Tanner
  8. %%
  9. %% Licensed under the Apache License, Version 2.0 (the "License");
  10. %% you may not use this file except in compliance with the License.
  11. %% You may obtain a copy of the License at
  12. %%
  13. %% http://www.apache.org/licenses/LICENSE-2.0
  14. %%
  15. %% Unless required by applicable law or agreed to in writing, software
  16. %% distributed under the License is distributed on an "AS IS" BASIS,
  17. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. %% See the License for the specific language governing permissions and
  19. %% limitations under the License.
  20. %%----------------------------------------------------------------------------------------------------------------
  21. -module(filter_datediff).
  22. -export([datediff/3]).
  23. datediff(undefined, _X, _Context) ->
  24. undefined;
  25. datediff(_X, undefined, _Context) ->
  26. undefined;
  27. datediff( {_Y,_M,_D} = Date, DatePart, Context) ->
  28. datediff( [Date,{0,0,0}], DatePart, Context );
  29. datediff( {{_Y,_M,_D},{_H,_I,_S}} = DateTime, DatePart, Context) ->
  30. datediff( [ DateTime, calendar:local_time() ], DatePart, Context );
  31. datediff( [ {{_Y,_M,_D},{_H,_I,_S}} = DateTimeA, {_YB,_MB,_DB} = DateB ], DatePart, Context) ->
  32. datediff( [ DateTimeA, {DateB,{0,0,0}} ], DatePart, Context );
  33. datediff( [ {{_YA,_MA,_DA},{_HA,_IA,_SA}} = DateTimeA, {{_YB,_MB,_DB},{_HB,_IB,_SB}} = DateTimeB ], DatePart, _Context ) ->
  34. {{Y,M,D},{H,I,S}} = z_datetime:diff( DateTimeA, DateTimeB ),
  35. case DatePart of
  36. "Y" -> Y;
  37. "M" -> M;
  38. "D" -> D;
  39. "H" -> H;
  40. "I" -> I;
  41. "S" -> S;
  42. _ -> Y % Defaults to YEAR
  43. end;
  44. datediff( [ DateStringA, DateStringB ], DatePart, Context ) when is_list(DateStringA), is_tuple(DateStringB) ->
  45. datediff( [ z_convert:to_datetime(DateStringA), DateStringB ], DatePart, Context);
  46. datediff( [ DateStringA, DateStringB ], DatePart, Context ) when is_tuple(DateStringA), is_list(DateStringB) ->
  47. datediff( [ DateStringA, z_convert:to_datetime(DateStringB) ], DatePart, Context);
  48. datediff( [ DateStringA, DateStringB ], DatePart, Context ) when is_list(DateStringA), is_list(DateStringB) ->
  49. datediff( [ z_convert:to_datetime(DateStringA), z_convert:to_datetime(DateStringB) ], DatePart, Context);
  50. datediff( DateString, DatePart, Context ) when is_list(DateString) ->
  51. datediff(z_convert:to_datetime(DateString), DatePart, Context).