unixutil.pp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2013 by the Free Pascal development team
  4. DO NOT ADD ROUTINES TO THIS FILE!
  5. THE ROUTINES IN THIS FILE ARE INTERNAL AND NOT FOR END USER USAGE!
  6. Background: This unit contains leftovers from the unix restructure that
  7. shouldn't be in the interface of unit baseunix/unix, but are needed
  8. in these units. (at the time routines were still being moved
  9. from baseunix to unix, and unit baseunix couldn't depend on unix)
  10. The routines are fairly OS independent but can't move to
  11. OS independent because the lowlevel units baseunix/unix depend
  12. on them. If they need to be generally accessable, copy these
  13. functions to a general purpose, OS independent, supportable unit.
  14. See the file COPYING.FPC, included in this distribution,
  15. for details about the copyright.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. **********************************************************************}
  20. unit unixutil;
  21. interface
  22. uses BaseUnix;
  23. type
  24. TTZInfo = record
  25. daylight : boolean;
  26. name : array[boolean] of pchar;
  27. seconds : Longint; // difference from UTC
  28. validsince : int64; // UTC timestamp
  29. validuntil : int64; // UTC timestamp
  30. leap_correct : longint;
  31. leap_hit : longint;
  32. end;
  33. var
  34. Tzinfo : TTZInfo;
  35. ReloadTzinfo : TProcedure;
  36. Function GetTzseconds : Longint;
  37. property Tzseconds : Longint read GetTzseconds;
  38. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  39. Function StringToPPChar(Var S:RawByteString;ReserveEntries:integer):ppchar;
  40. function ArrayStringToPPchar(const S:Array of RawByteString;reserveentries:Longint):ppchar; // const ?
  41. Function LocalToEpoch(year,month,day,hour,minute,second:Word):int64; deprecated 'use DateUtils.DateTimeToUnix';
  42. Procedure EpochToLocal(epoch:int64;var year,month,day,hour,minute,second:Word); deprecated 'use DateUtils.UnixToDateTime';
  43. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word); deprecated 'use DateUtils.DateTimetoJulianDate';
  44. Function GregorianToJulian(Year,Month,Day:Longint):LongInt; deprecated 'use DateUtils.JulianDateToDateTime';
  45. implementation
  46. Function GetTzseconds : Longint;
  47. var
  48. curtime: time_t;
  49. begin
  50. curtime:=fptime;
  51. if assigned(ReloadTzinfo) and ((curtime<Tzinfo.validsince+Tzinfo.seconds) or (curtime>Tzinfo.validuntil+Tzinfo.seconds)) then
  52. ReloadTzinfo;
  53. GetTzseconds:=Tzinfo.seconds;
  54. end;
  55. function ArrayStringToPPchar(const S:Array of RawByteString;reserveentries:Longint):ppchar; // const ?
  56. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  57. // Note: for internal use by skilled programmers only
  58. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  59. var p : ppchar;
  60. i : LongInt;
  61. begin
  62. if High(s)<Low(s) Then Exit(NIL);
  63. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  64. // for cmd
  65. if p=nil then
  66. begin
  67. {$ifdef xunix}
  68. fpseterrno(ESysEnomem);
  69. {$endif}
  70. exit(NIL);
  71. end;
  72. for i:=low(s) to high(s) do
  73. p[i+Reserveentries]:=pchar(s[i]);
  74. p[high(s)+1+Reserveentries]:=nil;
  75. ArrayStringToPPchar:=p;
  76. end;
  77. Function StringToPPChar(Var S:RawByteString;ReserveEntries:integer):ppchar;
  78. {
  79. Create a PPChar to structure of pchars which are the arguments specified
  80. in the string S. Especially useful for creating an ArgV for Exec-calls
  81. }
  82. begin
  83. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  84. end;
  85. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  86. var
  87. i,nr : longint;
  88. Buf : ^char;
  89. p : ppchar;
  90. begin
  91. buf:=s;
  92. nr:=1;
  93. while (buf^<>#0) do // count nr of args
  94. begin
  95. while (buf^ in [' ',#9,#10]) do // Kill separators.
  96. inc(buf);
  97. inc(nr);
  98. if buf^='"' Then // quotes argument?
  99. begin
  100. inc(buf);
  101. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  102. inc(buf);
  103. if buf^='"' then // skip closing quote.
  104. inc(buf);
  105. end
  106. else
  107. begin // else std
  108. while not (buf^ in [' ',#0,#9,#10]) do
  109. inc(buf);
  110. end;
  111. end;
  112. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  113. StringToPPChar:=p;
  114. if p=nil then
  115. begin
  116. {$ifdef xunix}
  117. fpseterrno(ESysEnomem);
  118. {$endif}
  119. exit;
  120. end;
  121. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  122. buf:=s;
  123. while (buf^<>#0) do
  124. begin
  125. while (buf^ in [' ',#9,#10]) do // Kill separators.
  126. begin
  127. buf^:=#0;
  128. inc(buf);
  129. end;
  130. if buf^='"' Then // quotes argument?
  131. begin
  132. inc(buf);
  133. p^:=buf;
  134. inc(p);
  135. p^:=nil;
  136. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  137. inc(buf);
  138. if buf^='"' then // skip closing quote.
  139. begin
  140. buf^:=#0;
  141. inc(buf);
  142. end;
  143. end
  144. else
  145. begin
  146. p^:=buf;
  147. inc(p);
  148. p^:=nil;
  149. while not (buf^ in [' ',#0,#9,#10]) do
  150. inc(buf);
  151. end;
  152. end;
  153. end;
  154. Const
  155. {Date Translation}
  156. C1970=2440588;
  157. D0 = 1461;
  158. D1 = 146097;
  159. D2 =1721119;
  160. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  161. Var
  162. YYear,XYear,Temp,TempMonth : LongInt;
  163. Begin
  164. Temp:=((JulianDN-D2) shl 2)-1;
  165. JulianDN:=Temp Div D1;
  166. XYear:=(Temp Mod D1) or 3;
  167. YYear:=(XYear Div D0);
  168. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  169. Day:=((Temp Mod 153)+5) Div 5;
  170. TempMonth:=Temp Div 153;
  171. If TempMonth>=10 Then
  172. Begin
  173. inc(YYear);
  174. dec(TempMonth,12);
  175. End;
  176. inc(TempMonth,3);
  177. Month := TempMonth;
  178. Year:=YYear+(JulianDN*100);
  179. end;
  180. Procedure EpochToLocal(epoch:Int64;var year,month,day,hour,minute,second:Word);
  181. {
  182. Transforms Epoch time into local time (hour, minute,seconds)
  183. }
  184. Var
  185. DateNum: LongInt;
  186. Begin
  187. inc(Epoch,TZSeconds);
  188. Datenum:=(Epoch Div 86400) + c1970;
  189. JulianToGregorian(DateNum,Year,Month,day);
  190. Epoch:=Abs(Epoch Mod 86400);
  191. Hour:=Epoch Div 3600;
  192. Epoch:=Epoch Mod 3600;
  193. Minute:=Epoch Div 60;
  194. Second:=Epoch Mod 60;
  195. End;
  196. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Int64;
  197. {
  198. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  199. (seconds since 00:00, january 1 1970, corrected for local time zone)
  200. }
  201. Begin
  202. LocalToEpoch:=(Int64(GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  203. (LongInt(Hour)*3600)+(Longint(Minute)*60)+Second-TZSeconds;
  204. End;
  205. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  206. Var
  207. Century,XYear: LongInt;
  208. Begin
  209. If Month<=2 Then
  210. Begin
  211. Dec(Year);
  212. Inc(Month,12);
  213. End;
  214. Dec(Month,3);
  215. Century:=(longint(Year Div 100)*D1) shr 2;
  216. XYear:=(longint(Year Mod 100)*D0) shr 2;
  217. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  218. End;
  219. end.