Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to convert UNIX time to TDateTime and vice versa Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
18-Oct-02
Category
Algorithm
Language
Delphi 2.x
Views
102
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

There is a date/ time format that I'm trying to translate, but I can't find 
anything that could match. This example is 2000-12-20 around 22:15. Integer: 
977347109, Hex: 3A412225. Anyone know how to translate it?

Answer:

The value is a Unix Time, defined as seconds since 1970-01-01T00:00:00,0Z. 
Important is the Letter Z, you live in Sweden, in consequence you must add 1 hour 
for StandardDate and 2 hours for DaylightDate to the date. The infos you can get 
with GetTimeZoneInformation. But you must determine, which Bias (Standard or 
Daylight) is valid for the date (in this case -60). You can convert the date value 
with the function below.

The Date for 977347109 is 2000-12-20T22:18:29+01:00.

1   const
2     UnixDateDelta = 25569; { 1970-01-01T00:00:00,0 }
3     SecPerMin = 60;
4     SecPerHour = SecPerMin * 60;
5     SecPerDay = SecPerHour * 24;
6     MinDayFraction = 1 / (24 * 60);
7   
8     {Convert Unix time to TDatetime}
9   
10  function UnixTimeToDateTime(AUnixTime: DWord; ABias: Integer): TDateTime;
11  begin
12    Result := UnixDateDelta + (AUnixTime div SecPerDay) { Days }
13    + ((AUnixTime mod SecPerDay) / SecPerDay) { Seconds }
14    - ABias * MinDayFraction { Bias to UTC in minutes };
15  end;
16  
17  {Convert Unix time to String with locale settings}
18  
19  function UnixTimeToStr(AUnixTime: DWord; ABias: Integer): string;
20  begin
21    Result := FormatDateTime('ddddd  hh:nn:ss', UnixTimeToDateTime(AUnixTime, ABias));
22  end;
23  
24  {Convert TDateTime to Unix time}
25  
26  function DateTimeToUnixTime(ADateTime: TDateTime; ABias: Integer): DWord;
27  begin
28    Result := Trunc((ADateTime - UnixDateDelta) * SecPerDay) + ABias * SecPerMin;
29  end;
30  
31  procedure TForm1.Button4Click(Sender: TObject);
32  begin
33    Label1.Caption := UnixTimeToStr(977347109, -60);
34  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC