Lazarus

Programming => Operating Systems => Android => Topic started by: Atak_Snajpera on January 16, 2022, 09:02:59 pm

Title: Why GetDateTimeDecode() does not return milliseconds like DecodeDateTime()?
Post by: Atak_Snajpera on January 16, 2022, 09:02:59 pm
As I workaround I've created this function because Now() on Android device returns greenwich time (UTC+0) instead of my local time zone (UTC+1).
Do you know more elegant way of dealing with this issue?
Code: Pascal  [Select][+][-]
  1. function DateTimeNow():TDateTime;
  2. var Day,Month,Year,H,M,S,MS:integer;
  3.     _Day,_Month,_Year,_H,_M,_S,_MS:Word;
  4.     MilliSecDiff:int64;
  5.     DateTimeUTC,DateTimeCurrentZone:TDateTime;
  6. begin
  7.  
  8.      DecodeDateTime(Now,_Year,_Month,_Day,_H,_M,_S,_MS);
  9.      DateTimeUTC:=EncodeDateTime(_Year,_Month,_Day,_H,_M,_S,0);
  10.  
  11.      form1.GetDateTimeDecode(Day,Month,Year,H,M,S);
  12.      DateTimeCurrentZone:=EncodeDateTime(Year,Month,Day,H,M,S,0);
  13.  
  14.      MilliSecDiff:=MilliSecondsBetween(DateTimeUTC,DateTimeCurrentZone);
  15.  
  16.      if DateTimeCurrentZone<DateTimeUTC then MilliSecDiff:=0-MilliSecDiff;
  17.      Result:=IncMilliSecond(Now,MilliSecDiff);
  18.  
  19. end;        
Title: Re: Why GetDateTimeDecode() does not return milliseconds like DecodeDateTime()?
Post by: winni on January 16, 2022, 09:35:01 pm
Hi!

Does this function from sysutils return someting useful??
Code: Pascal  [Select][+][-]
  1.  
  2. function GetLocalTimeOffset: integer

Winni
Title: Re: Why GetDateTimeDecode() does not return milliseconds like DecodeDateTime()?
Post by: engkin on January 16, 2022, 10:42:39 pm
@Atak_Snajpera,

I just tested, Now() gives my local time. It uses GetLocalTime.
Title: Re: Why GetDateTimeDecode() does not return milliseconds like DecodeDateTime()?
Post by: Atak_Snajpera on January 17, 2022, 10:58:16 pm
On Android 9 (Redmi 7) I always get UTC-0 instead of UTC+1 with Now(). On windows Now () works fine.
Title: Re: Why GetDateTimeDecode() does not return milliseconds like DecodeDateTime()?
Post by: engkin on January 17, 2022, 11:47:35 pm
Laz/FPC version?
Are you using LAMW, jvm or something else?
Title: Re: Why GetDateTimeDecode() does not return milliseconds like DecodeDateTime()?
Post by: Atak_Snajpera on January 18, 2022, 09:35:50 am
My version of Lazarus for Android (LAMW)
Title: Re: Why GetDateTimeDecode() does not return milliseconds like DecodeDateTime()?
Post by: engkin on January 18, 2022, 03:56:07 pm
Your version is old. Now there is an include file for Android,  rtl/android/unixandroid.inc. If, for whatever reason you can't move to a newer version, maybe you can use the code related to your time zone from that file.

It gets the timezone info and local time from libC:
Code: Pascal  [Select][+][-]
  1. type
  2.    Ptm = ^tm;
  3.    tm = record
  4.         tm_sec : longint;
  5.         tm_min : longint;
  6.         tm_hour : longint;
  7.         tm_mday : longint;
  8.         tm_mon : longint;
  9.         tm_year : longint;
  10.         tm_wday : longint;
  11.         tm_yday : longint;
  12.         tm_isdst : longint;
  13.         case boolean of
  14.           false : (tm_gmtoff : longint;tm_zone : Pchar);
  15.           true  : (__tm_gmtoff : longint;__tm_zone : Pchar);
  16.          end;
  17.  
  18. function localtime(t: Ptime_t): Ptm; cdecl; external 'c' name 'localtime';
  19.  
  20. var
  21.   c_tzname: array[0..1] of PAnsiChar; external 'c' name 'tzname';
  22.  
  23. function ReadTimeZoneFromLibC: boolean;
  24. var
  25.   t: time_t;
  26.   tt: Ptm;
  27. begin
  28.   ReadTimeZoneFromLibC:=False;
  29.   tzname[false]:=c_tzname[0];
  30.   tzname[true]:=c_tzname[1];
  31.   t:=fptime;
  32.   tt:=localtime(@t);
  33.   if tt <> nil then
  34.     begin
  35.       tzdaylight:=tt^.tm_isdst <> 0;
  36.       tzseconds:=tt^.tm_gmtoff;
  37.       ReadTimeZoneFromLibC:=tzname[false] <> nil;
  38.     end;
  39. end;
TinyPortal © 2005-2018