dati.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. {
  2. *********************************************************************
  3. $Id$
  4. Copyright (C) 1997, 1998 Gertjan Schouten
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *********************************************************************
  17. System Utilities For Free Pascal
  18. }
  19. { date time functions }
  20. function IsLeapYear(Year: Word): Boolean;
  21. begin
  22. IsLeapYear := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
  23. end;
  24. function DoEncodeDate(Year, Month, Day: Word):longint;
  25. var
  26. I: Longint;
  27. begin
  28. DoEncodeDate := 0;
  29. if (Year >= 1) and (Year <= 9999) and (Month >= 1) and (Month <= 12) and
  30. (Day >= 1) and (Day <= 31) then begin
  31. Day := Day + DayTable[IsLeapYear(Year), Month] - 1;
  32. I := Year - 1;
  33. DoEncodeDate := I * 365 + I div 4 - I div 100 + I div 400 + Day;
  34. end ;
  35. end ;
  36. function doEncodeTime(Hour,Minute,Second,MilliSecond:word):longint;
  37. begin
  38. doEncodeTime := (Hour * 3600000 + Minute * 60000 + Second * 1000 + MilliSecond) { div MSecsPerDay} ;
  39. end ;
  40. function DateToStr(Date:TDateTime):string;
  41. begin
  42. DateToStr := FormatDateTime('c', Date);
  43. end ;
  44. function TimeToStr(Time:TDateTime):string;
  45. begin
  46. TimeToStr := FormatDateTime('t', Time);
  47. end ;
  48. function DateTimeToStr(DateTime:TDateTime):string;
  49. begin
  50. DateTimeToStr := FormatDateTime('c t', DateTime);
  51. end ;
  52. function EncodeDate(Year, Month, Day :word):TDateTime;
  53. begin
  54. EncodeDate := DoEncodeDate(Year, Month, Day);
  55. end ;
  56. function EncodeTime(Hour, Minute, Second, MilliSecond:word):TDateTime;
  57. begin
  58. EncodeTime := doEncodeTime(hour, minute, second, millisecond) / MSecsPerDay;
  59. end ;
  60. procedure DecodeDate(Date:TDateTime;var Year:word;var Month:word;var Day:word);
  61. const
  62. D1 = 365; { number of days in 1 year }
  63. D4 = D1 * 4 + 1; { number of days in 4 years }
  64. D100 = D4 * 25 - 1; { number of days in 100 years }
  65. D400 = D100 * 4 + 1; { number of days in 400 years }
  66. var
  67. i:Longint;
  68. l:longint;
  69. ly:boolean;
  70. begin
  71. l := Trunc(Int(Date));
  72. year := 1 + 400 * (l div D400); l := (l mod D400);
  73. year := year + 100 * (l div D100);l := (l mod D100);
  74. year := year + 4 * (l div D4);l := (l mod D4);
  75. year := year + (l div D1);l := 1 + (l mod D1);
  76. month := 0;
  77. ly := IsLeapYear(Year);
  78. while (month < 12) and (l > DayTable[ly, month + 1]) do
  79. inc(month);
  80. day := l - DayTable[ly, month];
  81. end ;
  82. procedure DecodeTime(Time:TDateTime;var Hour:word;var Minute:word;var Second:word;var MilliSecond:word);
  83. var l:longint;
  84. begin
  85. l := Trunc(Frac(time) * MSecsPerDay);
  86. Hour := l div 3600000;l := l mod 3600000;
  87. Minute := l div 60000;l := l mod 60000;
  88. Second := l div 1000;l := l mod 1000;
  89. MilliSecond := l;
  90. end ;
  91. function FormatDateTime(formatstr:string;DateTime:TDateTime):string;
  92. var i:longint;result:string;current:string;e:longint;
  93. y,m,d,h,n,s,ms:word;
  94. mDate, mTime:double;
  95. begin
  96. mDate := int(DateTime);
  97. mTime := frac(DateTime);
  98. DecodeDate(mDate, y, m, d);
  99. DecodeTime(mTime, h, n, s, ms);
  100. result := '';
  101. current := '';
  102. i := 1;
  103. e := 0;
  104. while not(i > length(formatstr)) do begin
  105. while not(formatstr[i] in [' ','"','/',':','''']) and not(i > length(formatstr)) do begin
  106. current := current + formatstr[i];
  107. inc(i);
  108. end ;
  109. if ((current = 'a') or (current = 'am')) and (formatstr[i] = '/') then begin
  110. inc(i);current := current + '/';
  111. while not(formatstr[i] in [' ','"','/',':','''']) and not(i > length(formatstr)) do begin
  112. current := current + formatstr[i];
  113. inc(i);
  114. end ;
  115. end ;
  116. if not(current = '') then begin
  117. if (current = 'c') then begin
  118. i := 1; result := ''; current := '';
  119. formatstr := ' ' + shortdateformat + '" "' + shorttimeformat;
  120. end ;
  121. if not(mTime = 0) then begin
  122. if (current = 't') then begin
  123. formatstr := ' ' + shorttimeformat + copy(formatstr, i, length(formatstr));
  124. i := 1;
  125. end
  126. else if (current = 'tt') then begin
  127. formatstr := ' ' + longtimeformat + copy(formatstr,i,length(formatstr));
  128. i := 1;
  129. end
  130. else if (current = 'h') then result := result + inttostr(h)
  131. else if (current = 'hh') then result := result + right('0'+inttostr(h),2)
  132. else if (current = 'n') then result := result + inttostr(n)
  133. else if (current = 'nn') then result := result + right('0'+inttostr(n),2)
  134. else if (current = 's') then result := result + inttostr(s)
  135. else if (current = 'ss') then result := result + right('0'+inttostr(s),2)
  136. else if (current = 'am/pm') then begin
  137. if (h < 13) then result := result + 'am'
  138. else result := result + 'pm';
  139. end
  140. else if (current = 'a/p') then begin
  141. if h < 13 then result := result + 'a'
  142. else result := result + 'p';
  143. end
  144. else if (current = 'ampm') then begin
  145. if h < 13 then strCat(result, TimeAMString)
  146. else strCat(result, TimePMString);
  147. end ;
  148. end ;
  149. if not(mDate = 0) then begin
  150. if (current = 'd') then result := result + inttostr(d)
  151. else if (current = 'dd') then result := result + right('0' + inttostr(d), 2)
  152. else if (current = 'ddd') then StrCat(result, shortdaynames[DayOfWeek(DateTime)])
  153. else if (current = 'dddd') then StrCat(result, longdaynames[DayOfWeek(DateTime)])
  154. else if (current = 'm') then result := result + inttostr(m)
  155. else if (current = 'mm') then result := result + right('0' + inttostr(m), 2)
  156. else if (current = 'mmm') then StrCat(result, shortmonthnames[m])
  157. else if (current = 'mmmm') then StrCat(result, longmonthnames[m])
  158. else if (current = 'y') then result := result + inttostr(y)
  159. else if (current = 'yy') then result := result + right(inttostr(y), 2)
  160. else if (current = 'yyyy') or (current = 'yyy') then result := result + inttostr(y);
  161. end ;
  162. current := '';
  163. end ;
  164. if (formatstr[i] = '/') and not(mDate = 0) then result := result + dateseparator
  165. else if (formatstr[i] = ':') and not(mTime = 0) then result := result + timeseparator
  166. else if (formatstr[i] in ['"','''']) then begin
  167. inc(i);
  168. while not(formatstr[i] in ['"','''']) and not(i > length(formatstr)) do begin
  169. result := result + formatstr[i];
  170. inc(i);
  171. end ;
  172. end ;
  173. inc(i);
  174. end ;
  175. FormatDateTime := Result;
  176. end ;
  177. function StrToDate(const s:string):TDateTime;
  178. var
  179. df:string;
  180. d,m,y:word;n,i:longint;c:word;
  181. s1:string[4];
  182. values:array[0..2] of longint;
  183. LocalTime:tsystemtime;
  184. begin
  185. df := UpperCase(ShortDateFormat);
  186. d := 0;m := 0;y := 0;
  187. for i := 0 to 2 do values[i] := 0;
  188. s1 := '';
  189. n := 0;
  190. for i := 1 to length(s) do begin
  191. if (s[i] in ['0'..'9']) then s1 := s1 + s[i];
  192. if (s[i] in [dateseparator,' ']) or (i = length(s)) then begin
  193. val(s1, values[n], c);
  194. s1 := '';
  195. inc(n);
  196. end ;
  197. end ;
  198. if (df = 'D/M/Y') then begin
  199. d := values[0];
  200. m := values[1];
  201. y := values[2];
  202. end
  203. else if (df = 'M/D/Y') then begin
  204. if (n > 1) then begin
  205. m := values[0];
  206. d := values[1];
  207. y := values[2];
  208. end
  209. else { if there is just one value, it is the day of the month }
  210. d := values[0];
  211. end
  212. else if (df = 'Y/M/D') then begin
  213. if (n = 3) then begin
  214. y := values[0];
  215. m := values[1];
  216. d := values[2];
  217. end
  218. else if (n = 2) then begin
  219. m := values[0];
  220. d := values[1];
  221. end
  222. else if (n = 1) then
  223. d := values[0];
  224. end ;
  225. if (n < 3) then begin
  226. getLocalTime(LocalTime);
  227. y := LocalTime.wYear;
  228. if (n < 2) then
  229. m := LocalTime.wMonth;
  230. end ;
  231. if (y >= 0) and (y < 100) then y := 1900 + y;
  232. StrToDate := DoEncodeDate(y, m, d);
  233. end ;
  234. function StrToTime(const s:string):TDateTime;
  235. begin
  236. end ;
  237. function StrToDateTime(const s:string):TDateTime;
  238. begin
  239. end ;
  240. function DayOfWeek(DateTime:TDateTime):longint;
  241. begin
  242. DayOfWeek := (1 + Trunc(DateTime)) mod 7;
  243. end ;
  244. procedure getlocaltime(var systemtime:tsystemtime);
  245. var wDayOfWeek:word;
  246. begin
  247. getdate(systemtime.wYear,
  248. systemtime.wMonth,
  249. systemtime.wDay,
  250. wDayOfWeek);
  251. gettime(systemtime.whour,
  252. systemtime.wminute,
  253. systemtime.wsecond,
  254. systemtime.wmillisecond);
  255. systemtime.wmillisecond := systemtime.wmillisecond * 10;
  256. end ;
  257. function Date:TDateTime;
  258. var systemtime:tsystemtime;
  259. begin
  260. getlocaltime(systemtime);
  261. date := doEncodeDate(systemtime.wYear,systemtime.wMonth,systemtime.wDay);
  262. end ;
  263. function Time:TDateTime;
  264. var systemtime:tsystemtime;
  265. begin
  266. getlocaltime(systemtime);
  267. time := doEncodeTime(systemtime.wHour,systemtime.wMinute,
  268. systemtime.wSecond,systemtime.wMillisecond) / MSecsPerDay;
  269. end ;
  270. function Now:TDateTime;
  271. var systemtime:tsystemtime;
  272. begin
  273. getlocaltime(systemtime);
  274. now := doEncodeDate(systemtime.wYear,systemtime.wMonth,systemtime.wDay) +
  275. doEncodeTime(systemtime.wHour,systemtime.wMinute,
  276. systemtime.wSecond,systemtime.wMillisecond) / MSecsPerDay;
  277. end ;
  278. {
  279. $Log$
  280. Revision 1.1 1998-04-10 15:17:46 michael
  281. + Initial implementation; Donated by Gertjan Schouten
  282. His file was split into several files, to keep it a little bit structured.
  283. }