views:

677

answers:

3

I am porting a Delphi application to C# and I've run into a problem. The Delphi app records the time into a log file, which then gets read back into the program. But the format of the time it records confuses me. I can find no .Net library to convert it properly.

Delphi recorded time in log file: 976129709 (this gets converted to 1/14/2009 5:53:26 PM in the Delphi code)

//Here is the Delphi code which records it: 
IntToStr(DirInfo.Time);

//Here is the Delphi code which reads it back in:
DateTimeToStr(FileDateToDateTime(StrToInt(stringTime));

Anybody have any ideas how I can read this in .Net?

A: 

I've tried both googling and experimenting, starting with dummy code to find something similar to the unix epoch.

  var x = 976129709;
  var target = new DateTime(2009, 1, 14, 17, 53, 26);
  var testTicks = target.AddTicks(-x);     // 2009-01-14 17:51:48
  var testMs = target.AddMilliseconds(-x); // 2009-01-03 10:44:36
  var testS = target.AddSeconds(-x);       // 1978-02-08 22:44:57
  // No need to check for bigger time units
  // since your input indicates second precision.

Could you verify that your input is correct?

And for the love of the Flying Spaghetti Monster, abandon your 12 hour time format! ;)

Simon Svensson
Thanks for the assistance. Will do. I still have to make work retroactively however.
JimDaniel
+9  A: 

Delphi's TSearchRec.Time format is the old DOS 32-bit date/time value. As far as I know there's no built-in converter for it, so you'll have to write one. For example:

public static DateTime DosDateToDateTime(int DosDate)
{
     Int16 Hi = (Int16)((DosDate & 0xFFFF0000) >> 16);
     Int16 Lo = (Int16)(DosDate & 0x0000FFFF);

     return new DateTime(((Hi & 0x3F00) >> 9) + 1980, (Hi & 0xE0) >> 5, Hi & 0x1F,
        (Lo & 0xF800) >> 11, (Lo & 0x7E0) >> 5, (Lo & 0x1F) * 2);
}
Jon Benedicto
Thanks for this. I will try it when I get a chance. Sounds like this might be the solution.
JimDaniel
I did a quick test in a console app, and this seems to be the correct answer.
Simon Svensson
The format of the dos filetime is described at http://msdn.microsoft.com/en-us/library/ms724274(VS.85).aspx
VolkerK
+1  A: 

Here is description of different date/time formats (Delphi's native TDateTime is OLE Automation date format). According to this, you need System.DateTime.FromFileTime() and System.DateTime.ToFileTime() + DosDateTimeToFileTime() and FileTimeToDosDateTime() functions. Actually, this is conversion in two-steps.

Alexander