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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

## to be released

| Type | Namespace | Description | Reference | Breaking |
|-------------|------------|--------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|
| Type | Namespace | Description | Reference | Breaking |
|-------------|------------|-----------------------------------------------------------|--------------------------------------------------------|----------|
| Enhancement | `utils` | Add Java to JS conversion for `Instant` & `ZonedDateTime` | [#267](https://github.com/openhab/openhab-js/pull/267) | No |

Also see the [Release Milestone](https://github.com/openhab/openhab-js/milestone/16).

Expand Down
5 changes: 4 additions & 1 deletion actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
* const { actions } = require('openhab');
* actions.NotificationAction.sendBroadcastNotification("Hello World!")
*/
/** @typedef {import('@js-joda/core').ZonedDateTime} time.ZonedDateTime */
/**
* @typedef {import('@js-joda/core').ZonedDateTime} time.ZonedDateTime
* @private
*/

const osgi = require('./osgi');
// See https://github.com/openhab/openhab-core/blob/main/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptThingActionsImpl.java
Expand Down
3 changes: 2 additions & 1 deletion items/item-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @private
*/
const time = require('../time');
const utils = require('../utils');
/**
* @typedef {import('../quantity').Quantity} Quantity
* @private
Expand Down Expand Up @@ -54,7 +55,7 @@ class HistoricItem {
* timestamp of persisted Item
* @type {time.ZonedDateTime}
*/
this.timestamp = time.ZonedDateTime.parse(rawHistoricItem.getTimestamp().toString());
this.timestamp = utils.javaZDTToJsZDT(rawHistoricItem.getTimestamp());
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/java.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ const { ModuleBuilder, Configuration, JavaScriptExecution, JavaTransformation, Q
class BigDecimal {}
BigDecimal.valueOf = jest.fn(() => new BigDecimal());

class Instant {
toEpochMilli () {
return 0;
}
}

class ZonedDateTime {
toInstant () {
return new Instant();
}

getZone () {
return new ZoneId();
}
}

class ZoneId {
toString () {
return 'UTC';
}
}

class HashSet {
add () {}
}
Expand Down Expand Up @@ -65,6 +87,8 @@ Java = {
module.exports = {
ArrayList,
BigDecimal,
Instant,
ZonedDateTime,
FrameworkUtil,
HashSet,
Hashtable,
Expand Down
19 changes: 17 additions & 2 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { UUID, HashSet, ArrayList } = require('./java.mock');
const { UUID, HashSet, ArrayList, Instant, ZonedDateTime } = require('./java.mock');
const {
randomUUID,
jsSetToJavaSet,
Expand All @@ -7,8 +7,11 @@ const {
javaListToJsArray,
javaSetToJsArray,
javaSetToJsSet,
isJsInstanceOfJava
isJsInstanceOfJava,
javaInstantToJsInstant,
javaZDTToJsZDT
} = require('../utils');
const time = require('@js-joda/core');

describe('utils.js', () => {
describe('randomUUID', () => {
Expand Down Expand Up @@ -122,4 +125,16 @@ describe('utils.js', () => {
);
});
});

describe('javaInstantToJsInstant', () => {
it('returns JS-Joda Instant', () => {
expect(javaInstantToJsInstant(new Instant())).toBeInstanceOf(time.Instant);
});
});

describe('javaZDTToJsZDT', () => {
it('returns JS-Joda ZonedDateTime', () => {
expect(javaZDTToJsZDT(new ZonedDateTime())).toBeInstanceOf(time.ZonedDateTime);
});
});
});
20 changes: 4 additions & 16 deletions time.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require('@js-joda/timezone');
const time = require('@js-joda/core');
const items = require('./items');
const utils = require('./utils');

const javaZDT = Java.type('java.time.ZonedDateTime');
const javaDuration = Java.type('java.time.Duration');
Expand All @@ -17,19 +18,6 @@ const { DateTimeType, DecimalType, StringType, QuantityType } = require('@runtim
const { Quantity } = require('./quantity');
const ohItem = Java.type('org.openhab.core.items.Item');

/**
* Converts the Java ZonedDateTime to a time.ZonedDateTime.
* @private
* @param {JavaZonedDateTime} zdt date time to convert to a time.ZonedDateTime
* @returns {time.ZonedDateTime}
*/
function _javaZDTtoZDT (zdt) {
const epoch = zdt.toInstant().toEpochMilli();
const instant = time.Instant.ofEpochMilli(epoch);
const zone = time.ZoneId.of(zdt.getZone().toString());
return time.ZonedDateTime.ofInstant(instant, zone);
}

/**
* Adds millis to the passed in ZDT as milliseconds. The millis is rounded first.
* If millis is negative they will be subtracted.
Expand Down Expand Up @@ -176,7 +164,7 @@ function _convertItem (item) {
} else if (item.rawState instanceof StringType) { // String type Items
return _parseString(item.state);
} else if (item.rawState instanceof DateTimeType) { // DateTime Items
return _javaZDTtoZDT(item.rawState.getZonedDateTime());
return utils.javaZDTToJsZDT(item.rawState.getZonedDateTime());
} else if (item.rawState instanceof QuantityType) { // Number:Time type Items
return _addQuantityType(item.rawState);
} else {
Expand Down Expand Up @@ -241,12 +229,12 @@ function toZDT (when) {

// Java ZDT
if (when instanceof javaZDT) {
return _javaZDTtoZDT(when);
return utils.javaZDTToJsZDT(when);
}

// DateTimeType, extract the javaZDT and convert to time.ZDT
if (when instanceof DateTimeType) {
return _javaZDTtoZDT(when.getZonedDateTime());
return utils.javaZDTToJsZDT(when.getZonedDateTime());
}

// Quantity
Expand Down
2 changes: 1 addition & 1 deletion types/actions.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion types/items/item-history.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion types/openhab-js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ declare global {
*/
type JavaMap = object;
/**
* Native Java ZonedDateTime Object (instance of {@link https://docs.oracle.com/en/java/javase/17/docs/api//java.base/java/time/ZonedDateTime.html java.time.ZonedDateTime})
* Native Java Instant Object (instance of {@link https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/Instant.html java.time.Instant})
*/
type JavaInstant = object;
/**
* Native Java ZonedDateTime Object (instance of {@link https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/ZonedDateTime.html java.time.ZonedDateTime})
*/
type JavaZonedDateTime = object;
/**
Expand Down
2 changes: 1 addition & 1 deletion types/time.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ export function dumpObject(obj: any, dumpProps?: boolean): void;
* @throws error if type is not a java class
*/
export function isJsInstanceOfJava(instance: any, type: JavaClass): boolean;
/**
* Convert Java Instant to JS-Joda Instant.
*
* @memberOf utils
* @param {JavaInstant} instant {@link https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/Instant.html java.time.Instant}
* @returns {time.Instant} {@link https://js-joda.github.io/js-joda/class/packages/core/src/Instant.js~Instant.html JS-Joda Instant}
*/
export function javaInstantToJsInstant(instant: JavaInstant): time.Instant;
/**
* Convert Java ZonedDateTime to JS-Joda.
*
* @memberOf utils
* @param {JavaZonedDateTime} zdt {@link https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/ZonedDateTime.html java.time.ZonedDateTime}
* @returns {time.ZonedDateTime} {@link https://js-joda.github.io/js-joda/class/packages/core/src/ZonedDateTime.js~ZonedDateTime.html JS-Joda ZonedDateTime}
*/
export function javaZDTToJsZDT(zdt: JavaZonedDateTime): time.ZonedDateTime;
/**
* openHAB JavaScript library version
*
Expand All @@ -95,5 +111,6 @@ export function isJsInstanceOfJava(instance: any, type: JavaClass): boolean;
* @type {string}
*/
declare const VERSION: string;
import time = require("@js-joda/core");
export { VERSION as OPENHAB_JS_VERSION };
//# sourceMappingURL=utils.d.ts.map
2 changes: 1 addition & 1 deletion types/utils.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading