[IGN-8635]Convert UTC timestamp to readable date

having a bad brain day (having a cold sucks). can someone help me convert a UTC string to a readable date. i am wanting to drop this on my UDT tag so the conversion is done right at the UDT. here's the what i attempted so far:

toDate(tag('[MQTT]Edge Nodes/'+{GroupID}+'/'+{AssetName}+'/tags/gpsLastUpdated'))

incoming string: 032615572024
desired format: Tuesday, July 19, 3003 7:07:04 PM GMT-06:00 DST

You've gotta give us an example of what format the string is in and what format you want...

i was actually adding that when your message came in. i am brain dead today, but not like TOTALLY... :laughing: just slow as a dead badger...

1 Like

What date is this supposed to be? I'd expect ISO 8601 since you said "UTC String", but it's not.

Best guess: March 26, 2024 15:57?

1 Like

hm. that's a problem... i admit, i didn't examine that as the docs said the device reported a UTC string... dammit. regardless, is the expression good? i can find out what the hell this incoming number is; i have control over ensuring that comes in as an actual UTC string (don't trust docs). given that incoming is correct, will the expression yield what i want?

EDIT: i should just go home, drink Buckley's, and die until tomorrow....

I've been working through cold since Sunday too :laughing:

1 Like

It doesn't seem to match any of the supported formats so I'm guessing it will yield an error unless you massage the string into a more standard format.

EDIT: Actually since it's a valid integer it'll try to treat it like a UTC timestamp and parse it that way so you'd get a bogus DateTime

I think the leading zero rules that out.

It looks to me that it's a format like this: "MMddHHmmyyyy"

3 Likes
jshell> var df = new SimpleDateFormat("MMddHHmmyyyy")
df ==> java.text.SimpleDateFormat@48f26b00

jshell> df.setTimeZone(TimeZone.getTimeZone("UTC"))

jshell> df.parse("032615572024")
$12 ==> Tue Mar 26 08:57:00 PDT 2024

08:57 PDT is 15:57 UTC

2 Likes

image

Looks like the expression does ignore the leading zero and parse the string as a (edit: bogus) int timestamp.

Sure, but I assume that's just the completely wrong date/time.

Yeah, I was just going into detail on why the expression wouldn't work on that format as written. Just edited my earlier post to hopefully remove any ambiguity

1 Like

so, yeah. the timestamp IS an actual UTC string... 979 years in the future. that's a little too much forward thinking... :smiley:

the expression needs to be on the UDT. i know it works in a binding, but i want the UDT to return a readable date to the individual assets that source the UDT, thus the use of tag().

Oh, so it's a Unix timestamp in seconds, what a weird coincidence:
image

toDate() takes a Unix timestamp in milliseconds so you just need to multiply your tag value by 1000 first.

aha. :man_facepalming: so the expression needs to look like this then:

toDate(str(int(tag('[MQTT]Edge Nodes/'+{GroupID}+'/'+{AssetName}+'/tags/gpsLastUpdated')*1000)))

I think Kevin is right. I'd use this expression:

runScript(
	'system.date.parse',
	0,
	concat(
		tag('[MQTT]Edge Nodes/'+{GroupID}+'/'+{AssetName}+'/tags/gpsLastUpdated'),
		'+00'
	),
	'MMddHHmmyyyyX'
)
2 Likes

I attached this thread to the existing ticket for "add a dateParse analogue to expressions", since the runScript stuff is awkward and annoying.

4 Likes

i did something wrong:

EDIT source device is sending wrong data. undocumented flag that generates the correct UTC string, btw. so that's where the jacked up string comes from...

i'm getting the correct UTC string now (1711485613.000000) so i figured i could just use this expression:

toDate(tag('[MQTT]Edge Nodes/'+{GroupID}+'/'+{AssetName}+'/tags/gpsLastUpdated'))

buuut. it doesn't work. :frowning: the tag() takes a string, yields a string. and toDate() takes a string, so this should be just fine... but it isn't. it spits out this error:

Error_ExpressionEval

not sure why the fails. when i try to put it on a binding, i get a different error:

Bad_NotFound

so now i'm confused. :confused: i just want a dang date on the UDT.

'toDate()` seems to not like the float as a string, although it takes float literals with no complaints.

You can explicily parse it as a float first:

Not sure if the fractional portion is meaningful or not so just left it as a float. Might want to look into other conversions if you'd prefer..

1 Like