Jump to content

Wrong JDateHelper.SetAsDateTime conversion


Recommended Posts

I suspect this method is incorrect:

 

procedure JDateHelper.SetAsDateTime(dt : TDateTime);
begin
  Self.setTime((dt - 25569) * 864e5);
end;
 
It should be written:
 
procedure JDateHelper.SetAsDateTime(dt : TDateTime);
begin
  Self.setTime(round((dt - 25569) * 864e5));
end;
 
Otherwise the returned value is incorrect.

 

Link to post
Share on other sites

var
  foo: TDateTime;
  bar: TDateTime;
  year, month, day, hour, minute, second, milli: integer;
  timestamp: TDateTime;
  x : Integer;
 
function PosixToDateTime(posix: TDateTime): TDateTime;
begin
  Result := (posix / 86400) + 25569;
end;
 
function DateTimeToPosix(dt: TDateTime): TDateTime;
begin
  Result := Round((dt - 25569) * 86400);
  // Result := ((dt - 25569) * 864e5); //incompatible types Integer and Extended in Delphi
end;
 
procedure TForm1.W3Button5Click(Sender: TObject);
begin
    foo := PosixToDateTime(587675785);  // this is 15. August 1988, 19:16:25
  bar := now();                        // this is the current time
 
  writeln('foo was converted from some POSIX timestamp ');
  writeln('foo: '+DateTimeToStr(foo));  // this will print 15-8-88 19:16:25
  writeln('bar: '+DateTimeToStr(bar));  // this will print the current date and time
 
  // compare times
  if foo < bar then
  begin
    writeln('foo was before bar');
  end;
 
  // format it to a string (see the documentation of FormatDateTime)
  writeln(FormatDateTime('yyyy.mm.dd hh:mm:ss', bar));
 
  // day of week
  writeln('it is day '+ DayOfWeek(bar).ToString+ ' of the week');
 
  // decode
  DecodeDate(bar, year, month, day);
  writeln('DecodeDate: '+ year.ToString+ '-'+ month.ToString+ '-'+ day.ToString);
 
  DecodeTime(bar, hour, minute, second, milli);
  writeln('DecodeTime: '+ hour.ToString+ '-'+ minute.ToString+ '-'+ second.ToString+ '-'+ milli.ToString);
 
  // encode
  foo := EncodeDate(2012, 12, 21);
  writeln('encoded and formatted '+ FormatDateTime('dddd, dd. mmm yyyy', foo));
 
  // timestamps
  timestamp := DateTimeToPosix(foo);
  writeln('converted to POSIX timestamp '+ timestamp.ToString);
  foo := PosixToDateTime(timestamp);
  writeln('and back: '+ FormatDateTime('dddd, dd. mmm yyyy', foo));
end;

 


//---> Round((dt - 25569) * 86400); <---

foo was converted from some POSIX timestamp 

foo: 1988-08-15 19:16:25

bar: 2015-07-05 18:36:12

foo was before bar

2015.07.05 18:07:12

it is day 1 of the week

DecodeDate: 2015-7-5

DecodeTime: 18-36-12-773

encoded and formatted friday, 21. dec 2012

converted to POSIX timestamp 1356048000

and back: friday, 21. dec 2012  

-----------------------------------------

 

//---> Result := ((dt - 25569) * 864e5); <--- 

foo was converted from some POSIX timestamp 

foo: 1988-08-15 19:16:25

bar: 2015-07-05 18:41:50

foo was before bar

2015.07.05 18:07:50

it is day 1 of the week

DecodeDate: 2015-7-5

DecodeTime: 18-41-50-770

encoded and formatted friday, 21. dec 2012

converted to POSIX timestamp 1356048000000

and back: wednesday, 14. jun 44941

---------------------------------------------

Link to post
Share on other sites

Exactly, JavaScript works with the number of milliseconds since the epoch (1970/01/01) whereas most other languages work with the seconds. I just copy PosixToDateTime/DateTimeToPosix functions from free pascal that uses unix timestamp (in seconds).

 

Smart should use (not Unix timestamp -> in miliseconds)

1000*60*24*24  = 86400000 = 864 * 10^5 = 1 day

 

In this example, I'm using Unix timestamp (in seconds).

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...