Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide time.toInstant method #413

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

jlaur
Copy link
Contributor

@jlaur jlaur commented Jan 5, 2025

This provides 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:

var startTime = time.toZDT(lastUpdateItem).toInstant();
var endTime = time.Instant.now();

to the more straight-forward (and faster):

var startTime = time.toInstant(lastUpdateItem);
var endTime = time.toInstant();

Please note I have deliberately left out a few conversions provided by time.toZDT which I consider slightly misleading and/or bloated, for example considering a number to be a number of milliseconds to be added to now. This can be achieved by directly using the js-joda classes, e.g. time.Instant.now().plusMillis(500) which to me seems more readable than time.toInstant(500).

I was in doubt whether to even add time.toInstant() (without parameter) in favor of using time.Instant.now(), but it seems so commonly used for time.toZDT that I decided to leave it in for first review iteration to be discussed. I also provided those operations working directly on items, although I also have doubts about DecimalType and QuantityType.

Testing

var zoned = time.toZDT(items.TestDateTime);
var instant = time.toInstant(items.TestDateTime);
console.log("item: " + instant.toString());
console.log("instant: " + time.toInstant(instant).toString());
console.log("now: " + time.toInstant().toString());
console.log("zoned: " + time.toInstant(zoned).toString());
console.log("string: " + time.toInstant('2024-10-31T20:00:02Z').toString());
console.log("item quantity: " + time.toInstant(items.TestTime));
console.log("item decimal: " + time.toInstant(items.TestNumber));
console.log("item string: " + time.toInstant(items.TestInstantString));

console.log("zoned item quantity: " + time.toZDT(items.TestTime));
console.log("zoned item decimal: " + time.toZDT(items.TestNumber));

Output:

2025-01-05 23:12:23.749 [INFO ] [enhab.automation.script.file.test.js] - item: 2024-10-31T20:00:01Z
2025-01-05 23:12:23.752 [INFO ] [enhab.automation.script.file.test.js] - instant: 2024-10-31T20:00:01Z
2025-01-05 23:12:23.755 [INFO ] [enhab.automation.script.file.test.js] - now: 2025-01-05T22:12:23.754Z
2025-01-05 23:12:23.757 [INFO ] [enhab.automation.script.file.test.js] - zoned: 2024-10-31T20:00:01Z
2025-01-05 23:12:23.764 [INFO ] [enhab.automation.script.file.test.js] - string: 2024-10-31T20:00:02Z
2025-01-05 23:12:23.769 [INFO ] [enhab.automation.script.file.test.js] - item quantity: 2025-01-06T00:13:23.765Z
2025-01-05 23:12:23.771 [INFO ] [enhab.automation.script.file.test.js] - item decimal: 2025-01-05T22:12:33.770Z
2025-01-05 23:12:23.775 [INFO ] [enhab.automation.script.file.test.js] - item string: 2024-10-31T12:00:01Z
2025-01-05 23:12:23.778 [INFO ] [enhab.automation.script.file.test.js] - zoned item quantity: 2025-01-06T01:13:23.776+01:00[Europe/Copenhagen]
2025-01-05 23:12:23.781 [INFO ] [enhab.automation.script.file.test.js] - zoned item decimal: 2025-01-05T23:12:33.779+01:00[Europe/Copenhagen]

Resolves #412

@jlaur jlaur force-pushed the 412-time-instant branch 2 times, most recently from c7a8ee1 to d93cb68 Compare January 6, 2025 22:09
Resolves openhab#412

Signed-off-by: Jacob Laursen <[email protected]>
@jlaur jlaur force-pushed the 412-time-instant branch from d93cb68 to d02bb08 Compare January 6, 2025 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend support for time.Instant
1 participant