The account-expires attribute in Active Directory carries a value of time in 100 nanosecond units since `January 1, 1601 00:00 UTC`

. In fact quite a lot of Microsoft products are now using this epoch. Java and Unix use `January 1, 1970 00:00 UTC`

as their epoch.

This is important information if you’re ever going to develop code in ColdFusion that needs to, for example, figure out whether or not a domain account has expired.

Another piece of information that’s handy to have is an understanding of how ColdFusion handles large numbers. Numeric values are handled as signed 32-bit values that can range from -2,147,483,648 to 2,147,483,647. ColdFusion can handle much larger numbers, but it does so by converting numeric values into strings and then using its own library to handle math operations based on these strings. And it stores these large values in scientific notation. The problem with this is you start to lose precision. For example, if you run the following CF snippet:

`<cfoutput>#( 2147483648 * 2147483648 )#</cfoutput><br>`

<cfoutput>#( 2147483648 * 2147483648 + 1)#</cfoutput>

The output is the same for both operations, “`4.61168601843E+018`

“. This loss of precision makes it impossible to calculate the exact time `account-expires`

refers to. So what else can we do?

One trick that sort of works is to divide the value in `account-expires`

by 10000000 to convert from 100 nanoseconds to seconds. This will significantly shorten the number. So let’s try that on an account-expires value of “129247307050000000” (that’s 7/27/10 @2:58.25 EDT). The line of CF looks like this:

`<cfoutput>#DateAdd( "s", ( 129247307050000000 / 10000000 ), "01/01/1601 12:00 AM")#</cfoutput>`

Try to run this code and you get the error message “Cannot convert the value 1.2924730705E10 to an integer because it cannot fit inside an integer.”. It turns out that `DateAdd()`

can’t handle a number larger than 2147483647 (the limit of a 32-bit signed integer). 2,147,483,647 seconds is about 70 years of time. To use `DateAdd()`

we need to make the account-expires value much smaller. We could, for example, calculate the account-expires value with respect to a different epoch, one that’s within 70 years of the time referenced by account-expires. We could use any date we want, but I’m going to stick with Java’s epoch of January 1, 1970 to keep things in-line with what’s probably the most common epoch among computer systems.

The number of seconds between January 1, 1601 and January 1, 1970 is 11,644,473,600. If we plug this into the code above we get this:

`<cfoutput>#DateAdd( "s", ( 129247307050000000 / 10000000 ) - 11644473600, "01/01/1970 12:00 AM")#</cfoutput>`

Run this and we get a result! Specifically `{ts '2010-07-27 19:58:25'}`

. Remember this is in UTC, we’ll need to convert to our local timezone. We’ll do that in just a moment. But first…

You should have recognized by now that this solution won’t work forever. It will fail whenever the data in account-expires is after January 19, 2038. Perhaps this doesn’t bother you since whatever applications you’re developing now will surely be either obsolete or running on a more modern ColdFusion system where `DateAdd()`

can deal with larger numbers before account-expires with that large a value are in use.

Perhaps. But I hate making assumptions like that. Plan for the future! Assume nothing!

If we want lasting code free of the 2038 limitation we’ll need to steer clear of ColdFusion date and time functions. We can do this by using Java objects, specifically `java.util.Calendar`

to manage date and time, and `java.math.BigInteger`

to handle the very large numbers we’ll be working with.

The process is fairly straightforward, but there are a few things to watch out for. Java time is measured in milliseconds, not 100 nanoseconds, so we’ll have to do some conversion. Java and Microsoft’s epochs are different. Calendar objects with a date and time earlier than Java’s epoch will have a negative millisecond value. And we need to calculate this date/time with respect to UTC (GMT timezone) not the local timezone.

First create a BigInteger object. Initialize it to the value of account-expires, and then convert it to milliseconds as that’s the unit of time Java likes to work with.

`01: <cfset variables.bigInt = CreateObject( "java", "java.math.BigInteger" ) />`

02: <cfset variables.expTime = variables.bigInt.init( JavaCast( "String", "#arguments.accountExpires#" )) />

03: <cfset variables.expTime = variables.expTime.divide( variables.bigInt.valueOf( "10000" )) />

You can initialize BigInteger objects with strings and numerical values. I’m specifically casting account-expires as a string so that ColdFusion doesn’t treat it like an integer and try to put it into scientific notation. Note in line 3 the use of BigInteger’s `valueOf()`

function. This takes a string and returns a BigInteger value. Math functions for BigInteger objects require BitInteger arguments, thus we need to pass all values (even static ones) through `valueOf()`

instead of `JavaCast()`

.

Next we set up the calendar object. This means creating a `java.util.TimeZone`

object, a `java.util.Calendar`

object, and then initializing the calendar to the GMT timezone. Once the calendar object is initialized we set it to Microsoft’s epoch.

`04: <cfset variables.tz = CreateObject( "java", "java.util.TimeZone" ) />`

05: <cfset variables.Calendar = CreateObject( "java", "java.util.Calendar" ) />

06: <cfset variables.gCal = variables.Calendar.getInstance( tz.getTimeZone( "GMT" )) />

07: <cfset variables.gCal.set( 1601, 0, 1, 0, 0, 0 ) />

Note that the second argument of the calendar object’s `set()`

function is 0. Java’s calendar object’s month index begins with 0 for January instead of 1. This is something to be aware of whenever dealing with Java calendar objects.

This next step requires a little explanation. `variables.expTime`

is now in milliseconds thanks to line 3. I’m going to add the value of the calendar, in milliseconds, to `variables.expTime`

. Because the calendar is set to a date before Java’s epoch it will actually be a negative number. The addition operation is actually a subtraction operation. The resulting value of `variables.expTime`

will be the number of milliseconds since Java’s epoch.

`08: <cfset variables.expTime = variables.expTime.add( variables.bigInt.valueOf( "#variables.gCal.getTimeInMillis()#" )) />`

All we need to do now is reset the calendar to the value we now have in `variables.expTime`

, then convert it to our local timezone and we’re done!

`09: <cfset variables.gCal.setTimeInMillis( variables.expTime.longValue() ) />`

10: <cfset variables.gCal.setTimeZone( tz.getDefault() ) />

Last, but not least, we convert the calendar object into a ColdFusion date/time object with ColdFusion’s `CreateDateTime()`

function.

`11: <cfset variables.expDateTime = CreateDateTime(`

variables.gCal.get( variables.gCal.YEAR ),

variables.gCal.get( variables.gCal.MONTH ) + 1 ,

variables.gCal.get( variables.gCal.DAY_OF_MONTH ),

variables.gCal.get( variables.gCal.HOUR_OF_DAY ),

variables.gCal.get( variables.gCal.MINUTE ),

variables.gCal.get( variables.gCal.SECOND )

) />

Note that I’m adding 1 to the month variable because in Java 0 = January, but in ColdFusion 1 = January.

And there you have it. Converting from Microsoft’s 01/01/1601 100 nanosecond based timestamps to something a bit more usable in ColdFusion.

Of course you’ll need a reverse of this process as well, and I’ve already got that for you:

`01: <cfset variables.tz = CreateObject( "java", "java.util.TimeZone" ) />`

02: <cfset variables.Calendar = CreateObject( "java", "java.util.Calendar" ) />

03: <cfset variables.gCal = variables.Calendar.getInstance( tz.getDefault() ) />

04: <cfset variables.gCal.set(

Year( arguments.Date ),

Month( arguments.Date ) - 1,

Day( arguments.Date ),

Hour( arguments.Date ),

Minute( arguments.Date ),

Second( arguments.Date )

) />

05: <cfset variables.bigInt = CreateObject( "java", "java.math.BigInteger" ) />

06: <cfset variables.expTime = variables.bigInt.init( JavaCast( "String", "#variables.gCal.getTimeInMillis()#" )) />

07: <cfset variables.expTime = variables.bigInt.divide( variables.bigInt.valueOf( "1000" )) />

08: <cfset variables.expTime = variables.expTime.add( variables.bigInt.valueOf( "11644473600" )) />

09: <cfset variables.expTime = variables.expTime.multiply( variables.bigInt.valueOf( "10000000" )) />

10: <cfreturn variables.expTime.longValue() />

You should be able to follow along based on my explanation earlier in the article. However there are a couple extra bits I’d like to point out.

The calendar is initialized to the local timezone. We don’t have to worry about converting back to UTC/GMT because the calendar function getTimeInMillis() will give you the number of milliseconds from Java’s epoch, which is in UTC.

On line 8 I’m adding the number of seconds from Microsoft’s epoch to Java’s epoch. You’ll also notice the line before that I’m dividing by 1000 to convert time from milliseconds to seconds and after I add the epoch difference I’m converting to 100 nanoseconds by multiplying by 10000000. So why not skip the conversion to seconds and simply add three more zeroes to the end of the epoch difference (making the time in milliseconds) and multiplying by 10000?

The reason is that there’s a problem with precision and the resulting value will vary by a few hundred milliseconds. Since the times I’m working with are in seconds, those extra milliseconds could be ignored. But by dividing, adding, and then multiplying like this I don’t get the millisecond variances. I’m sure there’s better stuff on the ‘net to explain precision with BigInteger objects if you’re inclined to investigate further.

**Before you go** I have one piece of information I learned while working on this particular topic.

ColdFusion treats all date and time values as if they are in the local timezone. Specifically, if your timezone has daylight savings then all dates are treated as if they have daylight savings, including GMT/UTC times which do NOT have daylight savings time. DateConvert() will not protect you from this issue. There’s a deeper explanation here.

My solution is to check if `GetTimeZoneInfo().isDSTon`

is TRUE. If it is then I need to add (or subtract) 1 hour from my UTC time. Note that this is only needed when converting between UTC and local timezones with ColdFusion date and time functions. The above code specifically stays away from ColdFusion date and time functions so this isn’t a problem with my example. But it is something to keep in the back of your head when you start to notice dates are being calculated an hour off the time they should be.