I am writing an application where I need to get the epoch time on the server side using ColdFusion 8 running on Windows Server 2008 and client side using javascript (testing in Google Chrome). The problem is that the epoch time generated by ColdFusion is one hour behind the one generated by javascript. I have verified that the date/time settings are correct both client side and server side.
This is how I am setting the time stamp with ColdFusion:
<cfset cfEpoch = DateDiff("s", "January 1 1970 00:00", DateConvert("Local2utc", now()))>
And this is how I'm setting it with javascript:
var jsEpoch = Math.round(new Date().getTime()/1000.0);
The javascript epoch matches the one on this website (http://www.epochconverter.com/) which makes sense because they're using the same method I'm using. The ColdFusion epoch is one hour behind. Here's what I've run to try to sort this out:
<cfset localDate = now()>
<cfset utcDate = DateConvert("Local2utc", localDate)>
<cfset epoch = DateDiff("s", "January 1 1970 00:00", utcDate)>
<cfoutput>
Local Date: #localDate# <br>
UTC Date: #utcDate# <br>
Epoch: #epoch#
</cfoutput>
That code outputs:
Local Date: {ts '2013-04-30 17:44:56'}
UTC Date: {ts '2013-04-30 21:44:56'}
Epoch: 1367354696
So I'm at a loss. Both the local date and UTC date values are correct. It appears that the only explanation is that the DateDiff() function isn't working properly, but I've tested it with other dates and it seems to work fine. I suppose I can just add 3600 to the epoch value it generates but I'd rather not do that without knowing why I'm getting an incorrect value in the first place. Does anyone see what I'm missing here?
In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system.
Epoch Time Difference FormulaMultiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.
What is epoch time? The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
Writing a little script you might notice something very strange.
This is what it output for me.
So converting epoch to local time is spot on, but the other way around isn't. This is running on CF8 too, so if you convert Epoch to local time, it seems to do it right.
Try it out.
<script>
var jsEpoch = Math.round(new Date().getTime()/1000.0);
document.write(jsEpoch + ' - Javascript time <br>');
</script>
<cfset TheDate = now()>
<cfoutput>
#DateDiff("s",DateConvert("utc2Local", "January 1 1970 00:00"), TheDate)# - Epoch seconds (convert Epoch to local time)<br>
#DateDiff("s", "January 1 1970 00:00", DateConvert("Local2utc", TheDate))# - Epoch seconds (convert local time to UTC)
</cfoutput>
ColdFusion 8 is pretty old, so you should check your JVM version. You may be running an outdated version that doesn't account for the new DST rules. See this Adobe forum for more:
"The US day light savings rules changed since that Version of ColdFusion and it's underlining JRE where released. You need to update the JRE to at least 1.6.12 to get the new rules."
UPDATED:
After further testing, the JVM is not the issue. I get the same results as above on CF9, and we're already past the time frame where the old rules and new rules would return different results.
However, here are a couple ways you could get the correct result (thanks to Gavin's answer):
function getEpoch(date localDate=now()){
return dateDiff('s', dateConvert('utc2Local', createDateTime(1970, 1, 1, 0, 0, 0)), localDate);
}
Or, you can access the underlying Java method (wrap the result in int()
to drop the decimal):
int(createObject('java', 'java.lang.System').currentTimeMillis()/1000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With