timezone.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. {
  2. Support for timezone info in /usr/share/timezone
  3. }
  4. type
  5. ttzhead=packed record
  6. tzh_reserved : array[0..19] of byte;
  7. tzh_ttisgmtcnt,
  8. tzh_ttisstdcnt,
  9. tzh_leapcnt,
  10. tzh_timecnt,
  11. tzh_typecnt,
  12. tzh_charcnt : longint;
  13. end;
  14. pttinfo=^tttinfo;
  15. tttinfo=packed record
  16. offset : longint;
  17. isdst : boolean;
  18. idx : byte;
  19. isstd : byte;
  20. isgmt : byte;
  21. end;
  22. pleap=^tleap;
  23. tleap=record
  24. transition : longint;
  25. change : longint;
  26. end;
  27. var
  28. num_transitions,
  29. num_leaps,
  30. num_types : longint;
  31. transitions : plongint = nil;
  32. type_idxs : pbyte = Nil;
  33. types : pttinfo = Nil;
  34. zone_names : pchar = Nil;
  35. leaps : pleap = Nil;
  36. function find_transition(timer:longint;timerIsUTC:Boolean;var trans_start,trans_end:longint):pttinfo;
  37. var
  38. i : longint;
  39. begin
  40. if (num_transitions=0) or (timer<transitions[0]) then
  41. begin
  42. i:=0;
  43. while (i<num_types) and (types[i].isdst) do
  44. inc(i);
  45. if (i=num_types) then
  46. i:=0;
  47. { unknown transition boundaries }
  48. trans_start:=low(trans_start);
  49. trans_end:=high(trans_end);
  50. end
  51. else
  52. begin
  53. i:=1;
  54. while i<=num_transitions-1 do
  55. begin
  56. case timerIsUTC of
  57. True: if (timer<transitions[i]) then break;
  58. False: if (timer<transitions[i]+types[type_idxs[i]].offset) then break;
  59. end;
  60. inc(i);
  61. end;
  62. trans_start:=transitions[i-1];
  63. trans_end:=transitions[i];
  64. i:=type_idxs[i-1];
  65. end;
  66. find_transition:=@types[i];
  67. end;
  68. function GetLocalTimezone(timer:cint;timerIsUTC:Boolean;var ATZInfo:TTZInfo;FullInfo:Boolean):Boolean;
  69. var
  70. info : pttinfo;
  71. i,trans_start,trans_end : longint;
  72. begin
  73. { reset }
  74. ATZInfo.Daylight:=false;
  75. ATZInfo.Seconds:=0;
  76. ATZInfo.Name[false]:=nil;
  77. ATZInfo.Name[true]:=nil;
  78. ATZInfo.validsince:=0;
  79. ATZInfo.validuntil:=0;
  80. ATZInfo.leap_correct:=0;
  81. ATZInfo.leap_hit:=0;
  82. { get info }
  83. info:=find_transition(timer,timerIsUTC,trans_start,trans_end);
  84. GetLocalTimezone:=assigned(info);
  85. if not GetLocalTimezone then
  86. exit;
  87. ATZInfo.validsince:=trans_start;
  88. ATZInfo.validuntil:=trans_end;
  89. ATZInfo.Daylight:=info^.isdst;
  90. ATZInfo.Seconds:=info^.offset;
  91. if not FullInfo then
  92. Exit;
  93. i:=0;
  94. while (i<num_types) do
  95. begin
  96. ATZInfo.name[types[i].isdst]:=@zone_names[types[i].idx];
  97. inc(i);
  98. end;
  99. ATZInfo.name[info^.isdst]:=@zone_names[info^.idx];
  100. i:=num_leaps;
  101. repeat
  102. if i=0 then
  103. exit;
  104. dec(i);
  105. until (timer>leaps[i].transition);
  106. ATZInfo.leap_correct:=leaps[i].change;
  107. if (timer=leaps[i].transition) and
  108. (((i=0) and (leaps[i].change>0)) or
  109. (leaps[i].change>leaps[i-1].change)) then
  110. begin
  111. ATZInfo.leap_hit:=1;
  112. while (i>0) and
  113. (leaps[i].transition=leaps[i-1].transition+1) and
  114. (leaps[i].change=leaps[i-1].change+1) do
  115. begin
  116. inc(ATZInfo.leap_hit);
  117. dec(i);
  118. end;
  119. end;
  120. end;
  121. procedure GetLocalTimezone(timer:cint;timerIsUTC:Boolean);
  122. begin
  123. GetLocalTimezone(timer,timerIsUTC,Tzinfo,true);
  124. end;
  125. Const
  126. DefaultTimeZoneDir = '/usr/share/zoneinfo';
  127. function TimeZoneDir : ShortString;
  128. begin
  129. // Observe TZDIR environment variable.
  130. TimeZoneDir:=fpgetenv('TZDIR');
  131. if TimeZoneDir='' then
  132. TimeZoneDir:=DefaultTimeZoneDir;
  133. if TimeZoneDir[length(TimeZoneDir)]<>'/' then
  134. TimeZoneDir:=TimeZoneDir+'/';
  135. end;
  136. procedure ReadTimezoneFile(fn:shortstring);
  137. procedure decode(var l:longint);
  138. var
  139. k : longint;
  140. p : pbyte;
  141. begin
  142. p:=pbyte(@l);
  143. if (p[0] and (1 shl 7))<>0 then
  144. k:=not 0
  145. else
  146. k:=0;
  147. k:=(k shl 8) or p[0];
  148. k:=(k shl 8) or p[1];
  149. k:=(k shl 8) or p[2];
  150. k:=(k shl 8) or p[3];
  151. l:=k;
  152. end;
  153. const
  154. bufsize = 2048;
  155. var
  156. buf : array[0..bufsize-1] of byte;
  157. bufptr : pbyte;
  158. f : longint;
  159. procedure readfilebuf;
  160. begin
  161. bufptr := @buf[0];
  162. fpread(f, buf, bufsize);
  163. end;
  164. function readbufbyte: byte;
  165. begin
  166. if bufptr > @buf[bufsize-1] then
  167. readfilebuf;
  168. readbufbyte := bufptr^;
  169. inc(bufptr);
  170. end;
  171. function readbuf(var dest; count: integer): integer;
  172. var
  173. numbytes: integer;
  174. begin
  175. readbuf := 0;
  176. repeat
  177. numbytes := (@buf[bufsize-1] + 1) - bufptr;
  178. if numbytes > count then
  179. numbytes := count;
  180. if numbytes > 0 then
  181. begin
  182. move(bufptr^, dest, numbytes);
  183. inc(bufptr, numbytes);
  184. dec(count, numbytes);
  185. inc(readbuf, numbytes);
  186. end;
  187. if count > 0 then
  188. readfilebuf
  189. else
  190. break;
  191. until false;
  192. end;
  193. var
  194. tzdir : shortstring;
  195. tzhead : ttzhead;
  196. i : longint;
  197. chars : longint;
  198. begin
  199. if fn='' then
  200. fn:='localtime';
  201. if fn[1]<>'/' then
  202. fn:=TimeZoneDir+fn;
  203. f:=fpopen(fn,Open_RdOnly);
  204. if f<0 then
  205. exit;
  206. bufptr := @buf[bufsize-1]+1;
  207. i:=readbuf(tzhead,sizeof(tzhead));
  208. if i<>sizeof(tzhead) then
  209. exit;
  210. decode(tzhead.tzh_timecnt);
  211. decode(tzhead.tzh_typecnt);
  212. decode(tzhead.tzh_charcnt);
  213. decode(tzhead.tzh_leapcnt);
  214. decode(tzhead.tzh_ttisstdcnt);
  215. decode(tzhead.tzh_ttisgmtcnt);
  216. num_transitions:=tzhead.tzh_timecnt;
  217. num_types:=tzhead.tzh_typecnt;
  218. chars:=tzhead.tzh_charcnt;
  219. num_leaps:=tzhead.tzh_leapcnt;
  220. reallocmem(transitions,num_transitions*sizeof(longint));
  221. reallocmem(type_idxs,num_transitions);
  222. reallocmem(types,num_types*sizeof(tttinfo));
  223. reallocmem(zone_names,chars);
  224. reallocmem(leaps,num_leaps*sizeof(tleap));
  225. readbuf(transitions^,num_transitions*4);
  226. readbuf(type_idxs^,num_transitions);
  227. for i:=0 to num_transitions-1 do
  228. decode(transitions[i]);
  229. for i:=0 to num_types-1 do
  230. begin
  231. readbuf(types[i].offset,4);
  232. readbuf(types[i].isdst,1);
  233. readbuf(types[i].idx,1);
  234. decode(types[i].offset);
  235. types[i].isstd:=0;
  236. types[i].isgmt:=0;
  237. end;
  238. readbuf(zone_names^,chars);
  239. for i:=0 to num_leaps-1 do
  240. begin
  241. readbuf(leaps[i].transition,4);
  242. readbuf(leaps[i].change,4);
  243. decode(leaps[i].transition);
  244. decode(leaps[i].change);
  245. end;
  246. for i:=0 to tzhead.tzh_ttisstdcnt-1 do
  247. types[i].isstd:=byte(readbufbyte<>0);
  248. for i:=0 to tzhead.tzh_ttisgmtcnt-1 do
  249. types[i].isgmt:=byte(readbufbyte<>0);
  250. fpclose(f);
  251. end;
  252. Const
  253. // Debian system; contains location of timezone file.
  254. TimeZoneLocationFile = '/etc/timezone';
  255. // SuSE has link in /usr/lib/zoneinfo/localtime to /etc/localtime
  256. // RedHat uses /etc/localtime
  257. TimeZoneFile = '/etc/localtime'; // POSIX
  258. AltTimeZoneFile = '/usr/lib/zoneinfo/localtime'; // Other
  259. iOSTimeZoneFile = '/var/db/timezone/localtime'; // iOS
  260. {$ifdef BSD}
  261. BSDTimeZonefile = DefaultTimeZoneDir; // BSD usually is POSIX
  262. // compliant though
  263. {$ENDIF}
  264. {$ifndef FPC_HAS_GETTIMEZONEFILE}
  265. function GetTimezoneFile:shortstring;
  266. var
  267. f,len : longint;
  268. fn,s : shortstring;
  269. info : stat;
  270. begin
  271. GetTimezoneFile:='';
  272. // Observe TZ variable.
  273. fn:=fpgetenv('TZ');
  274. if (fn<>'') then
  275. if (fn[1]=':') then
  276. begin
  277. Delete(fn,1,1);
  278. if (fn<>'') then
  279. begin
  280. if (fn[1]<>'/') then
  281. Exit(TimeZoneDir+fn);
  282. Exit(fn);
  283. end;
  284. end;
  285. if (fn='') then
  286. fn:=TimeZoneLocationFile;
  287. f:=fpopen(TimeZoneLocationFile,Open_RdOnly);
  288. if f>0 then
  289. begin
  290. len:=fpread(f,s[1],high(s));
  291. s[0]:=chr(len);
  292. len:=pos(#10,s);
  293. if len<>0 then
  294. s[0]:=chr(len-1);
  295. fpclose(f);
  296. GetTimezoneFile:=s;
  297. end
  298. // Try SuSE
  299. else if fpstat(TimeZoneFile,{$ifdef oldlinuxstat}baseunix.stat(info){$else}info{$endif})>=0 then
  300. GetTimeZoneFile:=TimeZoneFile
  301. // Try RedHat
  302. else If fpstat(AltTimeZoneFile,{$ifdef oldlinuxstat}baseunix.stat(info){$else}info{$endif})>=0 then
  303. GetTimeZoneFile:=AltTimeZoneFile
  304. {$ifdef BSD}
  305. // else
  306. // If fpstat(BSDTimeZoneFile,{$ifdef oldlinuxstat}baseunix.stat(info){$else}info{$endif})>=0 then
  307. // GetTimeZoneFile:=BSDTimeZoneFile
  308. {$ENDIF}
  309. {$if (defined(darwin) and defined(arm)) or defined(iphonesim)}
  310. else If fpstat(iOSTimeZoneFile,info)>=0 then
  311. GetTimeZoneFile:=iOSTimeZoneFile
  312. {$endif}
  313. end;
  314. {$endif ndef FPC_HAS_GETTIMEZONEFILE}
  315. procedure InitLocalTime;
  316. begin
  317. ReadTimezoneFile(GetTimezoneFile);
  318. GetLocalTimezone(fptime,false);
  319. end;
  320. procedure DoneLocalTime;
  321. begin
  322. if assigned(transitions) then
  323. freemem(transitions);
  324. transitions:=nil;
  325. if assigned(type_idxs) then
  326. freemem(type_idxs);
  327. type_idxs:=nil;
  328. if assigned(types) then
  329. freemem(types);
  330. types:=nil;
  331. if assigned(zone_names) then
  332. freemem(zone_names);
  333. zone_names:=Nil;
  334. if assigned(leaps) then
  335. freemem(leaps);
  336. leaps:=nil;
  337. num_transitions:=0;
  338. num_leaps:=0;
  339. num_types:=0;
  340. end;
  341. Procedure ReReadLocalTime;
  342. begin
  343. DoneLocalTime;
  344. InitLocalTime;
  345. end;