You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Closes#412.
This adds a convenience method for constructing a `time.toInstant` from various input types, similar to `time.toZDT`.
The main purpose and motivation is to simplify this:
```javascript
var startTime = time.toZDT(lastUpdateItem).toInstant();
var endTime = time.Instant.now();
```
to the more straight-forward (and faster):
```javascript
var startTime = time.toInstant(lastUpdateItem);
var endTime = time.toInstant();
```
---------
Signed-off-by: Jacob Laursen <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+18-1
Original file line number
Diff line number
Diff line change
@@ -971,7 +971,7 @@ The following rules are used during the conversion:
971
971
| `null` or `undefined` | `time.ZonedDateTime.now()` | `time.toZDT();` |
972
972
| `time.ZonedDateTime` | passed through unmodified | |
973
973
| `java.time.ZonedDateTime` | converted to the `time.ZonedDateTime` equivalent | |
974
-
| JavaScript native `Date` | converted to the equivalent `time.ZonedDateTime` using `SYSTEM` as the timezone | |
974
+
| JavaScript native `Date` | converted to the `time.ZonedDateTime` equivalent using `SYSTEM` as the timezone | |
975
975
| `number`, `bingint`, `java.lang.Number`, `DecimalType` | rounded to the nearest integer and added to `now` as milliseconds | `time.toZDT(1000);` |
976
976
| [`Quantity`](#quantity) or `QuantityType` | if the unit is time-compatible, added to `now` | `time.toZDT(item.getItem('MyTimeItem').rawState);`, `time.toZDT(Quantity('10 min'));` |
977
977
| `items.Item` or `org.openhab.core.types.Item` | if the state is supported (see the `Type` rules in this table, e.g. `DecimalType`), the state is converted | `time.toZDT(items.getItem('MyItem'));` |
@@ -1089,6 +1089,23 @@ var timestamp = time.ZonedDateTime.now().plusMinutes(5);
1089
1089
console.log(timestamp.getMillisFromNow());
1090
1090
```
1091
1091
1092
+
#### `time.toInstant()`
1093
+
1094
+
The following rules are used during the conversion:
| `null` or `undefined` | `time.Instant.now()` | `time.toInstant();` |
1099
+
| `time.Instant` | passed through unmodified | |
1100
+
| `java.time.Instant` | converted to the `time.Instant` equivalent | |
1101
+
| `java.time.ZonedDateTime` | converted to the `time.Instant` equivalent | |
1102
+
| JavaScript native `Date` | converted to the `time.Instant` equivalent | |
1103
+
| `items.Item` or `org.openhab.core.types.Item` | if the state is supported (see the `*Type` rules in this table), the state is converted | `time.toInstant(items.getItem('MyItem'));` |
Copy file name to clipboardExpand all lines: test/time.spec.js
+21-1
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,27 @@ describe('time.js', () => {
22
22
23
23
it('delegates to parseString',()=>{
24
24
constwhen='when';
25
-
expect(()=>time.toZDT(when)).toThrowError('Failed to parse string when: DateTimeParseException: Text cannot be parsed to a Duration: when, at index: 0');
25
+
expect(()=>time.toZDT(when)).toThrow('Failed to parse string when: DateTimeParseException: Text cannot be parsed to a Duration: when, at index: 0');
26
+
});
27
+
28
+
// TODO: Add remaining possible cases for when
29
+
});
30
+
31
+
describe('toInstant',()=>{
32
+
it('passes through if when is a time.Instant',()=>{
33
+
constinstant=time.Instant.now();
34
+
expect(time.toInstant(instant)).toBe(instant);
35
+
});
36
+
37
+
it('converts if when is a time.ZonedDateTime',()=>{
0 commit comments