0

I am trying to use the DateTimeToUnix function in Delphi 5. DateTimeToUnix uses DateUtils (Which wasn't introduced until Delphi 6 according to some information found on the web); following this logic I ascertain that Delphi 5 does not support this function.

A question similar to this was asked on stackoverflow and marked as a duplicate, however; there was nothing mentioned about Delphi 5 in the answers.

Original Question: https://stackoverflow.com/questions/27885090/delphi-5-how-to-get-current-time-now-in-unix-timestamp?lq=1

I believe The code found at the following website answers mine and the original question: http://www.swissdelphicenter.com/torry/showcode.php?id=844

unit Unix_Utils;

interface

Uses SysUtils;

function DateTimeToUnix(ConvDate: TDateTime): Longint;
function UnixToDateTime(USec: Longint): TDateTime;

implementation

const UnixStartDate: TDateTime = 25569.0; // Sets UnixStartDate to TDateTime of 01/01/1970

    function DateTimeToUnix(ConvDate: TDateTime): Longint;
    begin
        //example: DateTimeToUnix(now);
        Result := Round((ConvDate - UnixStartDate) * 86400);
    end;

    function UnixToDateTime(USec: Longint): TDateTime;
    begin
        //Example: UnixToDateTime(1003187418);
        Result := (Usec / 86400) + UnixStartDate;
    end;

end.

Thomas Greiner is the author of the original code. "The above code has been modified from its original version. It has been formatted to fit the screen"

Steps to use the above code in Delphi 5:

  1. File > New... > New Tab > Unit > OK
  2. Replace the entire contents of the new unit with the code shown above
  3. Save the new unit "unix_utils" somewhere like C:\Program Files\Borland\Delphi5\Lib\
  4. If DateTimeToUnix is ever needed: Project > Add to Project... > browse to unix_utils > Then click open
  5. I believe if unix_utils file is saved in the aforementioned location it can just be added to the "Uses" section of a unit???

So is this a duplicate?

Community
  • 1
  • 1
Mark Davich
  • 512
  • 1
  • 5
  • 16
  • 1
    You have to either add the .pas file to every project that needs it, or else compile the .pas file into a .dcu and place that in a folder on your IDE's library path. Either way, you can then use the unit in your `uses` clause. – Remy Lebeau Feb 11 '15 at 21:33
  • Thanks Remy... ".dcu" will do. – Mark Davich Feb 11 '15 at 22:56
  • 1
    Don't save it under Delphi5\Lib. Just add the unit to any project that needs it. Or, put it in a shared location and add that location to your search path. But whatever you do, don't modify Delphi's lib folder. – David Heffernan Feb 12 '15 at 10:32
  • 1
    It is not clear what you are asking about. Task is pretty trivial - compute number of seconds since 1 Jan 1970 given number of days since 31 Dec 1899. But you have to remember what TDateTime is local, with Epoch Time is UTC. – Free Consulting Feb 12 '15 at 13:54
  • 1
    Yes I would consider this a duplicate. And instead of providing an answer inside the question, I suggest you go to the older question [here](http://stackoverflow.com/q/23968919/224704) and add your Delphi 5 answer there. – Disillusioned Feb 12 '15 at 16:00

0 Answers0