Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Converting Active Directory date Time to something useful

by astroboy (Chaplain)
on Jun 25, 2014 at 02:56 UTC ( [id://1091119]=perlquestion: print w/replies, xml ) Need Help??

astroboy has asked for the wisdom of the Perl Monks concerning the following question:

When Active Directory user is queried using Win32::OLE, date/time-based fields such as accountExpires are returned as Win32::OLE objects.

Dumping the value does nothing to elucidate the value

print Dumper $value;

Gives

$VAR1 = bless( {}, 'Win32::OLE' );

Does anyone know how to turn this into a date?

Replies are listed 'Best First'.
Re: Converting Active Directory date Time to something useful
by NetWallah (Canon) on Jun 25, 2014 at 06:00 UTC
    My experience with OLE/AD his has atrophied from lack of use .. but I believe the accountExpires is a Win32::OLE::Variant type that could be accessed thus:
    use Win32::OLE::Variant; my $Exp_yymmdd = $value->Date("yyyy-MM-dd");
    i.e call the "Date" method on the OLE object.

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: Converting Active Directory date Time to something useful
by astroboy (Chaplain) on Oct 21, 2014 at 00:59 UTC
    In case someone else is researching this, I've put down a solution that I've cobbled from http://code.activestate.com/lists/perl-win32-users/10905/. I use DateTime objects for my date in my app, so I've pulled the relevant bits out:
    use DateTime; use DateTime::Format::Epoch; # Windows epochs start on 1 Jan 1601 my $base_dt = DateTime->new( year => 1601, month => 1, day => 1 ); my $formatter = DateTime::Format::Epoch->new( epoch => $base_dt, unit => 'seconds', type => 'int', # or 'float', 'bigint' skip_leap_seconds => 1, start_at => 0, local_epoch => undef, ); # $value contains the Win32:OLE date my $hi_val = $value->HighPart; my $low_val = $value->LowPart; my $factor = 10000000; my $unp_val = pack("II", $low_val, $hi_val); my ($bVp, $aVp) = unpack("LL", $unp_val); $unp_val = ($aVp * 2**32 + $bVp) / $factor; if ($unp_val != 0) { my $dt = $formatter->parse_datetime($unp_val); # Good habit - always explicitly set to UTC zone before # converting to the local zone $dt->set_time_zone('UTC'); $dt->set_time_zone('local'); print $dt->strftime('%Y-%m-%d %H:%M:%S'); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1091119]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-19 11:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found