Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / date / DateDiffFunction.java @ 44371

History | View | Annotate | Download (2.55 KB)

1
package org.gvsig.expressionevaluator.impl.function.date;
2

    
3
import java.time.Duration;
4
import java.time.LocalDateTime;
5
import java.time.Period;
6
import org.apache.commons.lang3.Range;
7
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DATEDIFF;
8
import org.gvsig.expressionevaluator.Interpreter;
9
import org.gvsig.expressionevaluator.spi.AbstractFunction;
10

    
11
public class DateDiffFunction extends AbstractFunction {
12

    
13
    public DateDiffFunction() {
14
        super("Date", FUNCTION_DATEDIFF,Range.is(3),
15
            "Returns the difference between two dates",
16
            FUNCTION_DATEDIFF+"({{interval}}, date1, date2)",
17
            new String[]{
18
                "interval - a string with the value year,month,day,hour,minute,second or millisecond.",
19
                "date1 - a Date",
20
                "date2 - a Date",
21
            },
22
            "Long"
23
        );
24
    }
25

    
26
    @Override
27
    public boolean allowConstantFolding() {
28
        return true;
29
    }
30
    
31
    @Override
32
    public Object call(Interpreter interpreter, Object[] args) {
33
        String interval = this.getStr(args, 0);
34
        LocalDateTime date1 = this.getLocalDateTime(args,1);
35
        LocalDateTime date2 = this.getLocalDateTime(args,2);
36
        
37
        Period p;
38
        Duration d;
39

    
40
        switch(interval.toLowerCase()) {
41
            case "millisecond":
42
            case "ms":
43
                d = Duration.between(date1, date2);
44
                return d.toMillis();
45
            case "second":
46
            case "ss":
47
            case "s":
48
                d = Duration.between(date1, date2);
49
                return d.getSeconds();
50
            case "minute":
51
            case "mi":
52
            case "n":
53
                d = Duration.between(date1, date2);
54
                return d.toMinutes();
55
            case "hour":
56
            case "hh":
57
                d = Duration.between(date1, date2);
58
                return d.toHours();
59
            case "day":
60
            case "dy":
61
            case "y":
62
                d = Duration.between(date1, date2);
63
                return d.toDays();
64
            case "month":
65
            case "mm":
66
            case "m":
67
                p = Period.between(date1.toLocalDate(), date2.toLocalDate());
68
                return (long) p.getMonths();
69
            case "year":
70
            case "yyyy":
71
            case "yy":
72
                p = Period.between(date1.toLocalDate(), date2.toLocalDate());
73
                return (long) p.getYears();
74
            default:
75
                throw new IllegalArgumentException("Unsupported interval '"+interval+"'.");
76
        } 
77
        
78
    }
79
    
80

    
81
}