Reputation: 12703
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?
Upvotes: 3
Views: 4328
Reputation: 4247
I'm late to the game, but I've been using this for several years, and it works great. It converts the Delphi time to Windows ticks, which get converted to a DateTime.
public static DateTime DelphiTimeToDateTime(this double delphiTime)
{
// scale it to 100nsec ticks. Yes, we could do this as one expression
// but this makes it a lot easier to read.
// delphitime *= 864000000000L
delphiTime *= 24; // hours since Delphi epoch
delphiTime *= 60; // minutes since epoch
delphiTime *= 60; // seconds since epoch
delphiTime *= 1000; // milliseconds since epoch
delphiTime *= 1000; // microseconds since epoch
delphiTime *= 10; // 100 nsec ticks since epoch
// Now, delphiTime is the number of ticks since 1899/12/30
long time = 599264352000000000L; // 1/1/0001 --> 1899/12/30
time += (long)delphiTime;
// These are all *local* times, sadly
return new DateTime(time, DateTimeKind.Local);
}
Upvotes: 0
Reputation: 5668
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.
Upvotes: 3
Reputation: 10582
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);
}
Upvotes: 13
Reputation: 19781
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! ;)
Upvotes: 0