Wrong result calling Android method from Delphi
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to call Android's class GeomagneticField
method getDeclination()
from my Delphi 10.2.3 app.
First I imported the class GeomagneticField using Java2OP:
unit Androidapi.JNI.Interfaces.JGeomagneticField;
interface
uses
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes;
type
// ===== Forward declarations =====
JGeomagneticField = interface;//android.hardware.GeomagneticField
// ===== Interface declarations =====
JGeomagneticFieldClass = interface(JObjectClass)
['{77F2155B-1F9A-40E0-89FA-FE3422336577}']
{class} function init(gdLatitudeDeg: Single; gdLongitudeDeg: Single; altitudeMeters: Single; timeMillis: Int64): JGeomagneticField; cdecl;
{class} function getHorizontalStrength: Single; cdecl;//Deprecated
{class} function getInclination: Single; cdecl;//Deprecated
{class} function getX: Single; cdecl;//Deprecated
end;
[JavaSignature('android/hardware/GeomagneticField')]
JGeomagneticField = interface(JObject)
['{47CF41EC-AAAB-4EE2-867A-884A3EF00407}']
function getDeclination: Single; cdecl;//Deprecated
function getFieldStrength: Single; cdecl;//Deprecated
function getY: Single; cdecl;
function getZ: Single; cdecl;
end;
TJGeomagneticField = class(TJavaGenericImport<JGeomagneticFieldClass, JGeomagneticField>) end;
implementation
initialization
end.
Then I called the method this way:
procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; t0,t:TDatetime;
begin
t0 := EncodeDatetime(1970,1,1,0,0,0,0);
t := now;
tm := Trunc( (t-t0)*24*3600*1000); // <-- ms since 1/1/1970
GeoField := TJGeomagneticField.JavaClass.init(-23,-46,750,tm );
Label1.Text := FloatToStr( GeoField.getDeclination );
end;
When I run this and click, Label1 shows -174.3635711, which is wrong
On Android Studio I built a similar app in Java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShowDecl);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GeomagneticField geoField = new GeomagneticField(
(float) -23,
(float) -46,
(float) 750,
System.currentTimeMillis()
);
float f = geoField.getDeclination();
String s = Float.toString(f);
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG)
.show();
}
});
}
When I run this and click the button, I got -21.839144, which is correct.
Why is my Delphi app getting this wrong result?
android delphi
|
show 4 more comments
I'm trying to call Android's class GeomagneticField
method getDeclination()
from my Delphi 10.2.3 app.
First I imported the class GeomagneticField using Java2OP:
unit Androidapi.JNI.Interfaces.JGeomagneticField;
interface
uses
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes;
type
// ===== Forward declarations =====
JGeomagneticField = interface;//android.hardware.GeomagneticField
// ===== Interface declarations =====
JGeomagneticFieldClass = interface(JObjectClass)
['{77F2155B-1F9A-40E0-89FA-FE3422336577}']
{class} function init(gdLatitudeDeg: Single; gdLongitudeDeg: Single; altitudeMeters: Single; timeMillis: Int64): JGeomagneticField; cdecl;
{class} function getHorizontalStrength: Single; cdecl;//Deprecated
{class} function getInclination: Single; cdecl;//Deprecated
{class} function getX: Single; cdecl;//Deprecated
end;
[JavaSignature('android/hardware/GeomagneticField')]
JGeomagneticField = interface(JObject)
['{47CF41EC-AAAB-4EE2-867A-884A3EF00407}']
function getDeclination: Single; cdecl;//Deprecated
function getFieldStrength: Single; cdecl;//Deprecated
function getY: Single; cdecl;
function getZ: Single; cdecl;
end;
TJGeomagneticField = class(TJavaGenericImport<JGeomagneticFieldClass, JGeomagneticField>) end;
implementation
initialization
end.
Then I called the method this way:
procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; t0,t:TDatetime;
begin
t0 := EncodeDatetime(1970,1,1,0,0,0,0);
t := now;
tm := Trunc( (t-t0)*24*3600*1000); // <-- ms since 1/1/1970
GeoField := TJGeomagneticField.JavaClass.init(-23,-46,750,tm );
Label1.Text := FloatToStr( GeoField.getDeclination );
end;
When I run this and click, Label1 shows -174.3635711, which is wrong
On Android Studio I built a similar app in Java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShowDecl);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GeomagneticField geoField = new GeomagneticField(
(float) -23,
(float) -46,
(float) 750,
System.currentTimeMillis()
);
float f = geoField.getDeclination();
String s = Float.toString(f);
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG)
.show();
}
});
}
When I run this and click the button, I got -21.839144, which is correct.
Why is my Delphi app getting this wrong result?
android delphi
1
Is your Delphi code giving you the same value for "tm" asSystem.currentTimeMillis()
?
– Barns
Nov 16 '18 at 18:07
I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly
– Omar Reis
Nov 16 '18 at 18:28
1
This is a great question and deserves more upvotes.
– J...
Nov 16 '18 at 19:07
1
System.currentTimeMillis()
is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi'sNow()
returns aTDateTime
expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things likeTrunc( (t-t0) ...)
forTDateTime
, that performs floating-point arithmetic that can skew the results. UseSystem.DateUtils.MilliSecondsBetween()
instead, which usesTTimeStamp
internally for more accurate date/time calculations.
– Remy Lebeau
Nov 17 '18 at 1:02
1
Note, Delphi's RTL has aSystem.DateUtils.DateTimeToUnix()
function to convert aTDateTime
to a Unix timestamp, but it has seconds precision (it usesSecondsBetween()
internally), but it would be very easy to copy it and adjust it to use milliseconds instead.
– Remy Lebeau
Nov 17 '18 at 1:09
|
show 4 more comments
I'm trying to call Android's class GeomagneticField
method getDeclination()
from my Delphi 10.2.3 app.
First I imported the class GeomagneticField using Java2OP:
unit Androidapi.JNI.Interfaces.JGeomagneticField;
interface
uses
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes;
type
// ===== Forward declarations =====
JGeomagneticField = interface;//android.hardware.GeomagneticField
// ===== Interface declarations =====
JGeomagneticFieldClass = interface(JObjectClass)
['{77F2155B-1F9A-40E0-89FA-FE3422336577}']
{class} function init(gdLatitudeDeg: Single; gdLongitudeDeg: Single; altitudeMeters: Single; timeMillis: Int64): JGeomagneticField; cdecl;
{class} function getHorizontalStrength: Single; cdecl;//Deprecated
{class} function getInclination: Single; cdecl;//Deprecated
{class} function getX: Single; cdecl;//Deprecated
end;
[JavaSignature('android/hardware/GeomagneticField')]
JGeomagneticField = interface(JObject)
['{47CF41EC-AAAB-4EE2-867A-884A3EF00407}']
function getDeclination: Single; cdecl;//Deprecated
function getFieldStrength: Single; cdecl;//Deprecated
function getY: Single; cdecl;
function getZ: Single; cdecl;
end;
TJGeomagneticField = class(TJavaGenericImport<JGeomagneticFieldClass, JGeomagneticField>) end;
implementation
initialization
end.
Then I called the method this way:
procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; t0,t:TDatetime;
begin
t0 := EncodeDatetime(1970,1,1,0,0,0,0);
t := now;
tm := Trunc( (t-t0)*24*3600*1000); // <-- ms since 1/1/1970
GeoField := TJGeomagneticField.JavaClass.init(-23,-46,750,tm );
Label1.Text := FloatToStr( GeoField.getDeclination );
end;
When I run this and click, Label1 shows -174.3635711, which is wrong
On Android Studio I built a similar app in Java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShowDecl);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GeomagneticField geoField = new GeomagneticField(
(float) -23,
(float) -46,
(float) 750,
System.currentTimeMillis()
);
float f = geoField.getDeclination();
String s = Float.toString(f);
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG)
.show();
}
});
}
When I run this and click the button, I got -21.839144, which is correct.
Why is my Delphi app getting this wrong result?
android delphi
I'm trying to call Android's class GeomagneticField
method getDeclination()
from my Delphi 10.2.3 app.
First I imported the class GeomagneticField using Java2OP:
unit Androidapi.JNI.Interfaces.JGeomagneticField;
interface
uses
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes;
type
// ===== Forward declarations =====
JGeomagneticField = interface;//android.hardware.GeomagneticField
// ===== Interface declarations =====
JGeomagneticFieldClass = interface(JObjectClass)
['{77F2155B-1F9A-40E0-89FA-FE3422336577}']
{class} function init(gdLatitudeDeg: Single; gdLongitudeDeg: Single; altitudeMeters: Single; timeMillis: Int64): JGeomagneticField; cdecl;
{class} function getHorizontalStrength: Single; cdecl;//Deprecated
{class} function getInclination: Single; cdecl;//Deprecated
{class} function getX: Single; cdecl;//Deprecated
end;
[JavaSignature('android/hardware/GeomagneticField')]
JGeomagneticField = interface(JObject)
['{47CF41EC-AAAB-4EE2-867A-884A3EF00407}']
function getDeclination: Single; cdecl;//Deprecated
function getFieldStrength: Single; cdecl;//Deprecated
function getY: Single; cdecl;
function getZ: Single; cdecl;
end;
TJGeomagneticField = class(TJavaGenericImport<JGeomagneticFieldClass, JGeomagneticField>) end;
implementation
initialization
end.
Then I called the method this way:
procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; t0,t:TDatetime;
begin
t0 := EncodeDatetime(1970,1,1,0,0,0,0);
t := now;
tm := Trunc( (t-t0)*24*3600*1000); // <-- ms since 1/1/1970
GeoField := TJGeomagneticField.JavaClass.init(-23,-46,750,tm );
Label1.Text := FloatToStr( GeoField.getDeclination );
end;
When I run this and click, Label1 shows -174.3635711, which is wrong
On Android Studio I built a similar app in Java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShowDecl);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GeomagneticField geoField = new GeomagneticField(
(float) -23,
(float) -46,
(float) 750,
System.currentTimeMillis()
);
float f = geoField.getDeclination();
String s = Float.toString(f);
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG)
.show();
}
});
}
When I run this and click the button, I got -21.839144, which is correct.
Why is my Delphi app getting this wrong result?
android delphi
android delphi
edited Nov 16 '18 at 18:34
J...
24.5k446108
24.5k446108
asked Nov 16 '18 at 17:02
Omar ReisOmar Reis
137110
137110
1
Is your Delphi code giving you the same value for "tm" asSystem.currentTimeMillis()
?
– Barns
Nov 16 '18 at 18:07
I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly
– Omar Reis
Nov 16 '18 at 18:28
1
This is a great question and deserves more upvotes.
– J...
Nov 16 '18 at 19:07
1
System.currentTimeMillis()
is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi'sNow()
returns aTDateTime
expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things likeTrunc( (t-t0) ...)
forTDateTime
, that performs floating-point arithmetic that can skew the results. UseSystem.DateUtils.MilliSecondsBetween()
instead, which usesTTimeStamp
internally for more accurate date/time calculations.
– Remy Lebeau
Nov 17 '18 at 1:02
1
Note, Delphi's RTL has aSystem.DateUtils.DateTimeToUnix()
function to convert aTDateTime
to a Unix timestamp, but it has seconds precision (it usesSecondsBetween()
internally), but it would be very easy to copy it and adjust it to use milliseconds instead.
– Remy Lebeau
Nov 17 '18 at 1:09
|
show 4 more comments
1
Is your Delphi code giving you the same value for "tm" asSystem.currentTimeMillis()
?
– Barns
Nov 16 '18 at 18:07
I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly
– Omar Reis
Nov 16 '18 at 18:28
1
This is a great question and deserves more upvotes.
– J...
Nov 16 '18 at 19:07
1
System.currentTimeMillis()
is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi'sNow()
returns aTDateTime
expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things likeTrunc( (t-t0) ...)
forTDateTime
, that performs floating-point arithmetic that can skew the results. UseSystem.DateUtils.MilliSecondsBetween()
instead, which usesTTimeStamp
internally for more accurate date/time calculations.
– Remy Lebeau
Nov 17 '18 at 1:02
1
Note, Delphi's RTL has aSystem.DateUtils.DateTimeToUnix()
function to convert aTDateTime
to a Unix timestamp, but it has seconds precision (it usesSecondsBetween()
internally), but it would be very easy to copy it and adjust it to use milliseconds instead.
– Remy Lebeau
Nov 17 '18 at 1:09
1
1
Is your Delphi code giving you the same value for "tm" as
System.currentTimeMillis()
?– Barns
Nov 16 '18 at 18:07
Is your Delphi code giving you the same value for "tm" as
System.currentTimeMillis()
?– Barns
Nov 16 '18 at 18:07
I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly
– Omar Reis
Nov 16 '18 at 18:28
I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly
– Omar Reis
Nov 16 '18 at 18:28
1
1
This is a great question and deserves more upvotes.
– J...
Nov 16 '18 at 19:07
This is a great question and deserves more upvotes.
– J...
Nov 16 '18 at 19:07
1
1
System.currentTimeMillis()
is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi's Now()
returns a TDateTime
expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things like Trunc( (t-t0) ...)
for TDateTime
, that performs floating-point arithmetic that can skew the results. Use System.DateUtils.MilliSecondsBetween()
instead, which uses TTimeStamp
internally for more accurate date/time calculations.– Remy Lebeau
Nov 17 '18 at 1:02
System.currentTimeMillis()
is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi's Now()
returns a TDateTime
expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things like Trunc( (t-t0) ...)
for TDateTime
, that performs floating-point arithmetic that can skew the results. Use System.DateUtils.MilliSecondsBetween()
instead, which uses TTimeStamp
internally for more accurate date/time calculations.– Remy Lebeau
Nov 17 '18 at 1:02
1
1
Note, Delphi's RTL has a
System.DateUtils.DateTimeToUnix()
function to convert a TDateTime
to a Unix timestamp, but it has seconds precision (it uses SecondsBetween()
internally), but it would be very easy to copy it and adjust it to use milliseconds instead.– Remy Lebeau
Nov 17 '18 at 1:09
Note, Delphi's RTL has a
System.DateUtils.DateTimeToUnix()
function to convert a TDateTime
to a Unix timestamp, but it has seconds precision (it uses SecondsBetween()
internally), but it would be very easy to copy it and adjust it to use milliseconds instead.– Remy Lebeau
Nov 17 '18 at 1:09
|
show 4 more comments
1 Answer
1
active
oldest
votes
I narrowed the problem to the "timeMillis" parameter.
It is a Java long, which corresponds to Delphi's int64.
I suspected of some kind of endian problem, so I
tried to switch the long parameter high and lo dwords.
And it worked !
function switchDWords(n:int64):int64; // switch hi <--> lo dwords of an int64
var i: Integer;
nn :int64; nnA:array[0..7] of byte absolute nn;
nn1:int64; nn1A:array[0..7] of byte absolute nn1;
begin
nn1 := n; // copy n
for i := 0 to 3 do // switch bytes hidword <--> lodword
begin
nnA[i] := nn1A[i+4];
nnA[i+4] := nn1A[i];
end;
Result := nn;
end;
procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; aD:Single;
begin
tm := System.DateUtils.DateTimeToUnix( Now, {InputAsUTC:} false )*1000;
tm := switchDWords(tm); // <-- hack tm
GeoField := TJGeomagneticField.JavaClass.init({Lat:}-23, {Lon:}-46, {Alt:}750, tm );
aD := GeoField.getDeclination(); // <-- this shows -21.8416 which is correct !
Label1.Text := FloatToStr( aD );
end;
Java2OP translation seems to be ok, so I wonder where the problem is.
Anyway, passing this hacked int64 parameter fixed it.
reported to QC: quality.embarcadero.com/browse/RSP-21602
– Omar Reis
Nov 20 '18 at 11:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342348%2fwrong-result-calling-android-method-from-delphi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I narrowed the problem to the "timeMillis" parameter.
It is a Java long, which corresponds to Delphi's int64.
I suspected of some kind of endian problem, so I
tried to switch the long parameter high and lo dwords.
And it worked !
function switchDWords(n:int64):int64; // switch hi <--> lo dwords of an int64
var i: Integer;
nn :int64; nnA:array[0..7] of byte absolute nn;
nn1:int64; nn1A:array[0..7] of byte absolute nn1;
begin
nn1 := n; // copy n
for i := 0 to 3 do // switch bytes hidword <--> lodword
begin
nnA[i] := nn1A[i+4];
nnA[i+4] := nn1A[i];
end;
Result := nn;
end;
procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; aD:Single;
begin
tm := System.DateUtils.DateTimeToUnix( Now, {InputAsUTC:} false )*1000;
tm := switchDWords(tm); // <-- hack tm
GeoField := TJGeomagneticField.JavaClass.init({Lat:}-23, {Lon:}-46, {Alt:}750, tm );
aD := GeoField.getDeclination(); // <-- this shows -21.8416 which is correct !
Label1.Text := FloatToStr( aD );
end;
Java2OP translation seems to be ok, so I wonder where the problem is.
Anyway, passing this hacked int64 parameter fixed it.
reported to QC: quality.embarcadero.com/browse/RSP-21602
– Omar Reis
Nov 20 '18 at 11:24
add a comment |
I narrowed the problem to the "timeMillis" parameter.
It is a Java long, which corresponds to Delphi's int64.
I suspected of some kind of endian problem, so I
tried to switch the long parameter high and lo dwords.
And it worked !
function switchDWords(n:int64):int64; // switch hi <--> lo dwords of an int64
var i: Integer;
nn :int64; nnA:array[0..7] of byte absolute nn;
nn1:int64; nn1A:array[0..7] of byte absolute nn1;
begin
nn1 := n; // copy n
for i := 0 to 3 do // switch bytes hidword <--> lodword
begin
nnA[i] := nn1A[i+4];
nnA[i+4] := nn1A[i];
end;
Result := nn;
end;
procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; aD:Single;
begin
tm := System.DateUtils.DateTimeToUnix( Now, {InputAsUTC:} false )*1000;
tm := switchDWords(tm); // <-- hack tm
GeoField := TJGeomagneticField.JavaClass.init({Lat:}-23, {Lon:}-46, {Alt:}750, tm );
aD := GeoField.getDeclination(); // <-- this shows -21.8416 which is correct !
Label1.Text := FloatToStr( aD );
end;
Java2OP translation seems to be ok, so I wonder where the problem is.
Anyway, passing this hacked int64 parameter fixed it.
reported to QC: quality.embarcadero.com/browse/RSP-21602
– Omar Reis
Nov 20 '18 at 11:24
add a comment |
I narrowed the problem to the "timeMillis" parameter.
It is a Java long, which corresponds to Delphi's int64.
I suspected of some kind of endian problem, so I
tried to switch the long parameter high and lo dwords.
And it worked !
function switchDWords(n:int64):int64; // switch hi <--> lo dwords of an int64
var i: Integer;
nn :int64; nnA:array[0..7] of byte absolute nn;
nn1:int64; nn1A:array[0..7] of byte absolute nn1;
begin
nn1 := n; // copy n
for i := 0 to 3 do // switch bytes hidword <--> lodword
begin
nnA[i] := nn1A[i+4];
nnA[i+4] := nn1A[i];
end;
Result := nn;
end;
procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; aD:Single;
begin
tm := System.DateUtils.DateTimeToUnix( Now, {InputAsUTC:} false )*1000;
tm := switchDWords(tm); // <-- hack tm
GeoField := TJGeomagneticField.JavaClass.init({Lat:}-23, {Lon:}-46, {Alt:}750, tm );
aD := GeoField.getDeclination(); // <-- this shows -21.8416 which is correct !
Label1.Text := FloatToStr( aD );
end;
Java2OP translation seems to be ok, so I wonder where the problem is.
Anyway, passing this hacked int64 parameter fixed it.
I narrowed the problem to the "timeMillis" parameter.
It is a Java long, which corresponds to Delphi's int64.
I suspected of some kind of endian problem, so I
tried to switch the long parameter high and lo dwords.
And it worked !
function switchDWords(n:int64):int64; // switch hi <--> lo dwords of an int64
var i: Integer;
nn :int64; nnA:array[0..7] of byte absolute nn;
nn1:int64; nn1A:array[0..7] of byte absolute nn1;
begin
nn1 := n; // copy n
for i := 0 to 3 do // switch bytes hidword <--> lodword
begin
nnA[i] := nn1A[i+4];
nnA[i+4] := nn1A[i];
end;
Result := nn;
end;
procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; aD:Single;
begin
tm := System.DateUtils.DateTimeToUnix( Now, {InputAsUTC:} false )*1000;
tm := switchDWords(tm); // <-- hack tm
GeoField := TJGeomagneticField.JavaClass.init({Lat:}-23, {Lon:}-46, {Alt:}750, tm );
aD := GeoField.getDeclination(); // <-- this shows -21.8416 which is correct !
Label1.Text := FloatToStr( aD );
end;
Java2OP translation seems to be ok, so I wonder where the problem is.
Anyway, passing this hacked int64 parameter fixed it.
edited Nov 19 '18 at 12:51
answered Nov 19 '18 at 11:46
Omar ReisOmar Reis
137110
137110
reported to QC: quality.embarcadero.com/browse/RSP-21602
– Omar Reis
Nov 20 '18 at 11:24
add a comment |
reported to QC: quality.embarcadero.com/browse/RSP-21602
– Omar Reis
Nov 20 '18 at 11:24
reported to QC: quality.embarcadero.com/browse/RSP-21602
– Omar Reis
Nov 20 '18 at 11:24
reported to QC: quality.embarcadero.com/browse/RSP-21602
– Omar Reis
Nov 20 '18 at 11:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342348%2fwrong-result-calling-android-method-from-delphi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Is your Delphi code giving you the same value for "tm" as
System.currentTimeMillis()
?– Barns
Nov 16 '18 at 18:07
I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly
– Omar Reis
Nov 16 '18 at 18:28
1
This is a great question and deserves more upvotes.
– J...
Nov 16 '18 at 19:07
1
System.currentTimeMillis()
is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi'sNow()
returns aTDateTime
expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things likeTrunc( (t-t0) ...)
forTDateTime
, that performs floating-point arithmetic that can skew the results. UseSystem.DateUtils.MilliSecondsBetween()
instead, which usesTTimeStamp
internally for more accurate date/time calculations.– Remy Lebeau
Nov 17 '18 at 1:02
1
Note, Delphi's RTL has a
System.DateUtils.DateTimeToUnix()
function to convert aTDateTime
to a Unix timestamp, but it has seconds precision (it usesSecondsBetween()
internally), but it would be very easy to copy it and adjust it to use milliseconds instead.– Remy Lebeau
Nov 17 '18 at 1:09