Recent

Author Topic: Why GetDateTimeDecode() does not return milliseconds like DecodeDateTime()?  (Read 3279 times)

Atak_Snajpera

  • New Member
  • *
  • Posts: 11
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;        

winni

  • Hero Member
  • *****
  • Posts: 3045
Hi!

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

Winni

engkin

  • Hero Member
  • *****
  • Posts: 3112
@Atak_Snajpera,

I just tested, Now() gives my local time. It uses GetLocalTime.

Atak_Snajpera

  • New Member
  • *
  • Posts: 11
On Android 9 (Redmi 7) I always get UTC-0 instead of UTC+1 with Now(). On windows Now () works fine.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Laz/FPC version?
Are you using LAMW, jvm or something else?

Atak_Snajpera

  • New Member
  • *
  • Posts: 11
My version of Lazarus for Android (LAMW)

engkin

  • Hero Member
  • *****
  • Posts: 3112
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;
« Last Edit: January 18, 2022, 03:58:08 pm by engkin »

 

TinyPortal © 2005-2018