it seems like js-joda ISO_OFFSET_DATE_TIME is not compliant to the state of a DateTime item if used in a JSScripting rule.
js-joda: ‘2022-02-07T10:27:00.000+01:00’ (see Manual | js-joda)
DateTime.state:‘2022-02-07T10:27:00.000+0100’
The DateTime is missing the “:”. I observed this issue when transforming e.g. the sunrise to ‘HH:mm’ format, so I built a helper function to add the “:”:
// the datetime string is missing a : in the time zone which fails the parsing
// before: '2022-02-07T10:27:00.000+0100'
// after: '2022-02-07T10:27:00.000+01:00'
function fixTimeZone(timeString) {
return timeString.substring(0,26) + ':' + timeString.substring(26);
};
var sunRiseStartString = time.ZonedDateTime.parse(fixTimeZone(items.getItem('sun_Rise_Start').state)).format(time.DateTimeFormatter.ofPattern('HH:mm'));
Is there any better way to format the DateTime item? I tried xx.rawState.getZonedDateTime(), but that lead to a Java to JavaScript conversion error…
I’m currently working on a PR that will add a time.toZDT() that will convert just about anything that can be converted to a Joda ZonedDateTime. There is a different PR being worked to deal with importing Locales because a straight parse or format will fail without importing the right local.
In the mean time I would do the following. The openhab-js library has patched Joda’s ZonedDateTime.parse() so if you don’t pass a DateTimeFormat it assumes the format that is output by Java’s ZonedDateTime. So you could use
var sunRiseDT = time.ZonedDateTime.parse(items.getItem('sun_Rise_Start').rawItem.getZonedDateTime().toSting());
var sunRiseStartString = sunRiseDT.format...
Once my PR is done you’d want to do the following:
var sunRiseDT = time.toZDT(items.getItem('sun_Rise_Start')).format...
Thanks. There is a small issue in this code, the “getState()” is missing.
I got it working with this code:
var sunRiseStartString = time.ZonedDateTime.parse(items.getItem('sun_Rise_Start').rawItem.getState().getZonedDateTime().toString()).format(time.DateTimeFormatter.ofPattern('HH:mm'));