Gossamer Forum
Home : General : Perl Programming :

Need help convert timestamp to date

Quote Reply
Need help convert timestamp to date
Hi,
I have a LDAP Active Directory LastLogon timestamp that I'm trying to convert to human-readable date.
I've tried using UnixDate, ParseDate and other perl modules with no success. The reason being the timestamp(I guess the bits being used) in LDAP is different from Unix timestamps.
I'd really appreciate any ideas.

Here is an example: lastLoginTime is being read from LDAP Active Directory LastLogon attribute which can be

lastLoginTime = 127185822896362317

%my $date_format = '%a, %b %e %Y at %i:%M%p';
%my $lastLoginDate = &UnixDate(Date::Manip::ParseDateString("epoch ".$lastLoginTime), $date_format);
Last Login: The user last logged into the account on
<% $lastLoginDate %>

The lastLoginDate being printed is Tue Dec 30, 1969 at ...
Where as, the actual date of last login is 1/14/2004 2:31:29 PM
Thanks for the help
Quote Reply
Re: [meenu] Need help convert timestamp to date In reply to
Hello Meenu , See http://public.activestate.com/...obyeverett/tips.html

for the section on the page " Dealing With Returned TimeStamp Values "

hope this helps

cornball
Quote Reply
Re: [meenu] Need help convert timestamp to date In reply to
Hello Meenu, try :

#!/usr/bin/perl

# example given
# lastLoginTime = 127185822896362317
# 1/14/2004 2:31:29 PM

$win32Time ="127185822896362317";

$high = substr($win32Time,0,9);
$low = substr($win32Time,9,2);

$a_Temp = ((($high - 116444736) *100) + $low + 10800);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($a_Temp);

$year += 1900;
$mon += 1;

print "$mon/$mday/$year at $hour:$min:$sec \n";

# does this work ? with other lastLoginTime values ?

thanks cornball (-:

Last edited by:

cornball: Mar 25, 2004, 9:10 PM
Quote Reply
Re: [cornball] Need help convert timestamp to date In reply to
Hi Cornball,
It works. Thanks a lot. Smile
Quote Reply
Re: [meenu] Need help convert timestamp to date In reply to
Hello Meenu,

This code krap :

$high = substr($win32Time,0,9);
$low = substr($win32Time,9,2);

$a_Temp = ((($high - 116444736) *100) + $low + 10800);


is supposedly a way to take the win 32 time and convert it to unix time.

I am not positive it will work always. I made it up from reading a text explanation of the process.

Please test with other time values before you use in the real world or trust this process.

It seemed to work with your one example but it is really based on that one example only.

Thanks cornball

Last edited by:

cornball: Mar 26, 2004, 5:27 PM
Quote Reply
Re: [cornball] Need help convert timestamp to date In reply to
Hi Cornball,
Thanks for the update. Yes, I saw a VBScript that was doing something similar(converting win32 timestamp to unix timestamp). I've tested with a few other timestamps and it worked fine. I will test it with more timestamps to make sure it works before I implement it in realtime.
Thanks again.
Quote Reply
Re: [meenu] Need help convert timestamp to date In reply to
hey cornball,
One thing I found out though is it was showing time 3hrs ahead of the acutal time(so I don't have to add the 10800 to $low i.e., 3hrs=180*60=10800) and it seems to work right now.
thanks again!