Hi,
Is there any native function in FPC similar to getTime() of JavaScript?
Thx.
"... The getTime() method returns the number of milliseconds since midnight of January 1, 1970 and the specified date."

Mailing lists / lazarus / 2011-09
13 messages · 7 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi,
Is there any native function in FPC similar to getTime() of JavaScript?
Thx.
"... The getTime() method returns the number of milliseconds since midnight of January 1, 1970 and the specified date."
Yes:
MilliSecondsBetween(StrToDateTime('1970-01-01 08:00:00'), now);Thx.
2011/9/23 silvioprog :
First of all, Silvio, it's amazing how you can formulate questions and answers all by yourself ;-)
then, I think you should replace the 08:00:00 with a more suitable 00:00:00...
MilliSecondsBetween(StrToDateTime('1970-01-01 08:00:00'), now);
Cheers, A.
2011/9/23 Alberto Narduzzi :
First of all, Silvio, it's amazing how you can formulate questions and answers all by yourself ;-)
:D
then, I think you should replace the 08:00:00 with a more suitable 00:00:00...
Yes, I made:
uses
DateUtils;
var
Since19700101: TDateTime;
function getTime: Int64;
begin
Result := MilliSecondsBetween(Since19700101, Now);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Caption := IntToStr(getTime);
end;
initialization
Since19700101 := EncodeDate(1970, 1, 1);Thx.
Or...
2011/9/23 silvioprog :
[]
- var
- ? Since19700101: TDateTime;
+ const
+ CSince19700101 =
+ ((1970 { Year } - 1900) * 365.25) + ((1 { Month } - 1) * 30) + 1 { Day };
- ? Result := MilliSecondsBetween(Since19700101, Now);
+ Result := MilliSecondsBetween(CSince19700101, Now);
- initialization
- ? Since19700101 := EncodeDate(1970, 1, 1);What about
Result := Trunc(Now * MSecsPerDay);Zaher Dirkey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110925/9ed2cf5a/attachment.html>
2011/9/24 Zaher Dirkey :
What about Result := Trunc(Now * MSecsPerDay); Zaher Dirkey
Fail.
Test to 2011-09-24 21:13:**:
In correct method, result is: 1316941894378
Your method: 3526060294378
What about Result := Trunc(Now * MSecsPerDay); Zaher Dirkey -------------- next part…
2011/9/24 Zaher Dirkey :
Different from most languages, that base the date on 1899-12-30, javascript date is based on 1970-1-1.
-Fl?vio
Is there any native function in FPC similar to getTime() of JavaScript? Thx. "... The…
I personally suggest using "DateTimeToUnix" which is located in unit "dateutils". It converts a TDateTime to an Unix timestamp (which is the count of milliseconds since 1/1/1970). See also here: http://www.freepascal.org/docs-html/rtl/dateutils/datetimetounix.html
=== source begin ===
DateTimeToUnix(Now);=== source end ===
Regards,
Sven
2011/9/25 Sven Barth :
I know DateTimeToUnix, but:
getTime: 1317006621430
DateTimeToUnix: 1316963421
Hmm... it seems to only return the seconds since that date and according to Wikipedia ( http://en.wikipedia.org/wiki/Unix_time ) this is correct. So it would not be completely incorrect to multiply the returned time by 1000.
Regarding the offset:
For me the command "date +%s" returns
1316975223
while a program writing the result of "DateTimeToUnix(Now)" returns
1316982419
The difference between the two is
7196
In minutes:
~119
Which means around 2 hours. This matches my timezone. So one of the two does respect the timezone while the other does not. I don't know which is the correct solution though; perhaps this is mentioned in the Wikipedia article...
Regards,
Sven
Is there any native function in FPC similar to getTime() of JavaScript? Thx. "... The…
silvioprog wrote"... The getTime() method returns the number of milliseconds since midnight of January 1, 1970 and the specified date."
As the TDateTime type is a real incrementing in +1 for a day, it's easy:
var
BaseDate: TDateTime, MyDate;
MyMsecs: Int64BaseDate = (do a decent specification of Jan 1, 1970, 0:0:0:0)
...
MyDate := Now - BaseDate;
MyMsec := MyDate * (24*60*60*1000);-Michael
Hmm... it seems to only return the seconds since that date and according to Wikipedia (…
2011/9/25 Sven Barth :
SysUtils.Now is local not UTC. DateTimeToUnix cannot distinguish if passed parameter is UTC or local. There would need to be NowToUnix() which takes Now in UTC and converts to Unix Time.
$ date +"%z %Z"
+0100 CET
$ LC_ALL=C; date -s"1970-01-01 00:00:00 +0000" -R; echo -n "Date: "; date +%s; /tmp/project1
Thu, 01 Jan 1970 01:00:00 +0100
Date: 0
DateTimeToUnix(Now): 3600