How Often Are Numeric Dates Unique In ColdFusion?
As you probably know, ColdFusion can represent date/time values as numeric values. Not only can you get the milliseconds-since-Epoch from the underlying Java data/time object, you can also get the fractional-days since December 30, 1899. When dealing with the latter values - fractional days - I was curious to see what fractional time-delta would result in a unique numeric value.
To test this, I took a static start date and iterated over it 2,001 times, each time adding a millisecond. Then, I multiplied that value by one, which coerced it into a numeric value. Once I had the collection of numeric values, I looped over them to figure out how often they change:
<cfscript>
startTime = createDateTime( 2014, 12, 15, 0, 0, 0 );
// I hold the date/time stamps converted to a numeric representation.
numericTimes = [];
// Gather the numeric dates, each one MILLISECOND apart from the last.
for ( i = 0 ; i <= 2001 ; i++ ) {
arrayAppend(
numericTimes,
( dateAdd( "l", i, startTime ) * 1 )
);
}
lastValue = 0;
// Output the numeric values and indicate if two subsequent values match.
for ( i = 1 ; i <= arrayLen( numericTimes ) ; i++ ) {
value = numericTimes[ i ];
writeOutput( "+#( i - 1 )# : #value#" );
if ( value == lastValue ) {
writeOutput( " — same as last" );
}
writeOutput( "<br />" );
lastValue = value;
}
</cfscript>
When I run this code, I get 2,002 lines of output. For the sake of readability, I've removed many of the duplicates:
+0 : 41988
+1 : 41988 -- same as last
+2 : 41988 -- same as last
+3 : 41988 -- same as last
+4 : 41988 -- same as last
+5 : 41988 -- same as last
... removed for readability ...
+998 : 41988 -- same as last
+999 : 41988 -- same as last
+1000 : 41988.0000116
+1001 : 41988.0000116 -- same as last
+1002 : 41988.0000116 -- same as last
+1003 : 41988.0000116 -- same as last
+1004 : 41988.0000116 -- same as last
+1005 : 41988.0000116 -- same as last
... removed for readability ...
+1998 : 41988.0000116 -- same as last
+1999 : 41988.0000116 -- same as last
+2000 : 41988.0000231
+2001 : 41988.0000231 -- same as last
As you can see, while date/time objects have millisecond precision, the numeric representation of fractional days is only unique to the second. This means the the following code is only unique once a second:
#( now() * 1 )#
This is good to know since I often use this kind of a value to cache-bust image and CSS (Cascading Stylesheet) source URLs. And again, I'm not saying that ColdFusion date/time objects have second-precision - you can clearly add milliseconds to a date/time object; I'm only looking at ColdFusion's unique fractional-day representation, which is [apparently] precise to the second.
Want to use code from this post? Check out the license.
Reader Comments
wow! great Ben, thanks!!