PageRenderTime 34ms CodeModel.GetById 8ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

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