Fellow Monks,
I had a need today to find out when a person last logged into the network. It's a windows network, and the DC's are windows 2003 servers. So it has the one attribute needed to assist with this task, lastLogonTimestamp.
The issue with this value is though, it's a 64bit in of the number of 100 nanoseconds since 1/1/1601. Working out the date left me bit lost. Mostly because I was looking for the "right" way to do it, and I'm not saying that I have neccessarily hit on it the right way, but IF nothing else, this is at least a perl code sample for others to work on.
Of course this is a function where it's actually used, and the time stamp provided is an example only.
I hope I have it right, feel free to correct etc.
#!perl # it's run on windows that's why not a full path
# Code Snippet to show how to get the date of the last login time
# of an AD user on windows 2003 stored in the lastLogonTimestamp attri
+bute.
#
# For more inforamtion about the value and windows use of Epoch,
# basically put, the number of 100-nanosecond intervals that passed be
+tween January 1, 1601 and the time the user last logged on
#
# Reference to the maths: http://www.microsoft.com/technet/scriptcente
+r/topics/win2003/lastlogon.mspx
# The document also covers the issue replication of this attribute, be
+ warned, the attribute
# lastLogonTimestamp is replicated only once every 14 days.
#
# How to get the attribute out of AD is up to you. I was using Net::L
+DAP at the time.
#
# Hopefully this will be found and help out anyone else stuck with thi
+s problem.
#
use strict;
# we need to handle the large int. It's a 64bit int
use Math::BigInt;
# Use Date::Calc to work out the days and date since 1/1/1600
use Date::Calc qw/Add_Delta_Days/;
my $lastLoginTime = '127912653523035916';
my $loginTime = Math::BigInt->new( $lastLoginTime );
# Based on the document above and the words taken directly from:
# there are 1,000,000,000 nanoseconds in a second;
# therefore, there are 10,000,000 100-nanosecond intervals in a single
+ second (10,000,000 x 100 = 1,000,000,000).
$loginTime = $loginTime / (60 * 10000000 ); # 60 seconds in a minute!
# And because there are 1,440 minutes in every 24-hour day, this line
+of code tells us how many days have elapsed:
$loginTime = $loginTime / 1440;
# and now, to get the date of the last login:
my ($year,$month,$day) = Add_Delta_Days( 1601, 1, 1, $loginTime );
print "$year-$month-$day\n";
__END__