Weird Looking: mike:1 coldfusion: 0

mike:1 coldfusion: 0

December 19, 2005 7:58pm (4 years, 8 months and 3 weeks ago)
Cole mentioned that he was trying to parse Unix time stamps from Yahoo’s traffic web services to something human-readable.  In ColdFusion.  In case you don’t know, CF runs on top of Java and has access to its entire API.  “Easy”, thought I, Java’s Date object can deal with milliseconds since epoch, very similar to your Unix time stamps in most regards.  ColdFusion’s DateFormat function can then be used to translate Date objects into a human-readable string.

<cfset time = 1135038970>
<cfset date = createObject("java", "java.util.Date").init(time * 1000)>


Nope.  The constructor takes a long argument and coldfusion’s scalars cast to doubles when sent to java.  So we turn to ColdFusion’s answer, JavaCast.

<cfset date = createObject("java", "java.util.Date").init(JavaCast("long", time * 1000))>

This is where it got weird.  Coldfusion threw an error that the number was too big to be cast to an int.  What?!  Why is it casting anything to an int?!
I tried a slight variation:

<cfset date = createObject("java", "java.util.Date").init(JavaCast("long", time) * 1000)>

It cast time to a long alright, but then multiplying it by 1000 casts it back to a double.  So I wracked my brain for a way to get this number multiplied by 1000 into a long.  Laughing maniacally, I typed it out:

<cfset date = createObject("java", "java.util.Date").init(createObject("java", "java.lang.Long").parseLong(time & "000"))>

You proved a worthy adversary, ColdFusion, but your crane style was no match for my tiger style.
Oh, and there you go, Cole. :D

Comments

Dec 19, 2005 9:03pm
Oh, man that’s great!

Stupid ColdFusion. Who’d program with that crap anyway? Oh… wait…

I wonder why it would try to cast it as an int. That’s weird. I understand that Cast() is used to battle ambiguity, but why change the data type?

At any rate, thanks for code. As soon as I can figure out how to parse this not-so-strange-but-strange-to-coldfuion’s-xmlparser xml code, I’ll throw it in there.
Dec 19, 2005 9:58pm
This solution was pretty close.  Unfortunately ColdFusion neuters Java’s date parsing, so it’s impossible to account for GMT offset in any reasonable way.
Dec 19, 2005 11:35pm
Hrm. I was going to post your solution at that guy’s site, but WordPress obviously sucks and it wouldn’t let me post it because I didn’t type in the auth. image. Funny, there was no auth. image.
Dec 20, 2005 6:57pm
Turns out that I don’t have to convert the timestamp. I was trying to parse the wrong XML file.

Check it here: http://www.colebarksdale.com/rtr/

But, I will hold on to your solution for future reference.
Dec 21, 2005 10:00pm
That seems like an odd date format for XML.  I have nothing against Unix timestamps, but in general I like to see xsd:dateTime dates in XML (which is essentially the unambiguous ISO 8601 representation), since they strike a decent balance between machine- and human-usability.
Dec 21, 2005 11:36pm
Yeah, unix time stamps have no place in XML.  Really, I’d be reluctant to use them in anything that’s exported from a system (files, network protocols, etc).  Otherwise, I like them in code because they’re compact, fast, and easy to compare.

Also, thanks to unix timestamps, those of us who still know C will get to build up our retirement funds with outrageous consulting fees right before 2038.  Assuming we haven’t reached the technological singularity by then, of course.

Leave a comment


Accepts BBCode with a few enhancements.