Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert LDAP timestamp to Unix timestamp

When I retrieve the LDAP attribute "pwdLastSet" of an Active Directory using PHP I get a value like 1.29265206716E+17. I know that this value represents the date "Tue Aug 17 2010 14:11:11 GMT+0200".

How can I convert this value to a Unix timestamp in PHP? Thanks for any hints!

like image 203
Nick Avatar asked Jan 10 '11 13:01

Nick


People also ask

How do I convert LDAP to time?

The current timezone in LDAP is 0. If you want to increase or decrease the timezone, you need to follow the formula 60 seconds multiply 60 minutes multiplied by X hours (60 * 60 * X) = Y will get the number of seconds.

What is Etime in LDAP?

The timestamp is the number of 100-nanosecond intervals (1 nanosecond = one billionth of a second) since Jan 1, 1601 UTC. The current LDAP/Win32 FILETIME is 133050816050000000.


2 Answers

Please see here.

Actually it boils down to converting the FILETIME timestamp into a UNIX timestamp:

$fileTime = "130435290000000000";
$winSecs       = (int)($fileTime / 10000000); // divide by 10 000 000 to get seconds
$unixTimestamp = ($winSecs - 11644473600); // 1.1.1600 -> 1.1.1970 difference in seconds
echo date(DateTime::RFC822, $unixTimestamp);
like image 177
Stefan Gehrig Avatar answered Sep 19 '22 20:09

Stefan Gehrig


Rather than ask the same question for another language.

Python 3:

from datetime import datetime, timedelta

def ldap2datetime(ts: float):
    return datetime(1601, 1, 1) + timedelta(seconds=ts/10000000)

print(ldap2datetime(132255350424395239).isoformat())
# 2020-02-07T07:44:02.439524
like image 41
Samuel Harmer Avatar answered Sep 19 '22 20:09

Samuel Harmer