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 @ 44198

History | View | Annotate | Download (1.56 KB)

1 43987 jjdelcerro
package org.gvsig.expressionevaluator.impl.function.date;
2 43512 jjdelcerro
3 43987 jjdelcerro
import java.text.ParseException;
4
import java.text.SimpleDateFormat;
5
import java.util.Date;
6 43512 jjdelcerro
import org.apache.commons.lang3.Range;
7 44198 jjdelcerro
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TIME;
8 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
9 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
10
11 43987 jjdelcerro
public class TimeFunction extends AbstractFunction {
12 43512 jjdelcerro
13 43987 jjdelcerro
    public TimeFunction() {
14 44198 jjdelcerro
        super("Date", FUNCTION_TIME,Range.between(1,2),
15 43987 jjdelcerro
            "Returns a time from the arguments",
16 44198 jjdelcerro
            FUNCTION_TIME+"({{time}}, format)",
17 43983 jjdelcerro
            new String[]{
18 43987 jjdelcerro
                "time - a string with a time",
19
                "format - Optional. Format to use to parse the time"
20 43983 jjdelcerro
            },
21 43987 jjdelcerro
            "Time"
22 43983 jjdelcerro
        );
23 43512 jjdelcerro
    }
24
25
    @Override
26 44009 jjdelcerro
    public boolean allowConstantFolding() {
27
        return true;
28
    }
29
30
    @Override
31 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) {
32 43987 jjdelcerro
        String date = getStr(args, 0);
33
        String format = null;
34
        if( args.length==2 ) {
35
            format = getStr(args, 1);
36 43512 jjdelcerro
        }
37 43987 jjdelcerro
        SimpleDateFormat df = new SimpleDateFormat();
38
        if( format != null ) {
39
            df.applyPattern(format);
40 43512 jjdelcerro
        }
41 43987 jjdelcerro
        try {
42
            Date x = df.parse(date);
43
            x.setDate(0);
44
            x.setMonth(0);
45
            x.setYear(0);
46
            return x;
47
        } catch (ParseException ex) {
48
            throw new RuntimeException("Can't parse date value '"+date+"' with format '"+format==null?"":format+"'", ex);
49 43512 jjdelcerro
        }
50 43987 jjdelcerro
51 43512 jjdelcerro
    }
52
53
54
}