Author Topic: weird code (delphi?) puzzle - date as binary.  (Read 1050 times)

Euan Uzami

weird code (delphi?) puzzle - date as binary.
« on: 20 August, 2010, 06:36:52 pm »
I need a generic rule that will convert the binary representation of a date into the actual date.

The program that creates the binary data is written in delphi if that helps, but unfortunately it is not in the best interests of its author to help me read the file.
Hence I need to figure it out.
Weird thing is it seems to be stored as 10 bytes, not 8, or 4.
(There is actually 11, the first one is always 0x05, but I've deduced that that just means "there follows a date")

Here are some examples :
I can create more examples to test, but not automatically, hence generation of a lookup table is not possible.

           "yyyy/MM/dd" :
           "1965/04/23"   = { 0x00, 0xC8, 0x7E, 0x51, 0x0C, 0xC0, 0x5F, 0xBA, 0x0D, 0x40 }  
           "1999/12/15"   = { 0x00, 0x60, 0xBF, 0x28, 0x06, 0xE0, 0x9D, 0x8E, 0x0E, 0x40 }  
           "1900/01/01"   = { 0x00, 0x00, 0x00, 0xDE, 0x2F, 0x8A, 0x01, 0xB8, 0x00, 0x40 }  
           "1899/12/31"   = { 0x00, 0x00, 0x00, 0xBC, 0x5F, 0x14, 0x03, 0xF0, 0xFF, 0x3F }  
           "1899/12/29"   = { 0x00, 0x00, 0x00, 0xBC, 0x5F, 0x14, 0x03, 0xF0, 0xFF, 0xBF }  
           "1899/12/30"   = { 0x00, 0x00, 0x00, 0x78, 0xBF, 0x28, 0x06, 0xE0, 0xFE, 0x3F }  
           "1825/01/01"   = { 0x00, 0xf0, 0x7e, 0x51, 0x0c, 0xc0, 0xff, 0xd5, 0x0d, 0xc0 }  
           "1820/01/01"   = { 0x00, 0xf0, 0x7e, 0x51, 0x0c, 0xc0, 0x45, 0xe4, 0x0d, 0xc0 }  
           "1815/01/01"   = { 0x00, 0xf0, 0x7e, 0x51, 0x0c, 0xc0, 0x89, 0xf2, 0x0d, 0xc0 }  
           "1965/04/22"   = { 0x00, 0xF0, 0x7E, 0x51, 0x0C, 0xC0, 0x5D, 0xBA, 0x0D, 0x40 }  
           "1965/04/21"   = { 0x00, 0xF0, 0x7E, 0x51, 0x0C, 0xC0, 0x5B, 0xBA, 0x0D, 0x40 }  
           "1810/01/01"   = { 0x00, 0x78, 0xbf, 0x28, 0x06, 0xe0, 0x66, 0x80, 0x0e, 0xc0 }  
           "1805/01/01"   = { 0x00, 0x78, 0xbf, 0x28, 0x06, 0xe0, 0x88, 0x87, 0x0e, 0xc0 }  
           "1752/09/14"   = { 0x00, 0x78, 0xBF, 0x28, 0x06, 0xE0, 0x25, 0xD2, 0x0E, 0xC0 }  
           "1752/09/15"   = { 0x00, 0x78, 0xBF, 0x28, 0x06, 0xE0, 0x24, 0xD2, 0x0E, 0xC0 }  
           "1752/10/22"   = { 0x00, 0x78, 0xBF, 0x28, 0x06, 0xE0, 0xFF, 0xD1, 0x0E, 0xC0 }  
           "1752/10/21"   = { 0x00, 0x78, 0xBF, 0x28, 0x06, 0xE0, 0x00, 0xD2, 0x0E, 0xC0 }  
           "1752/10/20"   = { 0x00, 0x78, 0xBF, 0x28, 0x06, 0xE0, 0x01, 0xD2, 0x0E, 0xC0 }  
           "1752/10/19"   = { 0x00, 0x78, 0xBF, 0x28, 0x06, 0xE0, 0x02, 0xD2, 0x0E, 0xC0 }  
           "1753/10/19"   = { 0x00, 0x78, 0xBF, 0x28, 0x06, 0xE0, 0x95, 0xD0, 0x0E, 0xC0 }  
           "1800/01/01"   = { 0x00, 0x78, 0xBF, 0x28, 0x06, 0xE0, 0xAA, 0x8E, 0x0E, 0xC0 }  

edit: 14th september 1752 is the earliest date the program will allow me to put in, hence that is the earliest example I can give. Curiously 2nd - 14th septermber 1752 did not exist, apparently. I'm not sure why this is, or why the program cares.

Re: weird code (delphi?) puzzle - date as binary.
« Reply #1 on: 20 August, 2010, 07:27:59 pm »
Curiously 2nd - 14th septermber 1752 did not exist, apparently. I'm not sure why this is, or why the program cares.
That's when the Julian calendar was replaced by the Gregorian calendar in Britain, 11 days were "lost" and 2 September 1752 was followed by 14 September 1752.

Re: weird code (delphi?) puzzle - date as binary.
« Reply #2 on: 20 August, 2010, 10:43:36 pm »
Intriguing. Couple of thoughts:


1) If the first one 'is always 0x05', isn't  the second one also always 0x00?

- So does 0x05 0x00 mean "there follows a date"?


(And aren't these hex, not binary?)


2) .. and if the 21 and 22 of April 1965 start 0xF0, how come 23 April 1965 starts 0xC8 [which sounds quite a bit earlier?]

.. and looking closer at the difference between 21 and 22 April, how come the bit that changes is fourth in from the right? Which is also the case for a succession of dates in Oct 1752..  ie why is it incrementing in the fourth bit from the right, in hex?

3) Also, in April 1965 it increments one day by 2 bits: 5B to 5D; but in Oct 1752, it *decrements* the day by a *single* (fourth from right) bit: 02, 01, 00, FF

... can you get your own Delphi, feed it the 'binary' and get it to translate back to Julian yyyy/mm/dd?

... otherwise the key is to look at several series of days, and see if patterns emerge..

Good luck!

Re: weird code (delphi?) puzzle - date as binary.
« Reply #3 on: 21 August, 2010, 12:06:57 am »
Delphi represents dates and times as a double precision floating point number representing the number of days since 30 Dec 1899 (with the fractional part thus representing time of day).

What your program is doing is representing this number as an extended (80-bit) floatng point number, and the 10 bytes output are the 10 bytes that make up the extended float.

The following Delphi code (OK, I actually used FreePascal...):

Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
var
  e: extended;

const
  a: array [0..9] of byte =  ( $00, $00, $00, $78, $BF, $28, $06, $E0, $FE, $3F );

begin

  e := extended(a);
  Memo1.Lines.Add(DateTimeToStr(e));

end;   

gives the output:
Code: [Select]
30/12/1899 21:00:08

Which is the expected date for this line of data. I've tried a selection of the lines, and all give the right date. The time has been set to 8 seconds past nine for all the lines of data I tried; I guess this might make more sense to you than to me...

Euan Uzami

Re: weird code (delphi?) puzzle - date as binary.
« Reply #4 on: 21 August, 2010, 12:16:51 am »
bingo, thanks very much, that should do it.  :thumbsup: