How to convert NFA to datatime and vice versa?

6

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

Searching, I found little, even more in Portuguese. To be honest, I have not yet seen enough reason to use it (so far as I understand it to be a complication, but it can be simple and I'm wrong).

There is a site that performs the conversion .

How to get to this result?

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

Does the NFA follow any pattern?

    
asked by anonymous 31.01.2017 / 14:13

1 answer

9

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

You can do 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;
}

How to use:

$timestamp = nfaToTs('A70171480');

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

See working at IDEONE .

From timestamp to NFA, it is a simple 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;
}

How to use:

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

echo tsToNfa($ts);

See working at IDEONE .


Curiosities:

  • The number of days between the posix timestamp and the first day of the year 1900 is 25567 , perhaps the 25569 has some relation to this 70-year setting;

  • In hex, the decimal 1.000.000 is 0xF4240 , which is the NFA time limit;

  • One day has 86400 seconds, and when we divide 1.000.000 by 86.400 result by chance is 11.574 ;


Further Reading:

  

How do hex numbers work ?

  

link

    
31.01.2017 / 18:46