Converting to and from Unix / Java time (epoch)

Written by admin

Topics: Code, Rails, Ruby, Software Development

Working in telecom, I have to integrate my Rails and plain Ruby apps with Java or Unix based systems running on remote hosts. Most often these systems are running remotely and are accessed via an XML-over-HTTP interface (no SOAP, no REST, blah…).

I’ve found that one of the most infuriating things that UNIX and Java “roll your own web service” writers do is specify “time” fields as “seconds since the epoch”. WORSE: Many Java service writers specify time fields as “milliseconds since the epoch”.

Come on, now, people? It’s a web service! Do you really think I care about accuracy in milliseconds?

I won’t go into a rant about this. Suffice it to say: I Find This BS To Be Lazy. It works that way so that the server software doesn’t have to do any complicated date parsing or conversions.

Fortunately, Ruby makes it easy to deal with if you know the right methods to use. Here’s how to convert to a UNIX or Java internal-time:

1
2
3
4
5
rubytime = Time.new  # => Tue May 06 11:11:05 -0400 2008
 
# .. Convert from Ruby time to Unix time. 
unixtime = rubytime.to_i            # 1210086665
javatime = rubytime.to_i * 1000  # 1210086665000

What about converting the other way? The example above are converting the “less precise” Ruby time (no milliseconds) into equally or more precise time formats. What do you do when you have a “milliseceonds since epoch” time and you want to use it in Ruby, but preserve the sub-second precision?

1
2
3
4
5
6
7
8
9
# Add just over a half second to the time used above
millis   = 1210086665555  
 
# adding .0 preserves subsecond accuracy
precise = Time.at(millis / 1000.0) 
 
precise.to_i            #  1210086665
precise.to_f            #  1210086665.555
precise.to_f * 1000  # 1210086665555.0

Now. Go convince all those legacy Java and Unix service writers to stop specifying “Epoch Time” in their interfaces! It’s Just Plain Lazy!

  • Pingback: Pete On Rails » Blog Archive » Integrating with Services that exhibit the Java Timezone Issue()

  • rightside

    Thanks for sharing

    Best regards

    lastest unix news

  • Jono B

    We all know there is such thing as a leap year. There is also such thing as a leap second (I think there was one at the end of 2008).

    Is there such thing as a leap millisecond (or any smaller time division for that matter). Or in other words, does:

    javatime == (unixtime * 1000)

    in fact hold true (for those people who do care about ms precision when doing the conversion)?

  • http://blog.peteonrails.com peteonrails

    I've never heard of a leap millisecond, but I wouldn't exclude it from the realm of possibility.

  • http://blog.peteonrails.com peteonrails

    I've never heard of a leap millisecond, but I wouldn't exclude it from the realm of possibility.