updateState(CHANNEL_STATUS_NEXTSTART_TIMESTAMP, new DateTimeType(
new DateTime (mower.getAttributes().getPlanner().getNextStartTimestamp()).toString()));
My problem is that the epoch timestamp is in UTC but when i do this i get it in my local timezone.
How can i update the the channel with UTC DateTime.
Output from epoch Online
Convert epoch to human-readable date and vice versa
1594620000000
GMT: Monday 13 July 2020 06:00:00
Your time zone: måndag 13 juli 2020 kl. 08:00:00 GMT+02:00 DST
It is the GMT time i want in the DateTime not my time zone.
val EpochString = "1594620000000"
// epoch carries no timezone info at all
// and so is usually assumed to be GMT aka UTC
// conventional conversion
val MyDateTimeTypeFromEpoch = new DateTimeType(EpochString)
logInfo("test", "raw dttype " + MyDateTimeTypeFromEpoch.toString)
// post to Item
testDate.postUpdate(new DateTimeType(EpochString))
Thread::sleep(500)
logInfo("test", "item " + testDate.state.toString)
updateState(CHANNEL_STATUS_NEXTSTART_TIMESTAMP, new DateTimeType(Long.toString(mower.getAttributes().getPlanner().getNextStartTimestamp())));
and i get 2020-07-15T06:00:00.000+0000 and that is correct but the host saves everything in UTC and do not care about timezone.
So now when i display the channel in habpanel i get 2020-07-15T08:00:00.000+0200.
So i want to update the channel with 2020-07-15T06:00:00.000 and local timezone in my case +0200.
I’m not sure what you mean. But do you mean the number from NextStartTimestamp is NOT in UTC, but in the local time and when you put it in DateTimeType it is interpreted as UTC time. In other words the host does NOT save it in UTC but in local time, it just doesn’t tell which timezone it is in?
What is it that you want to happen instead? You want this timestamp to display in local time? Or you want it to be treated not as UTC, but as though it were some other timezone timestamp?
It’s more complicated than you might expect. In theory, the DateTime Item holds the timezone as well as the time. So when inputting UTC you get the Z zone, +0000.
It should be easy to adjust to the system default zone , which looks like +0200 DST for you. But this does not (yet) exist in the framework.
It’s something you’ll have to convert for yourself, which with summertime etc. is bit more complicated than just adding hours.
But first be clear about what you are trying to achieve.
Been having a play, in rules but principles should apply to binding code.
Three ways to “import” this timestamp -
(my timezone is BST +0100)
val EpochString = "1594620000000"
var EpochMillis = Long::parseLong(EpochString)
// conventional conversion
// Item gets true UTC value
val DTtypeFromEpoch = new DateTimeType(EpochString)
testDate.postUpdate(DTtypeFromEpoch.toString) // defaults to system zone
// use if Epoch is really Epoch e.g. in UTC
// offsets Item to local timezone
val corrZDT = java.time.ZonedDateTime.ofInstant(
java.time.Instant.ofEpochMilli(EpochMillis),
java.time.ZoneId.of(now.getZone.getID, java.time.ZoneId.SHORT_IDS)
)
testDate.postUpdate(corrZDT.toLocalDateTime.toString) // defaults to system zone
// use if Epoch is pretend Epoch e.g.
// assumes local zone without making offset
val ZDT = java.time.ZonedDateTime.ofInstant(
java.time.Instant.ofEpochMilli(EpochMillis),
java.time.ZoneId.of("UTC") // stops any offset
)
testDate.postUpdate(ZDT.toLocalDateTime.toString) // defaults to system zone
new states
2020-07-15 02:35:51.595 [vent.ItemStateChangedEvent] - testDate changed ... to 2020-07-13T06:00:00.000+0000
2020-07-15 02:35:51.614 [vent.ItemStateChangedEvent] - testDate changed ... to 2020-07-13T07:00:00.000+0100
2020-07-15 02:35:51.631 [vent.ItemStateChangedEvent] - testDate changed ... to 2020-07-13T06:00:00.000+0100
It looks like the following code should also do what you want:
long timestamp = mower.getAttributes().getPlanner().getNextStartTimestamp();
ZonedDateTime zonedDateTime = ZonedDateTime.ofLocal(
LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("UTC")),
ZoneId.systemDefault(), // Assuming this will give your systems local time zone
ZoneOffset.UTC);
updateState(CHANNEL_STATUS_NEXTSTART_TIMESTAMP, new DateTimeType(zonedDateTime));