需要帮助将Delphi时间转换为.Net时间

时间:2021-10-30 22:35:26

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应用程序移植到C#,我遇到了一个问题。 Delphi应用程序将时间记录到日志文件中,然后将其读回程序。但它记录的时间格式让我困惑。我找不到.Net库来正确转换它。

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

Delphi在日志文件中记录时间:976129709(在Delphi代码中将其转换为1/14/2009 5:53:26 PM)

//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?

任何人都有任何想法,我怎么能在.Net中读到这个?

3 个解决方案

#1


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:

Delphi的TSearchRec.Time格式是旧的DOS 32位日期/时间值。据我所知,它没有内置转换器,所以你必须写一个。例如:

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);
}

#2


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.

以下是不同日期/时间格式的描述(Delphi的本机TDateTime是OLE自动化日期格式)。根据这个,你需要System.DateTime.FromFileTime()和System.DateTime.ToFileTime()+ DosDateTimeToFileTime()和FileTimeToDosDateTime()函数。实际上,这是两步转换。

#3


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

我尝试了谷歌搜索和实验,从虚拟代码开始,找到类似于unix时代的东西。

  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! ;)

而对于Flying Spaghetti Monster的爱,放弃你的12小时时间格式! ;)

#1


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:

Delphi的TSearchRec.Time格式是旧的DOS 32位日期/时间值。据我所知,它没有内置转换器,所以你必须写一个。例如:

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);
}

#2


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.

以下是不同日期/时间格式的描述(Delphi的本机TDateTime是OLE自动化日期格式)。根据这个,你需要System.DateTime.FromFileTime()和System.DateTime.ToFileTime()+ DosDateTimeToFileTime()和FileTimeToDosDateTime()函数。实际上,这是两步转换。

#3


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

我尝试了谷歌搜索和实验,从虚拟代码开始,找到类似于unix时代的东西。

  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! ;)

而对于Flying Spaghetti Monster的爱,放弃你的12小时时间格式! ;)