I am receiving a timestamp value as output from a exec-binding command. As I have to parse it out of a longer string, the type of the 13 digit value is String (e.g. 1666275807). Now I would like to transform this to a normal date in zonedDateTime format. How can I do this in Rules DSL?
Unfortunately this is not working anymore in OH3 (it seems that the old rule code from 2015 comes from OH1 with Joda Time). How can this be done with the now used ZonedDateTime?
val results = // how ever you get the epoch as a String
val resultsLong = new BigDecimal(results).longValue
val instant = java.time.Instant.ofEpochSecond(resultsLong)
val tz = now.zone // default time zone
ZonedDateTime.ofInstant(instant, tz)
Be careful with that second line of code. If your exec returns a newline BigDecimal won’t be able to parse it. You’ll have to strip extraneous characters off of it. Often this is as easy as calling results.trim.
Thank you very much Rich, like always your advice is simply the best
However, the code did not work exactly as proposed by you but you gave me the relevant hints how to proceed and now it works finde in Rules DSL. Here is my final working coding (in case that others search for the same solution in the fututre):
var timestamp = data.substring(start,ende) //timestamp as String extracted from raw data
var long resultsLong = Integer.parseInt(timestamp) //timestamp as long integer
val instant = java.time.Instant.ofEpochSecond(resultsLong)
val tz = now.zone // default time zone
val final = ZonedDateTime.ofInstant(instant, tz)
For Debugging purposes I wrote the result of each line in OH log, which now correctly shows: