Rob Brooks-Bilson
Tech, Photography, Stuff
Tech, Photography, Stuff
October 11, 2007
I've seen a lot of postings on blogs and mailing lists lately showing various functions for calculating/displaying epoch time using ColdFusion.
There are actually two popular variants of epoch - UNIX epoch and Java epoch. UNIX epoch or "UNIX time" is defined by Wikipedia as:
"Unix time, or POSIX time, is a system for describing points in time: it is the number of seconds elapsed since midnight UTC of January 1, 1970, not counting leap seconds. It is widely used not only on Unix-like operating systems but also in many other computing systems. It is neither a linear representation of time nor a true representation of UTC (though it is frequently mistaken for both) as the times it represents are UTC but it has no way of representing UTC leap seconds (e.g. 1998-12-31 23:59:60)."
As you can see, UNIX epoch is expressed as the number of seconds since midnight UTC on January 1, 1970. Java epoch is still based on the same origination date, however, it is expressed in milliseconds.
There are several ways you can get the UNIX/Java epoch time in ColdFusion including using Java, the getTickCount() function, or using dateDiff().
What's important to note here is that the number of seconds/milliseconds returned depends on the method you use and your server's timezone. Both the Java method and getTickCount() return values that are relative to your server's local time, not UTC, which may or may not be how you need it within your application. The dateDiff() function returns the number of seconds relative to UCT time, not your server's local time.
Let's take a look at a few examples to help clarify my point. The following code uses the three methods above. I've written a udf to wrap the dateDiff() function because I want a reusable way to package things and I also want to account for the server's UTC offset using the dateConvert() function:
If you want to go from epoch to local ColdFusion server date/time, add the following code to the example:
You should note that the getTickCount() only started returning Java epoch time as of ColdFusion MX 6.1. Prior to that, it returned a sequential number, but it wasn't epoch.
Here are two links to articles I wrote for the ColdFusion Cookbook that give further clarification:
Convert a date/time to epoch
convert epoch to a CF date/time object
Additionally, there are four UDFs on cflib for converting to/from epoch:
epochTimeToDate()
epochTimeToLocalDate()
getEpochTime()
getEpochTimeFromLocal
millisecondsToDate()
convertActiveDirectoryTime
10/11/07 6:34 PM
You left out possibly the most annoying of all epoch times, Active Directory's date. Number of 100 nanosecond intervals since January 1st 1600.
10/11/07 6:40 PM
Terry,
That is annoying. Luckily, there's a UDF on cflib for the conversion. I added the link at the bottom of the post.
10/11/07 8:36 PM
actually you can get java epoch offset pretty easily:
now=now();
writeoutput("#now.getTime()#");
http://www.sustainablegis.com/blog/cfg11n/index.cf...
one of the things i discovered while in timezone hell ;-)
10/12/07 11:33 AM
Hey Paul,
I actually have the java example in my code - it's just split up a bit. As you mention, it does return epoch with the offset calculated. I feel your timezone pain. The political boundaries around timezones, and especially DST drive me up the wall!
9/18/09 4:54 AM
Hey Rob, Do you know how any of these epoch types are related to Javascript's epoch?