datih.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. {
  2. *********************************************************************
  3. Copyright (C) 1997, 1998 Gertjan Schouten
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. *********************************************************************
  16. System Utilities For Free Pascal
  17. }
  18. type
  19. PDayTable = ^TDayTable;
  20. TDayTable = array[1..12] of Word;
  21. const
  22. HoursPerDay = 24;
  23. MinsPerHour = 60;
  24. SecsPerMin = 60;
  25. MSecsPerSec = 1000;
  26. MinsPerDay = HoursPerDay * MinsPerHour;
  27. SecsPerDay = MinsPerDay * SecsPerMin;
  28. MSecsPerDay = SecsPerDay * MSecsPerSec;
  29. {TDateTime holds the date as the number of days since 30 Dec 1899, known as
  30. Microsoft Excel epoch}
  31. JulianEpoch = TDateTime(-2415018.5);
  32. UnixEpoch = JulianEpoch + TDateTime(2440587.5);
  33. DateDelta = 693594; // Days between 1/1/0001 and 12/31/1899
  34. UnixDateDelta = Trunc(UnixEpoch); //25569
  35. { True=Leapyear }
  36. MonthDays: array [Boolean] of TDayTable =
  37. ((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
  38. (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));
  39. var
  40. TwoDigitYearCenturyWindow : word absolute DefaultFormatSettings.TwoDigitYearCenturyWindow;
  41. { Threshold to be subtracted from year before
  42. age-detection.}
  43. { date time formatting characters:
  44. c : shortdateformat + ' ' + longtimeformat
  45. d : day of month
  46. dd : day of month (leading zero)
  47. ddd : day of week (abbreviation)
  48. dddd : day of week (full)
  49. ddddd : shortdateformat
  50. dddddd : longdateformat
  51. m : month
  52. mm : month (leading zero)
  53. mmm : month (abbreviation)
  54. mmmm : month (full)
  55. y : year (two digits)
  56. yy : year (two digits)
  57. yyyy : year (four digits, with century)
  58. h : hour
  59. hh : hour (leading zero)
  60. n : minute
  61. nn : minute (leading zero)
  62. s : second
  63. ss : second (leading zero)
  64. t : shorttimeformat
  65. tt : longtimeformat
  66. am/pm : use 12 hour clock and display am and pm accordingly
  67. a/p : use 12 hour clock and display a and p accordingly
  68. / : insert date seperator
  69. : : insert time seperator
  70. "xx" : literal text
  71. 'xx' : literal text
  72. }
  73. type
  74. { windows isn't defined in 2.0.2 (FK) }
  75. {$if not(defined(windows)) and not(defined(win32))}
  76. { Win32 reuses the struct from the Windows unit }
  77. TSystemTime = record
  78. Year, Month, Day: word;
  79. Hour, Minute, Second, MilliSecond: word;
  80. end ;
  81. {$endif windows}
  82. TTimeStamp = record
  83. Time: integer; { Number of milliseconds since midnight }
  84. Date: integer; { One plus number of days since 1/1/0001 }
  85. end ;
  86. function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
  87. function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
  88. function MSecsToTimeStamp(MSecs: Comp): TTimeStamp;
  89. function TimeStampToMSecs(const TimeStamp: TTimeStamp): comp;
  90. function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;
  91. function TryEncodeTime(Hour, Min, Sec, MSec: Word; out Time: TDateTime): Boolean;
  92. function EncodeDate(Year, Month, Day :word): TDateTime;
  93. function EncodeTime(Hour, Minute, Second, MilliSecond:word): TDateTime;
  94. function ComposeDateTime(Date,Time : TDateTime) : TDateTime;
  95. procedure DecodeDate(Date: TDateTime; out Year, Month, Day: word);
  96. function DecodeDateFully(const DateTime: TDateTime; out Year, Month, Day, DOW: Word): Boolean;
  97. procedure DecodeTime(Time: TDateTime; out Hour, Minute, Second, MilliSecond: word);
  98. procedure DateTimeToSystemTime(DateTime: TDateTime; out SystemTime: TSystemTime);
  99. function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
  100. function DayOfWeek(DateTime: TDateTime): integer;
  101. function Date: TDateTime;
  102. function Time: TDateTime;
  103. function Now: TDateTime;
  104. function IncMonth(const DateTime: TDateTime; NumberOfMonths: integer = 1 ): TDateTime;
  105. procedure IncAMonth(var Year, Month, Day: Word; NumberOfMonths: Integer = 1);
  106. function IsLeapYear(Year: Word): boolean;
  107. function DateToStr(Date: TDateTime): string;
  108. function TimeToStr(Time: TDateTime): string;
  109. function DateTimeToStr(DateTime: TDateTime): string;
  110. function StrToDate(const S: ShortString): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  111. function StrToDate(const S: Ansistring): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  112. function StrToDate(const S: ShortString; separator : char): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
  113. function StrToDate(const S: AnsiString; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  114. function StrToTime(const S: Shortstring): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  115. function StrToTime(const S: Ansistring): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  116. function StrToTime(const S: ShortString; separator : char): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
  117. function StrToTime(const S: AnsiString; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  118. function StrToDate(const S: ShortString; const useformat : string; separator : char): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
  119. function StrToDate(const S: AnsiString; const useformat : string; separator : char): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
  120. function StrToTime(const S: PChar; Len : integer; separator : char = #0): TDateTime;
  121. function StrToDate(const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
  122. function StrToDateTime(const S: string): TDateTime;
  123. function StrToDateTime(const s: ShortString; const UseFormat : TFormatSettings): TDateTime;
  124. function StrToDateTime(const s: AnsiString; const UseFormat : TFormatSettings): TDateTime;
  125. function FormatDateTime(FormatStr: string; DateTime: TDateTime):string;
  126. procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
  127. Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
  128. Function FileDateToDateTime (Filedate : Longint) :TDateTime;
  129. function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean; {$ifdef SYSUTILSINLINE}inline;{$endif}
  130. function TryStrToDate(const S: AnsiString; out Value: TDateTime): Boolean; {$ifdef SYSUTILSINLINE}inline;{$endif}
  131. function TryStrToDate(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
  132. function TryStrToDate(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
  133. function TryStrToTime(const S: ShortString; out Value: TDateTime): Boolean; {$ifdef SYSUTILSINLINE}inline;{$endif}
  134. function TryStrToTime(const S: AnsiString; out Value: TDateTime): Boolean; {$ifdef SYSUTILSINLINE}inline;{$endif}
  135. function TryStrToTime(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
  136. function TryStrToTime(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
  137. function TryStrToDate(const S: ShortString; out Value: TDateTime;
  138. const useformat : string; separator : char = #0): Boolean;
  139. function TryStrToDate(const S: AnsiString; out Value: TDateTime;
  140. const useformat : string; separator : char = #0): Boolean;
  141. function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean;
  142. function TryStrToDateTime(const S: AnsiString; out Value: TDateTime): Boolean;
  143. // function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  144. // function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  145. // function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  146. function StrToDateDef(const S: ShortString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  147. function StrToDateDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  148. function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  149. function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  150. function StrToDateTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  151. function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  152. function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  153. function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  154. function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  155. function StrToDateTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  156. function CurrentYear:Word;
  157. { FPC Extra }
  158. Procedure GetLocalTime(var SystemTime: TSystemTime);
  159. procedure ReplaceTime(var dati:TDateTime; NewTime : TDateTime); inline;
  160. procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime); inline;