How to Convert Ldap Timestamp to Unix Timestamp

How to convert LDAP timestamp to Unix timestamp

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);

Unix timestamp to LDAP timestamp

You can make your function a bit more readable by naming the input parameter properly and assigning an intermediate variable before returning. Since $AD2Unix is actually a fixed term, it could probably be a constant; for now, I've just moved it to the top of the function:

function LDAPtoUnix($ldap_ts) {
$AD2Unix = ( (1970-1601) * 365 -3 + round((1970-1601)/4) ) * 86400;

$secsAfterADepoch = $ldap_ts / 100000000;
$unix_ts = intval( $secsAfterADepoch - $AD2Unix );

return $unix_ts;
}

We now have 2 lines to invert; they clearly need to happen in reverse order, and then using simple algebra to rearrange the terms in each (/ becomes *, - becomes +) we get this:

function UnixtoLDAP($unix_ts) {
$AD2Unix = ( (1970-1601) * 365 -3 + round((1970-1601)/4) ) * 86400;

$secsAfterADepoch = intval( $AD2Unix + $unix_ts );
$ldap_ts = $secsAfterADepoch * 100000000;

return $ldap_ts;
}

A couple of tests suggest this inverts the original nicely.

Convert timestamp from MS AD in Linux

You can do this with awk, processing the string with the required calculations taken from How to convert LDAP timestamp to Unix timestamp and running it through awk's strftime function to get the locale date:

awk '{dat=($0/10000000)-11644473600;print strftime("%c",dat) }' <<< "131963400057892956"

Wed 06 Mar 2019 10:00:05 GMT

Change %c as required.

Convert LDAP timestamp to different format in Linux shell

Knowing the exact input format of the LDAP authTimestamp (e.g., 20200415062835Z) I came up with a fairly simple function to convert it to YYYY-MM-DD hh:mm:ss format in case the input matches a given regular expression.

function _convert_ldap_timestamp() {
ts=$1
if [[ "${ts}" =~ ^[0-9]{14}Z$ ]]; then
echo "${ts:0:4}-${ts:4:2}-${ts:6:2} ${ts:8:2}:${ts:10:2}:${ts:12:2}"
fi
}

Example execution:

> _convert_ldap_timestamp 20200804154203Z
2020-08-04 15:42:03

How to to convert from ldap time format to js date format?

Based on the question here*, 1.29265206716E+17 represents 2010-08-17T12:11:11Z so that can be used as a test value. The LDAP time value is in units of 0.0000001 of a second, whereas ECMAScript uses 0.001 of a second.

So the steps are:

  1. Convert the LDAP time value to milliseconds (divide by 1e4)
  2. Get the time difference between 1601-01-01 and 1970-01-01 in milliseconds
  3. Subtract the difference
  4. Pass to the Date constructor to create a Date object

Which can be combined into a single expression:

function ldapToJS(n) {
// Longer, equivalent to short version // return new Date(n/1e4 + new Date(Date.UTC(1601,0,1)).getTime());
// Shorter, more efficient. Uses time value for 1601-01-01 UTC return new Date(n/1e4 - 1.16444736e13);}
console.log(ldapToJS(1.29265206716E+17).toISOString()); // 2010-08-17T02:11:11.600Zconsole.log(ldapToJS(1.3160237812e17).toISOString()); // 2018-01-12T13:36:52.000Z
function doConversion(){ document.getElementById('dateString').textContent = ldapToJS(+document.getElementById('ldap').value).toISOString();}
<input placeholder="LDAP time value" id="ldap"><button onclick="doConversion()">Convert</button><br><span id="dateString"></span>

How to convert local time to LDAP timestamp in Java

java.time and ThreeTen Backport

    Instant ldapEpoch = Instant.parse("1601-01-01T00:00:00Z");
ZoneId zone = ZoneId.of("America/Port-au-Prince");

LocalDate date = LocalDate.now(zone);
Instant startOfDay = date.atStartOfDay(zone).toInstant();
Duration sinceLdapEpoch = Duration.between(ldapEpoch, startOfDay);
assert sinceLdapEpoch.getNano() == 0 : sinceLdapEpoch;
long ldapTimestamp = sinceLdapEpoch.getSeconds() * 10_000_000;
System.out.println(ldapTimestamp);

Output is (tested on jdk1.7.0_67):

132114384000000000

The start of the day differs from time zone to time zone. I have used America/Port-au-Prince as an example, you need to make your own pick. Use ZoneOffset.UTC if that is what you require.

For the end of the day use date.plusDays(1) instead of date in the same calculation. That will give you the first moment of the following day. Can you have the last moment of the current day instead? No, and you shouldn’t want to either. There is no last moment of a day. A day is a half-open time interval from the first moment of the day inclusive to the first moment of the next day exclusive. The best and the correct way to do in programming is to handle it as such (you may of course cheat and subtract 1 from the result you get to obtain the last possible LDAP timestamp of the day, not recommended).

I am using java.time, the modern Java date and time API. We don’t want to use Date or Calendar or other of the poorly designed and long outdated date and time classes in Java. java.time is so much nicer to work with.

Question: is there a way out using lower version like java 7?

Yes, java.time works nicely on Java 7. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.


Related Topics



Leave a reply



Submit