When passing a java.util.date type object through an authentication challenge payload, the object is changed into a unicode object before being handed off to the authentication challenge complete handler.
To test this I made a button on my main view with the following script:
logger = system.util.getLogger("ButtonScript")
payload = {
"identifier": "DateTest",
"date": system.date.now()
}
logger.info(str(type(payload['date'])))
system.perspective.authenticationChallenge(payload=payload, framing="embedded")
My authentication challenge complete handler has the following code:
logger = system.util.getLogger("AuthChallenge")
sourceType = payload.get("identifier", None)
if sourceType == "DateTest":
logger.info(str(type(payload['date'])))
Looking at my logs, the 'date' member starts as a java.util.date object, but once received in the challenge complete handler, the 'date' member is now a unicode object.
![AuthPayload](https://global.discourse-cdn.com/inductiveautomation/original/3X/2/b/2b95d061d60069655b7384549066e387f285407a.png)
JSON doesn't know anything about dates.
There's a good flowchart of JSON construction here:
https://www.json.org/json-en.html
Yes?
As mentioned in the docs, the payload must be JSON-encodable. JSON doesn't have a 'real' date type - only strings, numbers, booleans, and null. So if you want a lossless datetime you can "rehydrate" into a real Date object on the other side, send your payload as system.date.now().time
(or system.date.toMillis(system.date.now())
and then use system.date.fromMillis
on the other side.
2 Likes
Was not aware of this, I've only ever passed strings/ints through payloads.
For some reason I thought that Ignition did a smart conversion when handling the payload. Don't know why. ![:upside_down_face: :upside_down_face:](https://emoji.discourse-cdn.com/twitter/upside_down_face.png?v=12)
Guess this means I need to make a method to crawl my payload and convert all the dates to long ints
I'd go with Paul's toMillis suggestion. The conversion back to datetime is simple and foolproof. Avoid working with dates as strings other than to convert them at the last possible moment for human consumption.
3 Likes
I always use the base date objects, in this case I was running into the problem of unknowingly passing a query a string instead of a date. I rely on the components to do the stringify.