unixandroid.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2016 by Yury Sidorov,
  4. member of the Free Pascal development team.
  5. Android-specific part of the System unit.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. type
  13. UErrorCode = SizeInt;
  14. int32_t = longint;
  15. uint32_t = longword;
  16. UBool = LongBool;
  17. UCalendar = pointer;
  18. UCalendarType = longint;
  19. UCalendarDisplayNameType = longint;
  20. UCalendarDateFields = longint;
  21. const
  22. UCAL_STANDARD = 0;
  23. UCAL_SHORT_STANDARD = 1;
  24. UCAL_DST = 2;
  25. UCAL_SHORT_DST = 3;
  26. UCAL_ZONE_OFFSET = 15;
  27. UCAL_DST_OFFSET = 16;
  28. var
  29. ucal_open: function (zoneID: PUnicodeChar; len: int32_t; locale: PAnsiChar; ctype: UCalendarType; var status: UErrorCode): UCalendar; cdecl;
  30. ucal_close: procedure (cal: UCalendar); cdecl;
  31. ucal_getTimeZoneDisplayName: function (cal: UCalendar; dtype: UCalendarDisplayNameType; locale: PAnsiChar; result: PUnicodeChar; resultLength: int32_t;
  32. var status: UErrorCode): int32_t; cdecl;
  33. ucal_inDaylightTime: function (cal: UCalendar; var status: UErrorCode): UBool; cdecl;
  34. ucal_get: function (cal: UCalendar; field: UCalendarDateFields; var status: UErrorCode): int32_t; cdecl;
  35. var
  36. TZStandardName: utf8string;
  37. TZDaylightName: utf8string;
  38. function GetIcuProc(const Name: AnsiString; var ProcPtr; libId: longint): boolean; external name 'CWSTRING_GET_ICU_PROC';
  39. procedure ReadTimeZoneFromICU;
  40. var
  41. locale: utf8string;
  42. tz: unicodestring;
  43. res: unicodestring;
  44. err: UErrorCode;
  45. cal: UCalendar;
  46. begin
  47. if not GetIcuProc('ucal_open', ucal_open, 1) then exit;
  48. if not GetIcuProc('ucal_close', ucal_close, 1) then exit;
  49. if not GetIcuProc('ucal_getTimeZoneDisplayName', ucal_getTimeZoneDisplayName, 1) then exit;
  50. if not GetIcuProc('ucal_inDaylightTime', ucal_inDaylightTime, 1) then exit;
  51. if not GetIcuProc('ucal_get', ucal_get, 1) then exit;
  52. locale:='en_US';
  53. tz:=unicodestring(GetSystemProperty('persist.sys.timezone'));
  54. err:=0;
  55. cal:=ucal_open(PUnicodeChar(tz), Length(tz), PAnsiChar(locale), 0, err);
  56. if cal = nil then
  57. exit;
  58. tzdaylight:=ucal_inDaylightTime(cal, err);
  59. SetLength(res, 200);
  60. SetLength(res, ucal_getTimeZoneDisplayName(cal, UCAL_SHORT_STANDARD, PAnsiChar(locale), PUnicodeChar(res), Length(res), err));
  61. TZStandardName:=utf8string(res);
  62. tzname[False]:=PAnsiChar(TZStandardName);
  63. SetLength(res, 200);
  64. SetLength(res, ucal_getTimeZoneDisplayName(cal, UCAL_SHORT_DST, PAnsiChar(locale), PUnicodeChar(res), Length(res), err));
  65. TZDaylightName:=utf8string(res);
  66. tzname[True]:=PAnsiChar(TZDaylightName);
  67. Tzseconds:=ucal_get(cal, UCAL_ZONE_OFFSET, err) div 1000;
  68. if tzdaylight then
  69. Tzseconds:=Tzseconds + ucal_get(cal, UCAL_DST_OFFSET, err) div 1000;
  70. ucal_close(cal);
  71. end;
  72. type
  73. Ptm = ^tm;
  74. tm = record
  75. tm_sec : longint;
  76. tm_min : longint;
  77. tm_hour : longint;
  78. tm_mday : longint;
  79. tm_mon : longint;
  80. tm_year : longint;
  81. tm_wday : longint;
  82. tm_yday : longint;
  83. tm_isdst : longint;
  84. case boolean of
  85. false : (tm_gmtoff : longint;tm_zone : Pchar);
  86. true : (__tm_gmtoff : longint;__tm_zone : Pchar);
  87. end;
  88. function localtime(t: PLongInt): Ptm; cdecl; external 'c' name 'localtime';
  89. var
  90. c_tzname: array[0..1] of PAnsiChar; external 'c' name 'tzname';
  91. procedure ReadTimeZoneFromLibC;
  92. var
  93. t: longint;
  94. tt: Ptm;
  95. begin
  96. t:=fptime;
  97. tt:=localtime(@t);
  98. tzname[false]:=c_tzname[0];
  99. tzname[true]:=c_tzname[1];
  100. if tt <> nil then
  101. begin
  102. tzdaylight:=tt^.tm_isdst <> 0;
  103. tzseconds:=tt^.tm_gmtoff;
  104. end;
  105. end;
  106. procedure InitLocalTime;
  107. begin
  108. if SystemApiLevel > 10 then
  109. ReadTimeZoneFromLibC;
  110. // If cuurent Android version is too old and does not support timezone
  111. // in libc, use ICU library.
  112. if tzname[false] = nil then
  113. ReadTimeZoneFromICU;
  114. end;