Skip to content

Commit f62870e

Browse files
committed
Issue #3235 a formula to extract and format timestamp from pv values
1 parent 2f91d99 commit f62870e

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

core/formula/doc/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ Enum
164164

165165
**indexOf(Enum e)** - Return the index of the enum value.
166166

167+
Time
168+
----
169+
170+
**timestamp(VType value)** - returns the timestamp of the provided value formatted using the millisecond format.
171+
172+
**timestamp(VType value, String format)** - returns the timestamp of the provided value formatted using the user defined format.
167173

168174
Alarm
169175
-----

core/formula/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<groupId>org.phoebus</groupId>
1818
<artifactId>core-vtype</artifactId>
1919
<version>4.7.4-SNAPSHOT</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.phoebus</groupId>
23+
<artifactId>core-util</artifactId>
24+
<version>4.7.4-SNAPSHOT</version>
2025
</dependency>
2126
<dependency>
2227
<groupId>org.junit.jupiter</groupId>
@@ -30,5 +35,11 @@
3035
<version>1.3</version>
3136
<scope>test</scope>
3237
</dependency>
38+
<dependency>
39+
<groupId>org.phoebus</groupId>
40+
<artifactId>core-util</artifactId>
41+
<version>4.7.4-SNAPSHOT</version>
42+
<scope>compile</scope>
43+
</dependency>
3344
</dependencies>
3445
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.csstudio.apputil.formula.time;
2+
3+
import org.csstudio.apputil.formula.array.BaseArrayFunction;
4+
import org.csstudio.apputil.formula.spi.FormulaFunction;
5+
import org.epics.vtype.Alarm;
6+
import org.epics.vtype.Time;
7+
import org.epics.vtype.VString;
8+
import org.epics.vtype.VType;
9+
import org.phoebus.core.vtypes.VTypeHelper;
10+
11+
import java.time.ZoneId;
12+
import java.time.format.DateTimeFormatter;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
import static org.phoebus.util.time.TimestampFormats.MILLI_FORMAT;
18+
19+
/**
20+
* Extract the timestamps from PV values
21+
*/
22+
public class TimestampFunction implements FormulaFunction {
23+
24+
final private static ZoneId zone = ZoneId.systemDefault();
25+
final private static VString emptyString = VString.of("", Alarm.none(), Time.now());
26+
27+
@Override
28+
public String getCategory() {
29+
return "time";
30+
}
31+
32+
@Override
33+
public String getName() {
34+
return "timestamp";
35+
}
36+
37+
@Override
38+
public String getDescription() {
39+
return "Returns the timestamp of the last update";
40+
}
41+
42+
@Override
43+
public List<String> getArguments() {
44+
return List.of("pv_name", "time format (optional)");
45+
}
46+
47+
@Override
48+
public boolean isVarArgs() {
49+
return true;
50+
}
51+
52+
private Map<String, DateTimeFormatter> formatters = new HashMap<>();
53+
54+
@Override
55+
public VType compute(VType... args) {
56+
if (args.length == 1) {
57+
return VString.of(MILLI_FORMAT.format(VTypeHelper.getTimestamp(args[0])), Alarm.none(), Time.now());
58+
} else if (args.length == 2) {
59+
String format = VTypeHelper.toString(args[1]);
60+
if (!formatters.containsKey(format)) {
61+
formatters.put(format, DateTimeFormatter.ofPattern(VTypeHelper.toString(args[1])).withZone(zone));
62+
}
63+
return VString.of(formatters.get(format).format(VTypeHelper.getTimestamp(args[0])), Alarm.none(), Time.now());
64+
}
65+
return emptyString;
66+
67+
}
68+
}

core/formula/src/main/resources/META-INF/services/org.csstudio.apputil.formula.spi.FormulaFunction

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,5 @@ org.csstudio.apputil.formula.alarm.MinorAlarmFunction
7474
org.csstudio.apputil.formula.enums.IndexOfFunction
7575
org.csstudio.apputil.formula.enums.EnumOfFunction
7676

77+
# Time
78+
org.csstudio.apputil.formula.time.TimestampFunction

0 commit comments

Comments
 (0)