Tuesday, August 28, 2007

Writing UTC Coded Time to AD

I recently created several custom attributes for a client of the AD syntax type "String (Generalized Time)" also known as (or displayed in the UI as) UTC Coded Time.  It's a string that's formatted in Universal Time or what's known as Coordinated Universal Time.  For instance, the date string "2007-08-28 00:00:00" will look very familiar if you've ever imported a Date string from a SQL MA in MIIS.  It's just a string, and if you wanted to flow it out to Active Directory you'd need to convert it depending on the type of attribute you're going to store it in.  In my case, writing it out to UTC Coded Time requires that it look like this instead - "20070828070000.0Z".  Let me explain...

First off, it was much harder than I expected to track down the correct bit of code to do this, I kept expecting to find a method buried within the .NET DateTime object that would give me a result formatted the way I wanted, but alas none such method exists.

Second, since the data source was giving me a midnight timestamp relative to me, I couldn't just write the timestamp as 000000.0Z as I'm in the Pacific Standard Timezone and that wouldn't be midnight on the west coast when translated back to local time.

Last, I need to thank both Joe Stepongzi and Joe Kaplan on confirming the direction -so let's see what we have then:

' Formatting strings for writing UTC Coded Time to Active Directory 
' For use with AD Attributes of the String(Generalized Time)/UTC Coded Time syntax (2.5.5.11)

Dim strDate As String
Dim strUTC As String

' data source records midnight local time
strDate = "2007-08-28 00:00:00"
strUTC = System.DateTime.Parse(strDate).ToString("yyyyMMddHHmmss.0Z")
strUTC = System.DateTime.Parse(strDate).ToUniversalTime.ToString("yyyyMMddHHmmss.0Z")
'Returns (relative to PST):
'20070828000000.0Z
'20070828070000.0Z - use this one for UTC Coded Time

So that's it, you want to use the .ToUniversalTime() method to convert to UTC before converting the format, this takes into account that the string date you're formatting is already in local time.  If the date you were manipulating was already in Universal time then you'd skip this method and go directly to the .ToString method.  Now that you have the value, writing it out to AD via the Export Attribute Flow is simple enough:

csentry("UTCAttribute").Value = strUTC
There are probably 37 other ways to format the string into the same output but I was partial to System.DateTime and utilizing the .Parse method allows the incoming format to vary considerably without lots of needless string formatting of the incoming date value.  If I had more minutes I might record some of those methods here, but I don't so enjoy!

Friday, August 03, 2007

ILM Coding Tracks now at Desert Code Camp

Desert Code Camp, one of a string of successful "" across the US is hosted by the University of Advancing Technology here in Phoenix, AZ. 

I will be presenting the following session:

Coding Effective Advanced Attribute Flows for MIIS 2003/ILM 2007

You can request additional sessions or suggest topics on the DCC website!

My colleague Jerry Camel will be presenting a session as well on extensions in Visual Studio 2005 that has some very compelling hooks for not only MIIS/ILM implementations but VS projects in general - watch his blog for more information!

Newer Posts Older Posts Home