вторник, 10 декабря 2013 г.

Delphi's TDateTime to unixtime using PHP

if we have binary file wich was created using Delphi, and it's contain binary representation of TDateTime, you may read data using php the next way

function DelphiTimeToUnix(){
$fp = fopen("./dumped/file-1007.sta", "rb");
    if ($fp) {
        $i = 8;
        $data = fread($fp, $i); //Data
        $number = unpack("d", $data);
        $current_tz = date_default_timezone_get();
        date_default_timezone_set('UTC');
        $unix_start =  25569.0; //Unix start representation in a Delphi's format
        echo date("F j, Y, g:i a", ($number[1] - $unix_start + 0.00000001) * 86400) . PHP_EOL;
}
}

function UnixToDelphiTime(){
        $current_tz = date_default_timezone_get();
        date_default_timezone_set('UTC');
        $unix_start =  25569.0; //Unix start representation in a Delphi's format
        echo date( ($number[1] - $unix_start + 0.00000001) * 86400) . PHP_EOL;
}

The delphi dumps date as "double" which allocate 8 bytes for store

sources:

  1. http://en.kioskea.net/faq/998-parsing-a-binary-file-in-php
  2. https://igor.io/2012/09/24/binary-parsing.html
  3. http://delphidabbler.com/tips/30