datih.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. {
  2. *********************************************************************
  3. Copyright (C) 1997, 1998 Gertjan Schouten
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************
  10. System Utilities For Free Pascal
  11. }
  12. type
  13. PDayTable = ^TDayTable;
  14. TDayTable = array[1..12] of Word;
  15. const
  16. HoursPerDay = 24;
  17. MinsPerHour = 60;
  18. SecsPerMin = 60;
  19. MSecsPerSec = 1000;
  20. MinsPerDay = HoursPerDay * MinsPerHour;
  21. SecsPerDay = MinsPerDay * SecsPerMin;
  22. MSecsPerDay = SecsPerDay * MSecsPerSec;
  23. {TDateTime holds the date as the number of days since 30 Dec 1899, known as
  24. Microsoft Excel epoch}
  25. JulianEpoch = TDateTime(-2415018.5);
  26. UnixEpoch = JulianEpoch + TDateTime(2440587.5);
  27. DateDelta = 693594; // Days between 1/1/0001 and 12/31/1899
  28. UnixDateDelta = Trunc(UnixEpoch); //25569
  29. { True=Leapyear }
  30. MonthDays: array [Boolean] of TDayTable =
  31. ((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
  32. (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));
  33. var
  34. TwoDigitYearCenturyWindow : word absolute DefaultFormatSettings.TwoDigitYearCenturyWindow;
  35. { Threshold to be subtracted from year before
  36. age-detection.}
  37. { date time formatting characters:
  38. c : shortdateformat + ' ' + longtimeformat
  39. d : day of month
  40. dd : day of month (leading zero)
  41. ddd : day of week (abbreviation)
  42. dddd : day of week (full)
  43. ddddd : shortdateformat
  44. dddddd : longdateformat
  45. m : month
  46. mm : month (leading zero)
  47. mmm : month (abbreviation)
  48. mmmm : month (full)
  49. y : year (two digits)
  50. yy : year (two digits)
  51. yyyy : year (four digits, with century)
  52. h : hour
  53. hh : hour (leading zero)
  54. n : minute
  55. nn : minute (leading zero)
  56. s : second
  57. ss : second (leading zero)
  58. t : shorttimeformat
  59. tt : longtimeformat
  60. am/pm : use 12 hour clock and display am and pm accordingly
  61. a/p : use 12 hour clock and display a and p accordingly
  62. / : insert date seperator
  63. : : insert time seperator
  64. "xx" : literal text
  65. 'xx' : literal text
  66. }
  67. type
  68. { windows isn't defined in 2.0.2 (FK) }
  69. {$if not(defined(windows)) and not(defined(win32))}
  70. { Win32 reuses the struct from the Windows unit }
  71. TSystemTime = record
  72. Year, Month, Day: word;
  73. Hour, Minute, Second, MilliSecond: word;
  74. end ;
  75. {$endif windows}
  76. TTimeStamp = record
  77. Time: integer; { Number of milliseconds since midnight }
  78. Date: integer; { One plus number of days since 1/1/0001 }
  79. end ;
  80. function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
  81. function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
  82. function MSecsToTimeStamp(MSecs: Comp): TTimeStamp;
  83. function TimeStampToMSecs(const TimeStamp: TTimeStamp): comp;
  84. function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;
  85. function TryEncodeTime(Hour, Min, Sec, MSec: Word; out Time: TDateTime): Boolean;
  86. function EncodeDate(Year, Month, Day :word): TDateTime;
  87. function EncodeTime(Hour, Minute, Second, MilliSecond:word): TDateTime;
  88. function ComposeDateTime(Date,Time : TDateTime) : TDateTime;
  89. procedure DecodeDate(Date: TDateTime; out Year, Month, Day: word);
  90. function DecodeDateFully(const DateTime: TDateTime; out Year, Month, Day, DOW: Word): Boolean;
  91. procedure DecodeTime(Time: TDateTime; out Hour, Minute, Second, MilliSecond: word);
  92. procedure DateTimeToSystemTime(DateTime: TDateTime; out SystemTime: TSystemTime);
  93. function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
  94. function DayOfWeek(DateTime: TDateTime): integer;
  95. function Date: TDateTime;
  96. function Time: TDateTime;
  97. function Now: TDateTime;
  98. function IncMonth(const DateTime: TDateTime; NumberOfMonths: integer = 1 ): TDateTime;
  99. procedure IncAMonth(var Year, Month, Day: Word; NumberOfMonths: Integer = 1);
  100. function IsLeapYear(Year: Word): boolean;
  101. function DateToStr(Date: TDateTime): string;
  102. function DateToStr(Date: TDateTime; const FormatSettings: TFormatSettings): string;
  103. function TimeToStr(Time: TDateTime): string;
  104. function TimeToStr(Time: TDateTime; const FormatSettings: TFormatSettings): string;
  105. function DateTimeToStr(DateTime: TDateTime): string;
  106. function DateTimeToStr(DateTime: TDateTime; const FormatSettings: TFormatSettings): string;
  107. function StrToDate(const S: ShortString): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  108. function StrToDate(const S: Ansistring): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  109. function StrToDate(const S: ShortString; separator : char): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
  110. function StrToDate(const S: AnsiString; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  111. function StrToTime(const S: Shortstring): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  112. function StrToTime(const S: Ansistring): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  113. function StrToTime(const S: ShortString; separator : char): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
  114. function StrToTime(const S: AnsiString; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
  115. function StrToTime(const S: string; FormatSettings : TFormatSettings): TDateTime;
  116. function StrToDate(const S: ShortString; const useformat : string; separator : char = #0): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
  117. function StrToDate(const S: AnsiString; const useformat : string; separator : char = #0): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
  118. function StrToTime(const S: PChar; Len : integer; separator : char = #0): TDateTime;
  119. function StrToDate(const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
  120. function StrToDateTime(const S: string): TDateTime;
  121. function StrToDateTime(const s: ShortString; const FormatSettings : TFormatSettings): TDateTime;
  122. function StrToDateTime(const s: AnsiString; const FormatSettings : TFormatSettings): TDateTime;
  123. function FormatDateTime(const FormatStr: string; DateTime: TDateTime):string;
  124. function FormatDateTime(const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings): string;
  125. procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
  126. procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime; const FormatSettings: TFormatSettings);
  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;
  161. function GetLocalTimeOffset: Integer;