Monday, April 5, 2010

ldap time to perl time()

This might save 30 seconds for someone else who wrongly thought that DateTime::Format:ISO8601 would parse an LDAP timestamp.

# Takes an ISO8601ish LDAP timestamp datatype and converts it into a perl time()
# compatible structure.  Requires Time::Local.
sub ldap2time
    my $ldap_ts = shift;
    return unless $ldap_ts =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z/;
    my ($year, $mon, $day, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6);
    return timegm($sec, $min, $hour, $day, ($mon-1), $year);

No comments: