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 / TimeFunction.java @ 44338

History | View | Annotate | Download (1.81 KB)

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

    
3
import java.text.ParseException;
4
import java.text.SimpleDateFormat;
5
import java.util.Date;
6
import org.apache.commons.lang3.Range;
7
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TIME;
8
import org.gvsig.expressionevaluator.Interpreter;
9
import org.gvsig.expressionevaluator.spi.AbstractFunction;
10

    
11
public class TimeFunction extends AbstractFunction {
12

    
13
    public TimeFunction() {
14
        super("Date", FUNCTION_TIME,Range.between(1,2),
15
            "Returns a time from the arguments",
16
            FUNCTION_TIME+"({{time}}, format)",
17
            new String[]{
18
                "time - a string with a time",
19
                "format - Optional. Format to use to parse the time"
20
            },
21
            "Time"
22
        );
23
    }
24

    
25
    @Override
26
    public boolean allowConstantFolding() {
27
        return true;
28
    }
29
    
30
    @Override
31
    public Object call(Interpreter interpreter, Object[] args) {
32
        Object date_obj = getObject(args, 0);
33
        if( date_obj instanceof Date ) {
34
            Date x = new Date(((Date) date_obj).getTime());
35
            x.setDate(0);
36
            x.setMonth(0);
37
            x.setYear(0);
38
            return x;
39
        }
40
        String date = getStr(args, 0);
41
        String format = null;
42
        if( args.length==2 ) {
43
            format = getStr(args, 1);
44
        }
45
        SimpleDateFormat df = new SimpleDateFormat();
46
        if( format != null ) {
47
            df.applyPattern(format);
48
        }
49
        try {
50
            Date x = df.parse(date);
51
            x.setDate(0);
52
            x.setMonth(0);
53
            x.setYear(0);
54
            return x;
55
        } catch (ParseException ex) {
56
            throw new RuntimeException("Can't parse date value '"+date+"' with format '"+format==null?"":format+"'", ex);
57
        }
58
        
59
    }
60
    
61

    
62
}