4

In a Delphi XE application, I am reading values from a database originally created by a C++ program. There is a date column, stored (it would appear) as time_t, i.e. Unix time, the number of seconds since 00:00, Jan 1, 1970 UTC. I can deal with the time zone, but how can I get TDateTime from (long) time_t?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Marek Jedliński
  • 7,088
  • 11
  • 47
  • 57

2 Answers2

12

Try using the unixtodatetime function which is part of the Dateutils unit : http://docwiki.embarcadero.com/VCL/en/DateUtils.UnixToDateTime

RRUZ
  • 134,889
  • 20
  • 356
  • 483
3

If you want to do it yourself for some reason, the obvious approach would be to divide the time_t by the number of seconds in a day, then add that to a TDateTime for 00:00, Jan 1, 1970.

(But RRUZ answer should work and means you don't have to reinvent the wheel).

Thorsten Engler
  • 2,333
  • 12
  • 13
  • 1
    You'd need this if you're using a version of Delphi that doesn't include the DateUtils unit. The algorithm you describe is exactly what UnixToDateTime does; the TDateTime value you refer to is 25569. – Rob Kennedy Feb 16 '11 at 04:59
  • 1
    Thanks for mentioning that constant. I didn't look at the code of UnixToDateTime and was a bit too lazy to figure out the right TDataTime value. But a quick call once to EncodeDate should have provided the questioner with that value :) – Thorsten Engler Feb 16 '11 at 05:12