How to convert NFA to datatime and vice versa?

Asked

Viewed 84 times

6

Having a date (e.g.: 2017-01-18 11:08:09 (format y-m-d H:i:s)), the generated NFA results in A70171480.

Searching on, I found little, even more in Portuguese. To be honest, I haven’t seen enough reasons to use it (so far I understand how complicated, but it can be simple and I’m wrong).

There is a site that performs the conversion.

How to reach this result?

And the opposite? having X NFA, how to convert it to datatime?

NFA follows some pattern?

1 answer

9


This format you put in the question stores the time in the last five hexadecimal digits, and the rest is the date (both with an adjustment factor).

You can make a conversion from NFA to timestamp with this formula:

function nfaToTs($nfa) { 
   $nfadata = hexdec(substr($nfa,0,-5));
   $nfahora = hexdec(substr($nfa,-5));
   return ($nfadata-25569) * 86400 + ($nfahora - 7) / 11.574;
}

Mode of use:

$timestamp = nfaToTs('A70171480');

// Formate como quiser depois:
echo gmdate('Y-m-d H:i:s', $timestamp);

See working on IDEONE.


Of timestamp for NFA, it is mere inversion of logic:

function tsToNfa($ts) {
   $hexdata = substr('0000' .dechex($ts / 86400 + 25569 ), -4);
   $hexhora = substr('00000'.dechex($ts % 86400 * 11.574 + 7), -5);
   return $hexdata.$hexhora;
}

Mode of use:

// qualquer coisa que retorne um timestamp
$ts = strtotime('1980-01-31 15:53:09 GMT'); 

echo tsToNfa($ts);

See working on IDEONE.


Curiosities:

  • The number of days between the Posix timestamp and the first day of the year 1900 is 25567, perhaps the constant 25569 have some relationship with this 70 year setting;

  • in hexa, the decimal 1.000.000 is 0xF4240, which is the NFA time limit;

  • one day has 86400 seconds, and as we divide 1.000.000 for 86.400 the result by chance is 11.574;


Further reading:

How hexadecimal numbers work?

http://php.net/manual/en/function.hexdec.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.