?
Solved

Converting UNIX time to TDateTime/String

Posted on 1999-11-11
5
Medium Priority
?
1,220 Views
Last Modified: 2008-02-20
Greetings,

I need a function to convert UNIX time to the equivalent TDateTime or string.

Thanks,

Robert
0
Comment
Question by:RLancaster
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:Lischke
ID: 2199623
Hi Robert,

I assume you already have the UNIX time somehow retrieved and need to convert it now. In which format does the UNIX time appear?

Ciao, Mike
0
 
LVL 1

Author Comment

by:RLancaster
ID: 2199781
It is an signed long indiciating the number of seconds since 01/01/1970 at 00:00h.

I need to convert this to TDateTime.
0
 
LVL 10

Accepted Solution

by:
Lischke earned 400 total points
ID: 2199851
A yes, I rember. Well, here's the solution:

function UnixTimeToDateTime(const UnixTime: Integer): TDateTime;

var
  FileTime: TFileTime;
  SystemTime: TSystemTime;
  I: Int64;
 
begin
  // first convert unix time to a Win32 file time
  I := Int64(UnixTime) * Int64(10000000) + 116444736000000000;
  FileTime.dwLowDateTime := DWORD(I);
  FileTime.dwHighDateTime := I shr 32;

  // now convert to system time
  FileTimeToSystemTime(FileTime, SystemTime);

  // and finally convert the system time to TDateTime
  Result := SystemTimeToDateTime(SystemTime);
end;


Ciao, Mike
0
 
LVL 1

Author Comment

by:RLancaster
ID: 2199872
An extra 50 points bonus for a prompt and most interestingly coded responses I have ever seen !!!

Well done !!!
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2199922
Thank you :-) And the solution is very easy to understand. Unix time is the seconds from 1/1/1970 as you already wrote. Win32 file time is 100 nanoseconds from 1/1/1601. So multiplication by 10000000 converts the seconds into 100 nanoseconds units and 116444736000000000 is a constant offset (also in 100 ns units) from 1601 to 1970.

The rest is just a bit time format conversion.

Ciao, Mike
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question