Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.time / org.gvsig.expressionevaluator.time.lib / org.gvsig.expressionevaluator.time.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / date / RelativeInstantFunction.java @ 44644

History | View | Annotate | Download (2.31 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 44080 jjdelcerro
import java.util.Objects;
7 43512 jjdelcerro
import org.apache.commons.lang3.Range;
8 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
9 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
10 44080 jjdelcerro
import org.gvsig.timesupport.DataTypes;
11
import org.gvsig.timesupport.RelativeInstant;
12
import org.gvsig.timesupport.TimeSupportLocator;
13
import org.gvsig.tools.ToolsLocator;
14
import org.gvsig.tools.dataTypes.CoercionException;
15
import org.gvsig.tools.dataTypes.DataTypesManager;
16 43512 jjdelcerro
17 44080 jjdelcerro
public class RelativeInstantFunction extends AbstractFunction {
18 43512 jjdelcerro
19 44080 jjdelcerro
    private final DataTypesManager.Coercion toInstant;
20
21
    public RelativeInstantFunction() {
22
        super("Date", "RelativeInstant",Range.is(1),
23
            "Returns a relative instant from the arguments",
24
            "RelativeInstant({{date}}, format)",
25 43983 jjdelcerro
            new String[]{
26 43987 jjdelcerro
                "date - a string with a date",
27
                "format - Optional. Format to use to parse the date"
28 43983 jjdelcerro
            },
29 44080 jjdelcerro
            "RelativeInstant",
30
            false
31 43983 jjdelcerro
        );
32 44080 jjdelcerro
        this.toInstant = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.INSTANT);
33 43512 jjdelcerro
    }
34
35
    @Override
36 44009 jjdelcerro
    public boolean allowConstantFolding() {
37
        return true;
38
    }
39
40
    @Override
41 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) {
42 44080 jjdelcerro
        if( args.length==1 ) {
43
            Object arg0 = getObject(args, 0);
44
            try {
45
                Object instant = this.toInstant.coerce(arg0);
46
                return instant;
47
            } catch (CoercionException ex) {
48
                throw new RuntimeException("Can't convert value '"+Objects.toString(arg0, "")+"' to instant.", ex);
49
            }
50
        }
51
        String format = getStr(args, 1);
52 43987 jjdelcerro
        String date = getStr(args, 0);
53
        SimpleDateFormat df = new SimpleDateFormat();
54
        if( format != null ) {
55
            df.applyPattern(format);
56 43512 jjdelcerro
        }
57 43987 jjdelcerro
        try {
58
            Date x = df.parse(date);
59 44080 jjdelcerro
            RelativeInstant instant = TimeSupportLocator.getManager().createRelativeInstant(x);
60
            return instant;
61 43987 jjdelcerro
        } catch (ParseException ex) {
62
            throw new RuntimeException("Can't parse date value '"+date+"' with format '"+format==null?"":format+"'", ex);
63 43512 jjdelcerro
        }
64 43987 jjdelcerro
65 43512 jjdelcerro
    }
66
67
68
}