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

[utils] Add Java to JS conversion for Instant & ZonedDateTime #267

Merged
merged 6 commits into from
May 25, 2023

Conversation

florian-h05
Copy link
Contributor

@florian-h05 florian-h05 commented May 21, 2023

Adds two new methods to the utils namespce and makes use of them internally:

  • javaInstantToJsInstant: Takes a java.time.Instant and returns a JS-Joda Instant.
  • javaZDTToJsZDT: Takes a java.time.ZonedDateTime and returns a JS-Joda ZonedDateTime. This was already implemented under the time namespace, but was private.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@florian-h05
Copy link
Contributor Author

FYI, I have benchmarked the performance of these implementations against toString and parsing that string:

var Instant = Java.type('java.time.Instant');
var ins = Instant.now();

console.log('Benchmarking Instant.parse conversion ...')
var start = time.Instant.now().toEpochMilli();

for (var i = 0; i < 50; i++) {
  time.Instant.parse(ins.toString())
}

var end = time.Instant.now().toEpochMilli();
console.log('Benchmark finshed, took ' + (end - start) + ' ms')

console.log('Benchmarking advanced Instant conversion ...')
var start = time.Instant.now().toEpochMilli();

for (var i = 0; i < 50; i++) {
  utils.javaInstantToJsInstant(ins)
}

var end = time.Instant.now().toEpochMilli();
console.log('Benchmark finshed, took ' + (end - start) + ' ms')

var ZonedDateTime = Java.type('java.time.ZonedDateTime');
var zdt = ZonedDateTime.now();

console.log('Benchmarking ZonedDateTime.parse conversion ...')
var start = time.Instant.now().toEpochMilli();

for (var i = 0; i < 50; i++) {
  time.ZonedDateTime.parse(zdt.toString())
}

var end = time.Instant.now().toEpochMilli();
console.log('Benchmark finshed, took ' + (end - start) + ' ms')

console.log('Benchmarking advanced ZonedDateTime conversion ...')
var start = time.Instant.now().toEpochMilli();

for (var i = 0; i < 50; i++) {
  utils.javaZDTToJsZDT(zdt)
}

var end = time.Instant.now().toEpochMilli();
console.log('Benchmark finshed, took ' + (end - start) + ' ms')

gives:

15:01:37.711 [INFO ] [penhab.automation.script.file.test.js] - Benchmarking Instant.parse conversion ...
15:01:37.773 [INFO ] [penhab.automation.script.file.test.js] - Benchmark finshed, took 61 ms
15:01:37.774 [INFO ] [penhab.automation.script.file.test.js] - Benchmarking advanced Instant conversion ...
15:01:37.777 [INFO ] [penhab.automation.script.file.test.js] - Benchmark finshed, took 2 ms
15:01:37.777 [INFO ] [penhab.automation.script.file.test.js] - Benchmarking ZonedDateTime.parse conversion ...
15:01:37.947 [INFO ] [penhab.automation.script.file.test.js] - Benchmark finshed, took 170 ms
15:01:37.947 [INFO ] [penhab.automation.script.file.test.js] - Benchmarking advanced ZonedDateTime conversion ...
15:01:37.969 [INFO ] [penhab.automation.script.file.test.js] - Benchmark finshed, took 21 ms

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@florian-h05 florian-h05 marked this pull request as ready for review May 21, 2023 13:05
@florian-h05 florian-h05 requested a review from a team as a code owner May 21, 2023 13:05
@florian-h05 florian-h05 requested a review from digitaldan May 25, 2023 08:56
Copy link
Contributor

@digitaldan digitaldan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice ! LGTM!

@florian-h05 florian-h05 merged commit 4134df4 into openhab:main May 25, 2023
@florian-h05 florian-h05 deleted the utils-java-time branch May 25, 2023 14:38
@florian-h05 florian-h05 added the enhancement New feature or request label May 25, 2023
@florian-h05 florian-h05 added this to the to be released milestone May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants