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 @ 47702
History | View | Annotate | Download (1.65 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.is(1), |
15 |
"Returns a time from the arguments",
|
16 |
FUNCTION_TIME+"({{time}})",
|
17 |
new String[]{ |
18 |
"time - a string with a time"
|
19 |
}, |
20 |
"Time",
|
21 |
true
|
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 |
java.sql.Time x = new java.sql.Time(((Date) date_obj).getTime()); |
35 |
return x;
|
36 |
} |
37 |
String date = getStr(args, 0); |
38 |
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); |
39 |
try {
|
40 |
Date x0 = df.parse(date);
|
41 |
java.sql.Time x = new java.sql.Time(x0.getTime());
|
42 |
return x;
|
43 |
} catch (Exception ex) { |
44 |
throw new RuntimeException("Can't parse time value '"+date+"' expected format 'HH:mm:ss'.", ex); |
45 |
} |
46 |
|
47 |
} |
48 |
|
49 |
@Override
|
50 |
public String template() { |
51 |
java.sql.Time now = new java.sql.Time(System.currentTimeMillis()); |
52 |
return "TIME '{{"+now.toString()+"}}'"; |
53 |
} |
54 |
|
55 |
} |