Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Attribute Conversions from AD to ADAM

68 views
Skip to first unread message

Crul

unread,
Apr 12, 2010, 12:58:19 AM4/12/10
to
Hey Guys,


Does anyone know an easy way to convert an AD attribute like
"pwdLastChanged" that is in Milliseconds from 1601 to a usable
Timestamp for ADAM. Is there an easy way to convert in TDI? I have
tried messing round with the "Date" Java util but cant seem to get
this one to work. I know there is a Windows app that can do it but was
hopping to do the conversion in TDI itself.
Anyone successfully done this?

Cheers
Crul

Eddie Hartman

unread,
Apr 12, 2010, 5:23:59 AM4/12/10
to
To write a Date to AD (or other LDAP) just convert it
to a Java Date like this, Crul:
---
dt = new java.util.Date(msecs);
---
Make sure the msecs are based on the Unix epoch.
If it resolves wrong, calculate the difference in msecs
you need to add/subtract to get the Date you want.

I do not have an ADAM to test with, so please let
me know what I am missing.

-Eddie

Eddie Hartman

unread,
Apr 12, 2010, 5:27:09 AM4/12/10
to
Ok, so if the msecs you get from pwdLastChanged
is msecs since the last change, try subtracting this
from today:
---
today = new java.util.Date();
msecs = today.getTime();
lastchangeMsecs = work.getObject("pwdLastChanged");
changed = msecs - lastchangeMsecs;
changeDate = new java.util.Date(changed);
task.logmsg("Password last changed: " + changeDate);
---
The coffee is starting to work :)

-Eddie

Crul

unread,
Apr 13, 2010, 12:48:05 AM4/13/10
to
Hey Eddie,

Thanks for looking at this,

I Still can’t quite get it right. I want to convert the PwdLastSet
attribute which is in format: "129144606760737926" and change it to a
Timestamp format like 20100413100455.0z

If I try:
changed.getObject("pwdLastChanged");
changeDate = new java.util.Date(changed);
I get an error:
java.lang.reflect.InvocationTargetException
Cused by:
java.lang.IllegalArgumentException
at java.util.Date.parse(Date.java:610)
at java.util.Date.<init>(Date.java:270)
... 27 more


so trying the following code

In the Output Map
-------------------------------------------
var msecss = work.getString("PwdLastSet");
task.logmsg("PwdLastSet : "+msecss);

var msecsd = new Date(msecss);
task.logmsg("New Date : "+msecsd);

times = new java.sql.Timestamp(msecsd.getTime());
task.logmsg("Timestamp : "+times);

var er = system.remove("-",times);
var ty = system.remove(" ",er);
var ed = system.remove(":",ty);
var fed = ed.substring(0,8);
var fee = ed.substring(8,15);
var ts = (fed + fee +"0Z");
task.logmsg("Converted TS : "+ts);

ret.value = ts;


This is what I’m getting in my logs :
6:27:51 PwdLastSet : 129144585766593641
16:27:51 New Date : 1/01/70 12:00
16:27:51 Timestamp : 1970-01-01 12:00:00.0
16:27:51 Converted TS : 19700101120000.0Z
----------------------------------------------

It seems that the line 'var msecsd = new Date(msecss);' is just
returning 1/01/70 12:00 rather than a converted date for
129144585766593641.

Also it seems Java uses 1/01/70 12:00 as its Epoch where as AD looks
like it uses 1/01/1601 12:00. Is this going to cause me an issue?
All I really want to do is convert a number which is in Milliseconds
since 1601 format and put it into a regular timestamp format for ADAM.

Sorry if I explained this poorly but It really has me stumped, but I
am guessing others would have attempted this before with TDI.

Many Thanks

Crul

Eddie Hartman

unread,
Apr 13, 2010, 3:35:49 AM4/13/10
to
My first question would be: what kind of Java obj
are you getting when you do the getObject().

dt = changed.getObject("pwdLastChanged");
task.logmsg("Class is: " + dt.getClass());

You will need to convert to an integer in order to
create a date based on it. Same goes for the other
code snippet where you do a getString("PwdLastSet").
Make an int value out of it first and see if that helps.

As far as the different epochs, that just means you
create a diff value that you can add to the AD one to
get the corresponding Java version. In your case you'll
need to add msecs to the AD time value to get the
Unix epoch you need for Java date functions.

-Eddie

Eddie Hartman

unread,
Apr 14, 2010, 10:08:16 AM4/14/10
to
Ok, it was a bit more involved that I thought.

---
// Create a Win 32 "Year 0" date
Win32Epoch = new
java.util.GregorianCalendar(1601,java.util.Calendar.JANUARY,1);
// Get the java.util.Date object
Win32EpochDate = Win32Epoch.getTime()
// Get the msecs from the Date
Win32EpochMSecs = Win32EpochDate.getTime();
// Now do the same for Java Epoch
JavaEpochDate = new java.util.Date();
JavaEpochMSecs = JavaEpochDate.getTime();

EpochDiff = Win32EpochMSecs - JavaEpochMSecs;
task.logmsg("-EpochDiff-: "+EpochDiff);

// Note that "var" is really only for documentation
// purposes - except in function bodies. All variables
// are otherwise global


var msecss = work.getString("PwdLastSet");

task.logmsg("PwdLastSet (msecs): "+msecss);

msecsi = new java.lang.Long(msecss);
task.logmsg(" msecsi : " + msecsi);
AdjustedMsecs = msecsi - EpochDiff;

pwdLastSetDate = new java.util.Date(AdjustedMsecs);

task.logmsg( " PwdLastSet (date): " + pwdLastSetDate);
---

This date looked right for my system.

Hope it helps!
-Eddie

Crul

unread,
Apr 15, 2010, 5:58:09 AM4/15/10
to
Thanks Eddie,

Yeah its quite an involved process.

I tried the following code:

// Create a Win 32 "Year 0" date
Win32Epoch = new
java.util.GregorianCalendar(1601,java.util.Calendar.JANUARY,1);
// Get the java.util.Date object
Win32EpochDate = Win32Epoch.getTime()
// Get the msecs from the Date
Win32EpochMSecs = Win32EpochDate.getTime();
// Now do the same for Java Epoch
JavaEpochDate = new java.util.Date();
JavaEpochMSecs = JavaEpochDate.getTime();

EpochDiff = Win32EpochMSecs - JavaEpochMSecs;
task.logmsg("-EpochDiff-: "+EpochDiff);

// Note that "var" is really only for documentation
// purposes - except in function bodies. All variables
// are otherwise global
var msecss = work.getString("PwdLastSet");
task.logmsg("PwdLastSet (msecs): "+msecss);

msecsi = new java.lang.Long(msecss);
task.logmsg(" msecsi : " + msecsi);
AdjustedMsecs = msecsi - EpochDiff;

pwdLastSetDate = new java.util.Date(AdjustedMsecs);

task.logmsg( " PwdLastSet (date): " + pwdLastSetDate);

time = new java.sql.Timestamp(pwdLastSetDate.getTime());
task.logmsg("Timestamp : "+time);
var er = system.remove("-",time);


var ty = system.remove(" ",er);
var ed = system.remove(":",ty);
var fed = ed.substring(0,8);
var fee = ed.substring(8,15);
var ts = (fed + fee +"0Z");
task.logmsg("Converted TS : "+ts);

I see how its all working together but my result I get is:

21:49:46 Timestamp : 4094972-01-19 00:12:12.352
21:49:46 Converted TS : 4094972011900120Z

The actual conversion comes to something like this:
21:08:51.8545664 - 6/04/2010 9:08:51 a.m.

I c;ant see which line I am doing wrong.

Sorry to be a pain but this one goes over my head a bit :P


Thanks again Eddie

Crul

Crul

unread,
Apr 26, 2010, 4:30:32 AM4/26/10
to
Any ideas on why when I run that script above if returns a date that
is no where near what it should be.
It should be something like this : 2010040421085100.0z

But its returning 409497201190012.0Z a date a looong way in the
future

I cant see where I'm going wrong.

Cheers

Eddie Hartman

unread,
Apr 27, 2010, 6:22:47 AM4/27/10
to
Ok, Crul, after some more Googling I found this code,
modified (without testing) to fit this problem:

---
s = work.getString("PwdLastSet");
pwdSetDate = new java.lang.Long.parseLong(s);
// adjust factor for converting it to java
timeAdjust= new java.lang.Long(11644473600000);
convdate = new java.lang.Long(pwdSetDate/10000-timeAdjust);
pwdSet = new java.util.Date(convDate);
---

See if this works any better. The whole trick here is
to get the right epoch diff when doing the conversion.

-Eddie

0 new messages