diff --git a/README.md b/README.md index 12ab24ad6..28af12203 100644 --- a/README.md +++ b/README.md @@ -971,7 +971,7 @@ The following rules are used during the conversion: | `null` or `undefined` | `time.ZonedDateTime.now()` | `time.toZDT();` | | `time.ZonedDateTime` | passed through unmodified | | | `java.time.ZonedDateTime` | converted to the `time.ZonedDateTime` equivalent | | -| JavaScript native `Date` | converted to the equivalent `time.ZonedDateTime` using `SYSTEM` as the timezone | | +| JavaScript native `Date` | converted to the `time.ZonedDateTime` equivalent using `SYSTEM` as the timezone | | | `number`, `bingint`, `java.lang.Number`, `DecimalType` | rounded to the nearest integer and added to `now` as milliseconds | `time.toZDT(1000);` | | [`Quantity`](#quantity) or `QuantityType` | if the unit is time-compatible, added to `now` | `time.toZDT(item.getItem('MyTimeItem').rawState);`, `time.toZDT(Quantity('10 min'));` | | `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); console.log(timestamp.getMillisFromNow()); ``` +#### `time.toInstant()` + +The following rules are used during the conversion: + +| Argument Type | Rule | Examples | +|-----------------------------------------------|------------------------------------------------------------------------------------------|----------------------------------------------| +| `null` or `undefined` | `time.Instant.now()` | `time.toInstant();` | +| `time.Instant` | passed through unmodified | | +| `java.time.Instant` | converted to the `time.Instant` equivalent | | +| `java.time.ZonedDateTime` | converted to the `time.Instant` equivalent | | +| JavaScript native `Date` | converted to the `time.Instant` equivalent | | +| `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'));` | +| `String`, `java.lang.String`, `StringType` | parsed | `time.toInstant('2019-10-12T07:20:50.52Z');` | +| `DateTimeType` | converted to the `time.Instant` equivalent | | + +When a type or string that cannot be handled is encountered, an error is thrown. + ### Quantity The `Quantity` class greatly simplifies Quantity handling by providing unit conversion, comparisons and mathematical operations. diff --git a/src/time.js b/src/time.js index ce33d7e7c..fe6a70cd2 100644 --- a/src/time.js +++ b/src/time.js @@ -16,9 +16,10 @@ const time = require('@js-joda/core'); const log = require('./log')('time'); const osgi = require('./osgi'); -const { _isItem, _isZonedDateTime, _isDuration, _isQuantity } = require('./helpers'); +const { _isItem, _isZonedDateTime, _isInstant, _isDuration, _isQuantity } = require('./helpers'); const javaZDT = Java.type('java.time.ZonedDateTime'); +const javaInstant = Java.type('java.time.Instant'); const javaDuration = Java.type('java.time.Duration'); const javaString = Java.type('java.lang.String'); const javaNumber = Java.type('java.lang.Number'); @@ -41,28 +42,30 @@ time.ZonedDateTime.prototype.parse = function (text, formatter = rfcFormatter) { }; /** - * Adds millis to the passed in ZDT as milliseconds. The millis is rounded first. + * Adds millis to the passed in Temporal as milliseconds. The millis are rounded first. * If millis is negative they will be subtracted. * @private + * @param {time.Temporal} temporal temporal to add milleseconds to * @param {number|bigint} millis number of milliseconds to add */ -function _addMillisToNow (millis) { - return time.ZonedDateTime.now().plus(Math.round(millis), time.ChronoUnit.MILLIS); +function _addMillis (temporal, millis) { + return temporal.plus(Math.round(millis), time.ChronoUnit.MILLIS); } /** - * Adds the passed in QuantityType