Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-09

Alternative to getTime() of JavaScript

13 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

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."

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

silvioprog
to silvioprog

Yes:

MilliSecondsBetween(StrToDateTime('1970-01-01 08:00:00'), now);

Thx.

2011/9/23 silvioprog :

Show quoted text 8 lines
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."
Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

Alberto Narduzzi
to 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.

Reply by email

silvioprog
to Alberto Narduzzi

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);
Show quoted text 5 lines
> MilliSecondsBetween(StrToDateTime('1970-01-01 08:00:00'), now); Cheers, A.

Thx.

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

silvioprog
to silvioprog

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);
Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

Zaher Dirkey
to silvioprog
silvioprog wrote 30 lines
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); -- Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== * Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================
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>

Reply by email

silvioprog
to Zaher Dirkey

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

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

Flávio Etrusco
to Zaher Dirkey: What about Result := Trunc(Now * MSecsPerDay); Zaher Dirkey -------------- next part…

2011/9/24 Zaher Dirkey :

Show quoted text 39 lines
On Sat, Sep 24, 2011 at 2:30 AM, silvioprog wrote: > > 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); > > -- > Silvio Cl?cio > =============================================== > Blog - <silvioprog.com.br> > Twitter - <twitter.com/silvioprog> > Facebook - <facebook.com/silvioprog> > LazSolutions - <code.google.com/p/lazsolutions> > LazWebSolutions - <code.google.com/p/lazwebsolutions> > Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> > =============================================== > ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * > =============================================== What about Result := Trunc(Now * MSecsPerDay); Zaher Dirkey --

Different from most languages, that base the date on 1899-12-30, javascript date is based on 1970-1-1.

-Fl?vio

Reply by email

Sven Barth
to silvioprog: Is there any native function in FPC similar to getTime() of JavaScript? Thx. "... The…
silvioprog wrote 9 lines
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."

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

Reply by email

silvioprog
to Sven Barth

2011/9/25 Sven Barth :

Show quoted text 8 lines
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 Regards, Sven

I know DateTimeToUnix, but:

getTime: 1317006621430

DateTimeToUnix: 1316963421

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

Sven Barth
to silvioprog
silvioprog wrote 16 lines
2011/9/25 Sven Barth: > > 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 > > Regards, > Sven 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

Reply by email

Michael Schnell
to silvioprog: 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: Int64

BaseDate = (do a decent specification of Jan 1, 1970, 0:0:0:0)

...

MyDate := Now - BaseDate;
MyMsec := MyDate * (24*60*60*1000);

-Michael

Reply by email

cobines
to Sven Barth: Hmm... it seems to only return the seconds since that date and according to Wikipedia (…

2011/9/25 Sven Barth :

Show quoted text 22 lines
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...

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

cobines

Reply by email