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] Remove time conversions as moved to the time namespace #384

Merged
merged 1 commit into from
Sep 16, 2024
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,14 @@ var Locale = require('@js-joda/locale_de-de').Locale.GERMAN;
var formatter = time.DateTimeFormatter.ofPattern('dd.MM.yyyy HH:mm').withLocale(Locale);
```

#### `time.javaInstantToJsInstant()`

Converts a [`java.time.Instant`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/Instant.html) to a JS-Joda [`Instant`](https://js-joda.github.io/js-joda/manual/Instant.html).

#### `time.javaZDTToJsZDT()`

Converts a [`java.time.ZonedDateTime`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/ZonedDateTime.html) to a JS-Joda [`ZonedDateTime`](https://js-joda.github.io/js-joda/manual/ZonedDateTime.html).

#### `time.toZDT()`

There will be times when this automatic conversion is not available (for example when working with date times within a rule).
Expand Down
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ module.exports = {
get things () { return require('./things'); },
get triggers () { return require('./triggers'); },
get actions () { return require('./actions'); },
get utils () { return require('./utils'); },
utils: new Proxy({}, {
get: (target, name) => {
if (name.startsWith('javaZDTToJsZDT')) {
console.warn(`utils.${name} is deprecated, use time.javaZDTToJsZDT instead.`);
return require('./time').javaZDTToJsZDT;
} else if (name === 'javaInstantToJsInstant') {
console.warn('utils.javaInstantToJsInstant is deprecated, use time.javaInstantToJsInstant instead.');
return require('./time').javaInstantToJsInstant;
}
return require('./utils')[name];
}
}),
get osgi () { return require('./osgi'); },
get cache () { return require('./cache'); },
get time () { return require('./time'); },
Expand Down
3 changes: 1 addition & 2 deletions src/items/item-persistence.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const log = require('../log')('items.ItemPersistence');
const time = require('../time');
const utils = require('../utils');
const { getQuantity, QuantityError } = require('../quantity');
const { _toOpenhabPrimitiveType, _isTimeSeries } = require('../helpers');

Expand Down Expand Up @@ -99,7 +98,7 @@ class PersistedItem extends PersistedState {
* @type {time.ZonedDateTime}
*/
get timestamp () {
return utils.javaZDTToJsZDT(this.#rawHistoricItem.getTimestamp());
return time.javaZDTToJsZDT(this.#rawHistoricItem.getTimestamp());
}

toString () {
Expand Down
45 changes: 0 additions & 45 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const time = require('@js-joda/core');

/**
* openHAB JavaScript library version
*
Expand Down Expand Up @@ -194,46 +192,6 @@ function dumpObject (obj, dumpProps = false) {
}
}

/**
* 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}
*/
function javaInstantToJsInstant (instant) {
return time.Instant.ofEpochMilli(instant.toEpochMilli());
}

/**
* Convert Java ZonedDateTime to JS-Joda ZonedDateTime.
*
* @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}
*/
function javaZDTToJsZDT (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);
}

/**
* Convert Java ZonedDateTime to JS-Joda ZonedDateTime and default to SYSTEM timezone if not explicitly set.
*
* @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}
*/
function javaZDTToJsZDTWithDefaultZoneSystem (zdt) {
const jsZDT = javaZDTToJsZDT(zdt);
if (/^[+-]/.test(jsZDT.zone().toString())) {
return jsZDT.withZoneSameLocal(time.ZoneId.SYSTEM);
}
return jsZDT;
}

module.exports = {
jsSetToJavaSet,
jsArrayToJavaSet,
Expand All @@ -245,8 +203,5 @@ module.exports = {
javaMapToJsObj,
randomUUID,
dumpObject,
javaInstantToJsInstant,
javaZDTToJsZDT,
javaZDTToJsZDTWithDefaultZoneSystem,
OPENHAB_JS_VERSION: VERSION
};
19 changes: 2 additions & 17 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
const { UUID, HashSet, ArrayList, Instant, ZonedDateTime } = require('./java.mock');
const { UUID, HashSet, ArrayList } = require('./java.mock');
const {
randomUUID,
jsSetToJavaSet,
jsArrayToJavaSet,
jsArrayToJavaList,
javaListToJsArray,
javaSetToJsArray,
javaSetToJsSet,
javaInstantToJsInstant,
javaZDTToJsZDT
javaSetToJsSet
} = require('../src/utils');
const time = require('@js-joda/core');

describe('utils.js', () => {
describe('randomUUID', () => {
Expand Down Expand Up @@ -87,16 +84,4 @@ describe('utils.js', () => {
expect(javaSetToJsSet(new HashSet())).toBeInstanceOf(Set);
});
});

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);
});
});
});
2 changes: 1 addition & 1 deletion types/items/item-persistence.d.ts.map

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

25 changes: 0 additions & 25 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,6 @@ export function randomUUID(): string;
* @param {boolean} [dumpProps=false] whether properties also should be dumped
*/
export function dumpObject(obj: any, dumpProps?: boolean): void;
/**
* 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 ZonedDateTime.
*
* @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;
/**
* Convert Java ZonedDateTime to JS-Joda ZonedDateTime and default to SYSTEM timezone if not explicitly set.
*
* @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 javaZDTToJsZDTWithDefaultZoneSystem(zdt: JavaZonedDateTime): time.ZonedDateTime;
/**
* openHAB JavaScript library version
*
Expand All @@ -109,6 +85,5 @@ export function javaZDTToJsZDTWithDefaultZoneSystem(zdt: JavaZonedDateTime): tim
* @type {string}
*/
declare const VERSION: string;
import time = require("@js-joda/core");
export { VERSION as OPENHAB_JS_VERSION };
//# sourceMappingURL=utils.d.ts.map
Loading
Loading