1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
{*******************************************************}
{ }
{ Add FastReport Date Lbrary }
{ }
{ Copyright (c) 1995, 1996 AO ROSNO }
{ Copyright (c) 1997, 1998 Master-Bank }
{ }
{ Copyright (c) 2001 by Stalker SoftWare }
{ }
{*******************************************************}
unit frFuncDate;
interface
{$B-,V-,R-,Q-,H+}
{.$I FR.inc}
uses
SysUtils;
// RxLib
function frDaysPerMonth(nYear, nMonth: Integer): Integer;
function frFirstDayOfNextMonth(dDate:TDateTime): TDateTime;
function frFirstDayOfPrevMonth(dDate:TDateTime): TDateTime;
function frLastDayOfPrevMonth(dDate:TDateTime): TDateTime;
function frIncYear(dDate: TDateTime; nDelta: Integer): TDateTime;
function frIncDay(dDate: TDateTime; nDelta: Integer): TDateTime;
function frIncDate(dDate: TDateTime; nDays, nMonths, nYears: Integer): TDateTime;
function frIncDateEx(dDate: TDateTime; cDelta :String) :TDateTime;
function frIncTimeEx(dTime: TDateTime; cDelta :String): TDateTime;
procedure frDateDiffEx(dDate1, dDate2: TDateTime; var cDelta :String);
// StLib
function frIsRangeDate(dBegDate, dEndDate, dDate: TDateTime) :Boolean;
function frStrToDateDef(cDate: String; dDefault: TDateTime): TDateTime;
function frValidDate(cDate :String) :Boolean;
function frIsLeapYear(AYear: Integer): Boolean;
function frIncMonth(dDate: TDateTime; nDelta: Integer): TDateTime;
implementation
uses
frFuncStr;
function frIsLeapYear(AYear: Integer): Boolean;
begin
Result := (AYear mod 4 = 0) and ((AYear mod 100 <> 0) or (AYear mod 400 = 0));
end;
{--------------------------------------------------------------------}
{ Function returns days per month for specified year. }
{ nYear - year, nMonth - month }
{--------------------------------------------------------------------}
function frDaysPerMonth(nYear, nMonth: Integer): Integer;
const
DaysInMonth: array[1..12] of Integer =
(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
Result := DaysInMonth[nMonth];
if (nMonth = 2) and frIsLeapYear(nYear) then Inc(Result); // leap-year Feb is special
end; { frDaysPerMonth }
{--------------------------------------------------------------------}
{ Function return first day of the next month from date dDate }
{ in TDateTime format. }
{--------------------------------------------------------------------}
function frFirstDayOfNextMonth(dDate:TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(dDate, Year, Month, Day);
Day := 1;
if Month < 12 then
Inc(Month)
else begin
Inc(Year);
Month := 1;
end; { if }
Result := EncodeDate(Year, Month, Day);
end; { frFirstDayOfNextMonth }
{--------------------------------------------------------------------}
{ Function return first day of the previous month from date dDate }
{ in TDateTime format. }
{--------------------------------------------------------------------}
function frFirstDayOfPrevMonth(dDate:TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(dDate, Year, Month, Day);
Day := 1;
if Month > 1 then
Dec(Month)
else begin
Dec(Year);
Month := 12;
end;
Result := EncodeDate(Year, Month, Day);
end; { frFirstDayOfPrevMonth }
{--------------------------------------------------------------------}
{ Function return last day of the previous month from date dDate }
{ in TDateTime format. }
{--------------------------------------------------------------------}
function frLastDayOfPrevMonth(dDate:TDateTime): TDateTime;
var
D: TDateTime;
Year, Month, Day: Word;
begin
D := frFirstDayOfPrevMonth(dDate);
DecodeDate(D, Year, Month, Day);
Day := frDaysPerMonth(Year, Month);
Result := EncodeDate(Year, Month, Day);
end; { frLastDayOfPrevMonth }
{--------------------------------------------------------------------}
{ Increase date dDate on specified count days, months, years, }
{ returns result date. }
{--------------------------------------------------------------------}
function frIncDate(dDate: TDateTime; nDays, nMonths, nYears: Integer): TDateTime;
var
D, M, Y: Word;
Day, Month, Year: LongInt;
begin
DecodeDate(dDate, Y, M, D);
Year := Y; Month := M; Day := D;
Inc(Year, nYears);
Inc(Year, nMonths div 12);
Inc(Month, nMonths mod 12);
if Month < 1 then begin
Inc(Month, 12);
Dec(Year);
end
else
if Month > 12 then begin
Dec(Month, 12);
Inc(Year);
end; { if }
if Day > frDaysPerMonth(Year, Month) then Day := frDaysPerMonth(Year, Month);
Result := EncodeDate(Year, Month, Day) + nDays + Frac(dDate);
end; { frIncDate }
{--------------------------------------------------------------------}
{ Increase date dDate on specified count days nDelta }
{--------------------------------------------------------------------}
function frIncDay(dDate: TDateTime; nDelta: Integer): TDateTime;
begin
Result := dDate + nDelta;
end; { frIncDay }
{--------------------------------------------------------------------}
{ Increase date dDate on specified count years nDelta }
{--------------------------------------------------------------------}
function frIncYear(dDate: TDateTime; nDelta: Integer): TDateTime;
begin
Result := frIncDate(dDate, 0, 0, nDelta);
end; { frIncYear }
{--------------------------------------------------------------------}
{ Increase date dDate on specified count months nDelta }
{--------------------------------------------------------------------}
function frIncMonth(dDate: TDateTime; nDelta: Integer): TDateTime;
begin
Result := frIncDate(dDate, 0, nDelta, 0);
end; { frIncYear }
{--------------------------------------------------------------------}
{ Detect difference before dates Date1 and Date2 in days, }
{ months, years. }
{--------------------------------------------------------------------}
procedure frDateDiffEx(dDate1, dDate2: TDateTime; var cDelta :String);
{ Corrected by Anatoly A. Sanko (2:450/73) }
var
DtSwap: TDateTime;
Day1, Day2, Month1, Month2, Year1, Year2: Word;
Days, Months, Years: Word;
begin
if dDate1 > dDate2 then begin
DtSwap := dDate1;
dDate1 := dDate2;
dDate2 := DtSwap;
end;
DecodeDate(dDate1, Year1, Month1, Day1);
DecodeDate(dDate2, Year2, Month2, Day2);
Years := Year2 - Year1;
Months := 0;
Days := 0;
if Month2 < Month1 then begin
Inc(Months, 12);
Dec(Years);
end;
Inc(Months, Month2 - Month1);
if Day2 < Day1 then begin
Inc(Days, frDaysPerMonth(Year1, Month1));
if Months = 0 then begin
Dec(Years);
Months := 11;
end
else Dec(Months);
end;
Inc(Days, Day2 - Day1);
// Compile string for result
cDelta := IntToStr(Days)+';'+IntToStr(Months)+';'+IntToStr(Years);
end; { frDateDiffEx }
{----------------------------------------------------------------}
{ Return True if specified date be in given range }
{ vBegDate - Begin range }
{ vEndDate - End range }
{ vDate - Checked date }
{----------------------------------------------------------------}
function frIsRangeDate(dBegDate, dEndDate, dDate: TDateTime) :Boolean;
begin
if (dDate >= dBegDate) and (dDate <= dEndDate) then
Result := True
else
Result := False
end; { frIsDiapDate }
{----------------------------------------------------------------}
{ Convert string into date. }
{----------------------------------------------------------------}
function frStrToDateDef(cDate: String; dDefault: TDateTime): TDateTime;
begin
try
Result := StrToDate(cDate)
except
Result := dDefault;
end; { try }
end; { frStrToDateDef }
{--------------------------------------------------------------------}
{ Return True, if cDate really date }
{--------------------------------------------------------------------}
function frValidDate(cDate :String) :Boolean;
begin
Result := True;
try
StrToDate(cDate)
except
Result := False;
end; { try }
end; { frValidDate }
{--------------------------------------------------------------------}
{ Increase date dDate on specified count days, months, years, }
{ extracting them from string cDelta and return this date }
{ as result. }
{--------------------------------------------------------------------}
function frIncDateEx(dDate: TDateTime; cDelta :String) :TDateTime;
var
nDay, nMonth, nYear: LongInt;
begin
// Split string on parts
nDay := StrToInt(frExtractWord(1,cDelta,[';']));
nMonth := StrToInt(frExtractWord(2,cDelta,[';']));
nYear := StrToInt(frExtractWord(3,cDelta,[';']));
Result := frIncDate(dDate, nDay, nMonth, nYear);
end; { frIncDateEx }
{--------------------------------------------------------------------}
{ Increase time ATime on specified count hours, minuts, seconds, }
{ and milliseconds, extracting his from string cDelta }
{--------------------------------------------------------------------}
function frIncTimeEx(dTime: TDateTime; cDelta :String): TDateTime;
var
nHours, nMinutes, nSeconds, nMSecs: Integer;
begin
// Split string on parts
nHours := StrToInt(frExtractWord(1,cDelta,[';']));
nMinutes := StrToInt(frExtractWord(2,cDelta,[';']));
nSeconds := StrToInt(frExtractWord(3,cDelta,[';']));
nMSecs := StrToInt(frExtractWord(4,cDelta,[';']));
Result := dTime + (nHours div 24) + (((nHours mod 24) * 3600000 +
nMinutes * 60000 + nSeconds * 1000 + nMSecs) / MSecsPerDay);
if Result < 0 then Result := Result + 1;
end; { frIncTimeEx }
end.
|