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

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