Converting from Int32 to element datetime?

Have this function on delphi

var result : TDateTime;

result := location.getTime/86400000.0 + 25569.0;

On android location.getTime is an Int32

How can i pass this to element datetime type? i try to cast and get invalid typecaste error.

Thanks!

I’m not entirely sure of the scale of the Delphi DateTime, but something like:

new DateTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc.Ticks + location.getTime() * 10000)

(might be 1 zero off)

1 Like

I use this helper functions on Delphi side to convert DateTime between Android and PC:

function Android2DateTime(ut: Int64): TDateTime;
begin
    if ut = 0 then
        Result := 0
    else
        Result := UnixToDateTime( ut div 1000 );
end;

function DateTime2Android(dt: TDateTime): Int64;
begin
    if dt = 0 then
        Result := 0
    else
        Result := DateTimeToUnix( dt ) * 1000;
end;
1 Like

I’ll look at adding these to Elements RTL.

1 Like

Thank you!

Carlo,
DateTimeKind.Utc.Ticks is not present on android. Is a constant value? Im taking datetime from location because i dont want to do from the main clock in the event the user change that.

Please let mek now how to do that on android.

I hope element do in a universal way (android + iphone)

Oh then can I presume you use rtl2?

new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(location.getTime() / 1000)

Hi Carlo,

Now im coding for COCOA, cant get a correct datetime.

Thats the code:

var SecondsinTimeZone : Long := 0;
// 29/09/2017 ver si est esta correcto, funciona para bolivia al menos...
SecondsinTimeZone := self.adapter_user_timezone * 60 * 60 * 2;
{$IFDEF JAVA}
ServerDateTime := new RemObjects.elements.rtl.DateTime(1970, 1, 1, 0, 0, 0).AddSeconds((aLastLocation.getTime() / 1000) + SecondsinTimeZone);
{$else}
// ios code
var TiempoenSegundos : ULONG := (aLastLocation.timestamp.timeIntervalSince1970 * 1000).ToString.intValue;
ServerDateTime := new RemObjects.Elements.RTL.DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(TiempoenSegundos + SecondsinTimeZone);
{$ENDIF}

JAVA code works, IOS dont. Can please tell me what im doing wrong?

Best regards.

What specifically doesn’t work?

Returns an invalid datetime in year 2038.

ah thats because
timeIntervalSince1970

already returns seconds, not msec. you don’t need to * 1000 it.

1 Like

Works! Thanks!

2 Likes