4
$Id: dateutil.inc,v 1.2 2004/01/10 19:35:17 michael Exp $
5
This file is part of the Free Pascal run time library.
6
Copyright (c) 1999-2000 by the Free Pascal development team
8
Delphi/Kylix compatibility unit, provides Date/Time handling routines.
10
See the file COPYING.FPC, included in this distribution,
11
for details about the copyright.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
**********************************************************************}
24
SysUtils, Math, Types;
26
{ ---------------------------------------------------------------------
28
---------------------------------------------------------------------}
32
WeeksPerFortnight = 2;
35
YearsPerCentury = 100;
36
YearsPerMillennium = 1000;
48
OneHour = 1/HoursPerDay;
49
OneMinute = 1/MinsPerDay;
50
OneSecond = 1/SecsPerDay;
51
OneMillisecond = 1/MSecsPerDay;
53
{ This is actual days per year but you need to know if it's a leap year}
54
DaysPerYear: array [Boolean] of Word = (365, 366);
56
{ Used in RecodeDate, RecodeTime and RecodeDateTime for those datetime }
57
{ fields you want to leave alone }
58
RecodeLeaveFieldAsIs = High(Word);
60
{ ---------------------------------------------------------------------
61
Global variables used in this unit
62
---------------------------------------------------------------------}
66
{ Average over a 4 year span. Valid for next 100 years }
67
ApproxDaysPerMonth: Double = 30.4375;
68
ApproxDaysPerYear: Double = 365.25;
72
{ ---------------------------------------------------------------------
73
Simple trimming functions.
74
---------------------------------------------------------------------}
76
Function DateOf(const AValue: TDateTime): TDateTime;
77
Function TimeOf(const AValue: TDateTime): TDateTime;
79
{ ---------------------------------------------------------------------
80
Identification functions.
81
---------------------------------------------------------------------}
83
Function IsInLeapYear(const AValue: TDateTime): Boolean;
84
Function IsPM(const AValue: TDateTime): Boolean;
85
Function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
86
Function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
87
Function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
88
Function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
89
Function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean;
90
Function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean;
92
{ ---------------------------------------------------------------------
93
Enumeration functions.
94
---------------------------------------------------------------------}
96
Function WeeksInYear(const AValue: TDateTime): Word;
97
Function WeeksInAYear(const AYear: Word): Word;
98
Function DaysInYear(const AValue: TDateTime): Word;
99
Function DaysInAYear(const AYear: Word): Word;
100
Function DaysInMonth(const AValue: TDateTime): Word;
101
Function DaysInAMonth(const AYear, AMonth: Word): Word;
104
{ ---------------------------------------------------------------------
105
Variations on current date/time.
106
---------------------------------------------------------------------}
109
Function Today: TDateTime;
110
Function Yesterday: TDateTime;
111
Function Tomorrow: TDateTime;
112
Function IsToday(const AValue: TDateTime): Boolean;
113
Function IsSameDay(const AValue, ABasis: TDateTime): Boolean;
114
Function PreviousDayOfWeek (DayOfWeek : Word) : Word;
116
{ ---------------------------------------------------------------------
117
Extraction functions.
118
---------------------------------------------------------------------}
120
Function YearOf(const AValue: TDateTime): Word;
121
Function MonthOf(const AValue: TDateTime): Word;
122
Function WeekOf(const AValue: TDateTime): Word;
123
Function DayOf(const AValue: TDateTime): Word;
124
Function HourOf(const AValue: TDateTime): Word;
125
Function MinuteOf(const AValue: TDateTime): Word;
126
Function SecondOf(const AValue: TDateTime): Word;
127
Function MilliSecondOf(const AValue: TDateTime): Word;
129
{ ---------------------------------------------------------------------
130
Start/End of year functions.
131
---------------------------------------------------------------------}
133
Function StartOfTheYear(const AValue: TDateTime): TDateTime;
134
Function EndOfTheYear(const AValue: TDateTime): TDateTime;
135
Function StartOfAYear(const AYear: Word): TDateTime;
136
Function EndOfAYear(const AYear: Word): TDateTime;
138
{ ---------------------------------------------------------------------
139
Start/End of month functions.
140
---------------------------------------------------------------------}
142
Function StartOfTheMonth(const AValue: TDateTime): TDateTime;
143
Function EndOfTheMonth(const AValue: TDateTime): TDateTime;
144
Function StartOfAMonth(const AYear, AMonth: Word): TDateTime;
145
Function EndOfAMonth(const AYear, AMonth: Word): TDateTime;
147
{ ---------------------------------------------------------------------
148
Start/End of week functions.
149
---------------------------------------------------------------------}
152
Function StartOfTheWeek(const AValue: TDateTime): TDateTime;
153
Function EndOfTheWeek(const AValue: TDateTime): TDateTime;
154
Function StartOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
155
Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // ADayOFWeek 1
156
Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
157
Function EndOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // const ADayOfWeek: Word = 7
160
{ ---------------------------------------------------------------------
161
Start/End of day functions.
162
---------------------------------------------------------------------}
164
Function StartOfTheDay(const AValue: TDateTime): TDateTime;
165
Function EndOfTheDay(const AValue: TDateTime): TDateTime;
166
Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime; overload;
167
Function StartOfADay(const AYear, ADayOfYear: Word): TDateTime; overload;
168
Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime; overload;
169
Function EndOfADay(const AYear, ADayOfYear: Word): TDateTime; overload;
171
{ ---------------------------------------------------------------------
172
Part of year functions.
173
---------------------------------------------------------------------}
175
Function MonthOfTheYear(const AValue: TDateTime): Word;
176
Function WeekOfTheYear(const AValue: TDateTime): Word; overload;
177
Function WeekOfTheYear(const AValue: TDateTime; var AYear: Word): Word; overload;
178
Function DayOfTheYear(const AValue: TDateTime): Word;
179
Function HourOfTheYear(const AValue: TDateTime): Word;
180
Function MinuteOfTheYear(const AValue: TDateTime): LongWord;
181
Function SecondOfTheYear(const AValue: TDateTime): LongWord;
182
Function MilliSecondOfTheYear(const AValue: TDateTime): Int64;
184
{ ---------------------------------------------------------------------
185
Part of month functions.
186
---------------------------------------------------------------------}
188
Function WeekOfTheMonth(const AValue: TDateTime): Word; overload;
189
Function WeekOfTheMonth(const AValue: TDateTime; var AYear, AMonth: Word): Word; overload;
190
Function DayOfTheMonth(const AValue: TDateTime): Word;
191
Function HourOfTheMonth(const AValue: TDateTime): Word;
192
Function MinuteOfTheMonth(const AValue: TDateTime): Word;
193
Function SecondOfTheMonth(const AValue: TDateTime): LongWord;
194
Function MilliSecondOfTheMonth(const AValue: TDateTime): LongWord;
196
{ ---------------------------------------------------------------------
197
Part of week functions.
198
---------------------------------------------------------------------}
200
Function DayOfTheWeek(const AValue: TDateTime): Word;
201
Function HourOfTheWeek(const AValue: TDateTime): Word;
202
Function MinuteOfTheWeek(const AValue: TDateTime): Word;
203
Function SecondOfTheWeek(const AValue: TDateTime): LongWord;
204
Function MilliSecondOfTheWeek(const AValue: TDateTime): LongWord;
206
{ ---------------------------------------------------------------------
207
Part of day functions.
208
---------------------------------------------------------------------}
210
Function HourOfTheDay(const AValue: TDateTime): Word;
211
Function MinuteOfTheDay(const AValue: TDateTime): Word;
212
Function SecondOfTheDay(const AValue: TDateTime): LongWord;
213
Function MilliSecondOfTheDay(const AValue: TDateTime): LongWord;
215
{ ---------------------------------------------------------------------
216
Part of hour functions.
217
---------------------------------------------------------------------}
219
Function MinuteOfTheHour(const AValue: TDateTime): Word;
220
Function SecondOfTheHour(const AValue: TDateTime): Word;
221
Function MilliSecondOfTheHour(const AValue: TDateTime): LongWord;
223
{ ---------------------------------------------------------------------
224
Part of minute functions.
225
---------------------------------------------------------------------}
228
Function SecondOfTheMinute(const AValue: TDateTime): Word;
229
Function MilliSecondOfTheMinute(const AValue: TDateTime): LongWord;
231
{ ---------------------------------------------------------------------
232
Part of second functions.
233
---------------------------------------------------------------------}
235
Function MilliSecondOfTheSecond(const AValue: TDateTime): Word;
238
{ ---------------------------------------------------------------------
239
Range checking functions.
240
---------------------------------------------------------------------}
242
Function WithinPastYears(const ANow, AThen: TDateTime; const AYears: Integer): Boolean;
243
Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean;
244
Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean;
245
Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean;
246
Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean;
247
Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean;
248
Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean;
249
Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean;
251
{ ---------------------------------------------------------------------
253
---------------------------------------------------------------------}
255
Function YearsBetween(const ANow, AThen: TDateTime): Integer;
256
Function MonthsBetween(const ANow, AThen: TDateTime): Integer;
257
Function WeeksBetween(const ANow, AThen: TDateTime): Integer;
258
Function DaysBetween(const ANow, AThen: TDateTime): Integer;
259
Function HoursBetween(const ANow, AThen: TDateTime): Int64;
260
Function MinutesBetween(const ANow, AThen: TDateTime): Int64;
261
Function SecondsBetween(const ANow, AThen: TDateTime): Int64;
262
Function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;
264
{ ---------------------------------------------------------------------
265
Timespan in xxx functions.
266
---------------------------------------------------------------------}
268
{ YearSpan and MonthSpan are approximate values }
269
Function YearSpan(const ANow, AThen: TDateTime): Double;
270
Function MonthSpan(const ANow, AThen: TDateTime): Double;
271
Function WeekSpan(const ANow, AThen: TDateTime): Double;
272
Function DaySpan(const ANow, AThen: TDateTime): Double;
273
Function HourSpan(const ANow, AThen: TDateTime): Double;
274
Function MinuteSpan(const ANow, AThen: TDateTime): Double;
275
Function SecondSpan(const ANow, AThen: TDateTime): Double;
276
Function MilliSecondSpan(const ANow, AThen: TDateTime): Double;
278
{ ---------------------------------------------------------------------
279
Increment/decrement functions.
280
---------------------------------------------------------------------}
282
Function IncYear(const AValue: TDateTime; const ANumberOfYears: Integer ): TDateTime;
283
Function IncYear(const AValue: TDateTime): TDateTime; // ; const ANumberOfYears: Integer = 1)
284
// Function IncMonth is in SysUtils
285
Function IncWeek(const AValue: TDateTime; const ANumberOfWeeks: Integer): TDateTime;
286
Function IncWeek(const AValue: TDateTime): TDateTime; // ; const ANumberOfWeeks: Integer = 1)
287
Function IncDay(const AValue: TDateTime; const ANumberOfDays: Integer): TDateTime;
288
Function IncDay(const AValue: TDateTime): TDateTime; //; const ANumberOfDays: Integer = 1)
289
Function IncHour(const AValue: TDateTime; const ANumberOfHours: Int64): TDateTime;
290
Function IncHour(const AValue: TDateTime): TDateTime; //; const ANumberOfHours: Int64 = 1
291
Function IncMinute(const AValue: TDateTime; const ANumberOfMinutes: Int64): TDateTime;
292
Function IncMinute(const AValue: TDateTime): TDateTime; // ; const ANumberOfMinutes: Int64 = 1
293
Function IncSecond(const AValue: TDateTime; const ANumberOfSeconds: Int64): TDateTime;
294
Function IncSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfSeconds: Int64 = 1
295
Function IncMilliSecond(const AValue: TDateTime; const ANumberOfMilliSeconds: Int64): TDateTime;
296
Function IncMilliSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfMilliSeconds: Int64 = 1
298
{ ---------------------------------------------------------------------
299
Encode/Decode of complete timestamp
300
---------------------------------------------------------------------}
302
Function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
303
Procedure DecodeDateTime(const AValue: TDateTime; var AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word);
304
Function TryEncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AValue: TDateTime): Boolean;
306
{ ---------------------------------------------------------------------
307
Encode/decode date, specifying week of year and day of week
308
---------------------------------------------------------------------}
310
Function EncodeDateWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
311
Function EncodeDateWeek(const AYear, AWeekOfYear: Word): TDateTime; //; const ADayOfWeek: Word = 1
312
Procedure DecodeDateWeek(const AValue: TDateTime; var AYear, AWeekOfYear, ADayOfWeek: Word);
313
Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime; const ADayOfWeek: Word): Boolean;
314
Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime): Boolean; //; const ADayOfWeek: Word = 1
316
{ ---------------------------------------------------------------------
317
Encode/decode date, specifying day of year
318
---------------------------------------------------------------------}
320
Function EncodeDateDay(const AYear, ADayOfYear: Word): TDateTime;
321
Procedure DecodeDateDay(const AValue: TDateTime; var AYear, ADayOfYear: Word);
322
Function TryEncodeDateDay(const AYear, ADayOfYear: Word; var AValue: TDateTime): Boolean;
324
{ ---------------------------------------------------------------------
325
Encode/decode date, specifying week of month
326
---------------------------------------------------------------------}
328
Function EncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): TDateTime;
329
Procedure DecodeDateMonthWeek(const AValue: TDateTime; var AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
330
Function TryEncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word; var AValue: TDateTime): Boolean;
332
{ ---------------------------------------------------------------------
333
Replace given element with supplied value.
334
---------------------------------------------------------------------}
336
Function RecodeYear(const AValue: TDateTime; const AYear: Word): TDateTime;
337
Function RecodeMonth(const AValue: TDateTime; const AMonth: Word): TDateTime;
338
Function RecodeDay(const AValue: TDateTime; const ADay: Word): TDateTime;
339
Function RecodeHour(const AValue: TDateTime; const AHour: Word): TDateTime;
340
Function RecodeMinute(const AValue: TDateTime; const AMinute: Word): TDateTime;
341
Function RecodeSecond(const AValue: TDateTime; const ASecond: Word): TDateTime;
342
Function RecodeMilliSecond(const AValue: TDateTime; const AMilliSecond: Word): TDateTime;
343
Function RecodeDate(const AValue: TDateTime; const AYear, AMonth, ADay: Word): TDateTime;
344
Function RecodeTime(const AValue: TDateTime; const AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
345
Function RecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
346
Function TryRecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AResult: TDateTime): Boolean;
348
{ ---------------------------------------------------------------------
349
Comparision of date/time
350
---------------------------------------------------------------------}
352
Function CompareDateTime(const A, B: TDateTime): TValueRelationship;
353
Function CompareDate(const A, B: TDateTime): TValueRelationship;
354
Function CompareTime(const A, B: TDateTime): TValueRelationship;
355
Function SameDateTime(const A, B: TDateTime): Boolean;
356
Function SameDate(const A, B: TDateTime): Boolean;
357
Function SameTime(const A, B: TDateTime): Boolean;
359
{ For a given date these Functions tell you the which day of the week of the
360
month (or year). If its a Thursday, they will tell you if its the first,
361
second, etc Thursday of the month (or year). Remember, even though its
362
the first Thursday of the year it doesn't mean its the first week of the
363
year. See ISO 8601 above for more information. }
365
Function NthDayOfWeek(const AValue: TDateTime): Word;
367
Procedure DecodeDayOfWeekInMonth(const AValue: TDateTime; var AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
369
Function EncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word): TDateTime;
370
Function TryEncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word; var AValue: TDateTime): Boolean;
372
{ ---------------------------------------------------------------------
373
Exception throwing routines
374
---------------------------------------------------------------------}
376
Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; const ABaseDate: TDateTime);
377
Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word); // const ABaseDate: TDateTime = 0
378
Procedure InvalidDateWeekError(const AYear, AWeekOfYear, ADayOfWeek: Word);
379
Procedure InvalidDateDayError(const AYear, ADayOfYear: Word);
380
Procedure InvalidDateMonthWeekError(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
381
Procedure InvalidDayOfWeekInMonthError(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
383
{ ---------------------------------------------------------------------
384
Julian and Modified Julian Date conversion support
385
---------------------------------------------------------------------}
387
Function DateTimeToJulianDate(const AValue: TDateTime): Double;
388
Function JulianDateToDateTime(const AValue: Double): TDateTime;
389
Function TryJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;
391
Function DateTimeToModifiedJulianDate(const AValue: TDateTime): Double;
392
Function ModifiedJulianDateToDateTime(const AValue: Double): TDateTime;
393
Function TryModifiedJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;
395
{ ---------------------------------------------------------------------
396
Unix timestamp support.
397
---------------------------------------------------------------------}
399
Function DateTimeToUnix(const AValue: TDateTime): Int64;
400
Function UnixToDateTime(const AValue: Int64): TDateTime;
406
{ ---------------------------------------------------------------------
408
---------------------------------------------------------------------}
410
Procedure NotYetImplemented (FN : String);
413
Raise Exception.CreateFmt('Function "%s" (dateutils) is not yet implemented',[FN]);
416
{ ---------------------------------------------------------------------
417
Simple trimming functions.
418
---------------------------------------------------------------------}
420
Function DateOf(const AValue: TDateTime): TDateTime;
422
Result:=Trunc(DateOf);
426
Function TimeOf(const AValue: TDateTime): TDateTime;
428
Result:=Frac(Avalue);
432
{ ---------------------------------------------------------------------
433
Identification functions.
434
---------------------------------------------------------------------}
437
Function IsInLeapYear(const AValue: TDateTime): Boolean;
443
DecodeDate(AValue,Y,M,D);
444
Result:=IsLeapYear(Y);
448
Function IsPM(const AValue: TDateTime): Boolean;
454
DecodeTime(AValue,H,M,S,MS);
459
Function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
461
Result:=(AYear<>0) and (AYear<10000)
462
and (AMonth in [1..12])
463
and (ADay<>0) and (ADay<=MonthDays[IsleapYear(AYear),AMonth]);
467
Function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
469
Result:=(AHour=HoursPerDay) and (AMinute=0) and (ASecond=0) and (AMillisecond=0);
471
((AHour<HoursPerDay) and (AMinute<MinsPerHour) and (ASecond<SecsPerMin) and
472
(AMillisecond<MSecsPerSec));
476
Function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
478
Result:=IsValidDate(AYear,AMonth,ADay) and
479
IsValidTime(AHour,AMinute,ASecond,AMillisecond)
483
Function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
485
Result:=(AYear<>0) and (ADayOfYear<>0) and (AYear<10000) and
486
(ADayOfYear<=DaysPerYear[IsLeapYear(AYear)]);
490
Function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean;
492
Result:=(AYear<>0) and (AYear<10000)
493
and (ADayOfWeek in [1..7])
495
and (AWeekOfYear<=WeeksInaYear(AYear));
496
{ should we not also check whether the day of the week is not
497
larger than the last day of the last week in the year 9999 ?? }
501
Function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean;
504
Result:=(AYear<>0) and (AYear<10000)
505
and (AMonth in [1..12])
506
and (AWeekOfMonth in [1..5])
507
and (ADayOfWeek in [1..7]);
510
{ ---------------------------------------------------------------------
511
Enumeration functions.
512
---------------------------------------------------------------------}
514
Function WeeksInYear(const AValue: TDateTime): Word;
520
DecodeDate(AValue,Y,M,D);
521
Result:=WeeksInAYear(Y);
525
Function WeeksInAYear(const AYear: Word): Word;
532
DOW:=DayOfTheWeek(StartOfAYear(AYear));
533
If (DOW=4) or ((DOW=3) and IsLeapYear(AYear)) then
538
Function DaysInYear(const AValue: TDateTime): Word;
544
DecodeDate(AValue,Y,M,D);
545
Result:=DaysPerYear[IsLeapYear(Y)];
549
Function DaysInAYear(const AYear: Word): Word;
551
Result:=DaysPerYear[Isleapyear(AYear)];
555
Function DaysInMonth(const AValue: TDateTime): Word;
561
Decodedate(AValue,Y,M,D);
562
Result:=MonthDays[IsLeapYear(Y),M];
566
Function DaysInAMonth(const AYear, AMonth: Word): Word;
568
Result:=MonthDays[IsLeapYear(AYear),AMonth];
572
{ ---------------------------------------------------------------------
573
Variations on current date/time.
574
---------------------------------------------------------------------}
577
Function Today: TDateTime;
583
Function Yesterday: TDateTime;
589
Function Tomorrow: TDateTime;
595
Function IsToday(const AValue: TDateTime): Boolean;
597
Result:=IsSameDay(AValue,Date);
601
Function IsSameDay(const AValue, ABasis: TDateTime): Boolean;
607
D:=AValue-Trunc(ABasis);
608
Result:=(D>=0) and (D<1);
612
DOWMap: array [1..7] of Word = (7, 1, 2, 3, 4, 5, 6);
614
Function PreviousDayOfWeek (DayOfWeek : Word) : Word;
617
If Not (DayOfWeek in [1..7]) then
618
Raise EConvertError.CreateFmt(SErrInvalidDayOfWeek,[DayOfWeek]);
619
Result:=DOWMap[DayOfWeek];
624
{ ---------------------------------------------------------------------
625
Extraction functions.
626
---------------------------------------------------------------------}
629
Function YearOf(const AValue: TDateTime): Word;
635
DecodeDate(AValue,Result,D,M);
639
Function MonthOf(const AValue: TDateTime): Word;
645
DecodeDate(AValue,Y,Result,D);
649
Function WeekOf(const AValue: TDateTime): Word;
651
Result:=WeekOfTheYear(AValue);
655
Function DayOf(const AValue: TDateTime): Word;
661
DecodeDate(AValue,Y,M,Result);
665
Function HourOf(const AValue: TDateTime): Word;
671
DecodeTime(AValue,Result,N,S,MS);
675
Function MinuteOf(const AValue: TDateTime): Word;
681
DecodeTime(AValue,H,Result,S,MS);
685
Function SecondOf(const AValue: TDateTime): Word;
691
DecodeTime(AVAlue,H,N,Result,MS);
695
Function MilliSecondOf(const AValue: TDateTime): Word;
701
DecodeTime(AValue,H,N,S,Result);
705
{ ---------------------------------------------------------------------
706
Start/End of year functions.
707
---------------------------------------------------------------------}
710
Function StartOfTheYear(const AValue: TDateTime): TDateTime;
716
DecodeDate(AValue,Y,M,D);
717
Result:=EncodeDate(Y,1,1);
721
Function EndOfTheYear(const AValue: TDateTime): TDateTime;
727
DecodeDate(AValue,Y,M,D);
728
Result:=EncodeDateTime(Y,12,31,23,59,59,999);
732
Function StartOfAYear(const AYear: Word): TDateTime;
734
Result:=EncodeDate(AYear,1,1);
738
Function EndOfAYear(const AYear: Word): TDateTime;
741
Result:=(EncodeDateTime(AYear,12,31,23,59,59,999));
744
{ ---------------------------------------------------------------------
745
Start/End of month functions.
746
---------------------------------------------------------------------}
748
Function StartOfTheMonth(const AValue: TDateTime): TDateTime;
754
DecodeDate(AValue,Y,M,D);
755
Result:=EncodeDate(Y,M,1);
756
// MonthDays[IsLeapYear(Y),M])
760
Function EndOfTheMonth(const AValue: TDateTime): TDateTime;
766
DecodeDate(AValue,Y,M,D);
767
Result:=EncodeDateTime(Y,M,MonthDays[IsLeapYear(Y),M],23,59,59,999);
771
Function StartOfAMonth(const AYear, AMonth: Word): TDateTime;
773
Result:=EncodeDate(AYear,AMonth,1);
777
Function EndOfAMonth(const AYear, AMonth: Word): TDateTime;
780
Result:=EncodeDateTime(AYear,AMonth,MonthDays[IsLeapYear(AYear),AMonth],23,59,59,999);
784
{ ---------------------------------------------------------------------
785
Start/End of week functions.
786
---------------------------------------------------------------------}
789
Function StartOfTheWeek(const AValue: TDateTime): TDateTime;
791
Result:=Trunc(AValue)-DayOfTheWeek(AValue)+1;
795
Function EndOfTheWeek(const AValue: TDateTime): TDateTime;
797
Result:=EndOfTheDay(AValue-DayOfTheWeek(AValue)+7);
801
Function StartOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
803
Result:=EncodeDateWeek(AYear,AWeekOfYear,ADayOfWeek);
807
Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // ADayOFWeek 1
809
Result:=StartOfAWeek(AYear,AWeekOfYear,1)
813
Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
815
Result := EndOfTheDay(EncodeDateWeek(AYear, AWeekOfYear, ADayOfWeek));
819
Function EndOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // const ADayOfWeek: Word = 7
823
Result:=EndOfAWeek(AYear,AWeekOfYear,7);
826
{ ---------------------------------------------------------------------
827
Start/End of day functions.
828
---------------------------------------------------------------------}
830
Function StartOfTheDay(const AValue: TDateTime): TDateTime;
832
StartOfTheDay:=Trunc(Avalue);
836
Function EndOfTheDay(const AValue: TDateTime): TDateTime;
842
DecodeDate(AValue,Y,M,D);
843
Result:=EncodeDateTime(Y,M,D,23,59,59,999);
847
Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime;
849
Result:=EncodeDate(AYear,AMonth,ADay);
853
Function StartOfADay(const AYear, ADayOfYear: Word): TDateTime;
855
Result:=StartOfAYear(AYear)+ADayOfYear;
859
Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime;
861
Result:=EndOfTheDay(EncodeDate(AYear,AMonth,ADay));
865
Function EndOfADay(const AYear, ADayOfYear: Word): TDateTime;
869
Result:=StartOfAYear(AYear)+ADayOfYear+EncodeTime(23,59,59,999);
872
{ ---------------------------------------------------------------------
873
Part of year functions.
874
---------------------------------------------------------------------}
877
Function MonthOfTheYear(const AValue: TDateTime): Word;
883
DecodeDate(AValue,Y,Result,D);
887
Function WeekOfTheYear(const AValue: TDateTime): Word;
893
DecodeDateWeek(AValue,Y,Result,DOW)
897
Function WeekOfTheYear(const AValue: TDateTime; var AYear: Word): Word;
903
DecodeDateWeek(AValue,AYear,Result,DOW);
907
Function DayOfTheYear(const AValue: TDateTime): Word;
909
Result:=Trunc(AValue-StartOfTheYear(AValue)+1);
913
Function HourOfTheYear(const AValue: TDateTime): Word;
919
DecodeTime(AValue,H,M,S,MS);
920
Result:=H+((DayOfTheYear(AValue)-1)*24);
924
Function MinuteOfTheYear(const AValue: TDateTime): LongWord;
930
DecodeTime(AValue,H,M,S,MS);
931
Result:=M+(H+((DayOfTheYear(AValue)-1)*24))*60;
935
Function SecondOfTheYear(const AValue: TDateTime): LongWord;
941
DecodeTime(AValue,H,M,S,MS);
942
Result:=(M+(H+((DayOfTheYear(AValue)-1)*24))*60)*60+S;
946
Function MilliSecondOfTheYear(const AValue: TDateTime): Int64;
952
DecodeTime(AValue,H,M,S,MS);
953
Result:=((M+(H+((DayOfTheYear(AValue)-1)*24))*60)*60+S)*1000+MS;
957
{ ---------------------------------------------------------------------
958
Part of month functions.
959
---------------------------------------------------------------------}
962
Function WeekOfTheMonth(const AValue: TDateTime): Word;
968
DecodeDateMonthWeek(AValue,Y,M,Result,DOW);
972
Function WeekOfTheMonth(const AValue: TDateTime; var AYear, AMonth: Word): Word;
978
DecodeDateMonthWeek(AValue,AYear,AMonth,Result,DOW);
982
Function DayOfTheMonth(const AValue: TDateTime): Word;
988
DecodeDate(AValue,Y,M,Result);
992
Function HourOfTheMonth(const AValue: TDateTime): Word;
995
Y,M,D,H,N,S,MS : Word;
998
DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
1003
Function MinuteOfTheMonth(const AValue: TDateTime): Word;
1006
Y,M,D,H,N,S,MS : Word;
1009
DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
1010
Result:=((D-1)*24+H)*60+N;
1014
Function SecondOfTheMonth(const AValue: TDateTime): LongWord;
1017
Y,M,D,H,N,S,MS : Word;
1020
DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
1021
Result:=(((D-1)*24+H)*60+N)*60+S;
1025
Function MilliSecondOfTheMonth(const AValue: TDateTime): LongWord;
1028
Y,M,D,H,N,S,MS : Word;
1031
DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
1032
Result:=((((D-1)*24+H)*60+N)*60+S)*1000+MS;
1035
{ ---------------------------------------------------------------------
1036
Part of week functions.
1037
---------------------------------------------------------------------}
1040
Function DayOfTheWeek(const AValue: TDateTime): Word;
1043
Result:=DowMAP[DayOfWeek(AValue)];
1047
Function HourOfTheWeek(const AValue: TDateTime): Word;
1053
DecodeTime(AValue,H,M,S,MS);
1054
Result:=(DayOfTheWeek(AValue)-1)*24+H;
1058
Function MinuteOfTheWeek(const AValue: TDateTime): Word;
1064
DecodeTime(AValue,H,M,S,MS);
1065
Result:=((DayOfTheWeek(AValue)-1)*24+H)*60+M;
1069
Function SecondOfTheWeek(const AValue: TDateTime): LongWord;
1075
DecodeTime(AValue,H,M,S,MS);
1076
Result:=(((DayOfTheWeek(AValue)-1)*24+H)*60+M)*60+S;
1080
Function MilliSecondOfTheWeek(const AValue: TDateTime): LongWord;
1087
DecodeTime(AValue,H,M,S,MS);
1088
Result:=((((DayOfTheWeek(AValue)-1)*24+H)*60+M)*60+S)*1000+MS;
1091
{ ---------------------------------------------------------------------
1092
Part of day functions.
1093
---------------------------------------------------------------------}
1096
Function HourOfTheDay(const AValue: TDateTime): Word;
1102
DecodeTime(AValue,Result,M,S,MS);
1106
Function MinuteOfTheDay(const AValue: TDateTime): Word;
1112
DecodeTime(AValue,H,M,S,MS);
1117
Function SecondOfTheDay(const AValue: TDateTime): LongWord;
1123
DecodeTime(AValue,H,M,S,MS);
1124
Result:=((H*60)+M)*60+S;
1128
Function MilliSecondOfTheDay(const AValue: TDateTime): LongWord;
1134
DecodeTime(AValue,H,M,S,MS);
1135
Result:=(((H*60)+M)*60+S)*1000+MS;
1138
{ ---------------------------------------------------------------------
1139
Part of hour functions.
1140
---------------------------------------------------------------------}
1143
Function MinuteOfTheHour(const AValue: TDateTime): Word;
1149
DecodeTime(AValue,H,Result,S,MS);
1153
Function SecondOfTheHour(const AValue: TDateTime): Word;
1159
DecodeTime(AValue,H,M,S,MS);
1164
Function MilliSecondOfTheHour(const AValue: TDateTime): LongWord;
1170
DecodeTime(AValue,H,M,S,MS);
1171
Result:=(M*60+S)*1000+MS;
1174
{ ---------------------------------------------------------------------
1175
Part of minute functions.
1176
---------------------------------------------------------------------}
1179
Function SecondOfTheMinute(const AValue: TDateTime): Word;
1185
DecodeTime(AValue,H,M,Result,MS);
1189
Function MilliSecondOfTheMinute(const AValue: TDateTime): LongWord;
1195
DecodeTime(AValue,H,M,S,MS);
1199
{ ---------------------------------------------------------------------
1200
Part of second functions.
1201
---------------------------------------------------------------------}
1203
Function MilliSecondOfTheSecond(const AValue: TDateTime): Word;
1209
DecodeTime(AValue,H,M,S,Result);
1212
{ ---------------------------------------------------------------------
1213
Range checking functions.
1214
---------------------------------------------------------------------}
1216
Function WithinPastYears(const ANow, AThen: TDateTime; const AYears: Integer): Boolean;
1218
Result:=YearsBetween(ANow,AThen)<=AYears;
1222
Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean;
1224
Result:=MonthsBetween(ANow,AThen)<=AMonths;
1228
Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean;
1230
Result:=WeeksBetween(ANow,AThen)<=AWeeks;
1234
Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean;
1236
Result:=DaysBetween(ANow,AThen)<=ADays;
1240
Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean;
1242
Result:=HoursBetween(ANow,AThen)<=AHours;
1246
Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean;
1248
Result:=MinutesBetween(ANow,AThen)<=AMinutes;
1252
Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean;
1254
Result:=SecondsBetween(ANow,Athen)<=ASeconds;
1258
Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean;
1260
Result:=MilliSecondsBetween(ANow,AThen)<=AMilliSeconds;
1264
{ ---------------------------------------------------------------------
1266
---------------------------------------------------------------------}
1269
These functions are declared as approximate by Borland.
1270
A bit strange, since it can be calculated exactly ?
1274
Function YearsBetween(const ANow, AThen: TDateTime): Integer;
1276
Result:=Trunc(Abs(ANow-AThen)/ApproxDaysPerYear);
1280
Function MonthsBetween(const ANow, AThen: TDateTime): Integer;
1282
Result:=Trunc(Abs(ANow-Athen)/ApproxDaysPerMonth);
1286
Function WeeksBetween(const ANow, AThen: TDateTime): Integer;
1288
Result:=Trunc(Abs(ANow-AThen)) div 7;
1292
Function DaysBetween(const ANow, AThen: TDateTime): Integer;
1294
Result:=Trunc(Abs(ANow-AThen));
1298
Function HoursBetween(const ANow, AThen: TDateTime): Int64;
1300
Result:=Trunc(Abs(ANow-AThen)*HoursPerDay);
1304
Function MinutesBetween(const ANow, AThen: TDateTime): Int64;
1306
Result:=Trunc(Abs(ANow-AThen)*MinsPerDay);
1310
Function SecondsBetween(const ANow, AThen: TDateTime): Int64;
1312
Result:=Trunc(Abs(ANow-AThen)*SecsPerDay);
1316
Function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;
1318
Result:=Trunc(Abs(ANow-AThen)*MSecsPerDay);
1322
{ ---------------------------------------------------------------------
1323
Timespan in xxx functions.
1324
---------------------------------------------------------------------}
1326
Function YearSpan(const ANow, AThen: TDateTime): Double;
1328
Result:=Abs(Anow-Athen)/ApproxDaysPerYear;
1332
Function MonthSpan(const ANow, AThen: TDateTime): Double;
1334
Result:=Abs(ANow-AThen)/ApproxDaysPerMonth;
1338
Function WeekSpan(const ANow, AThen: TDateTime): Double;
1340
Result:=Abs(ANow-AThen) / 7
1344
Function DaySpan(const ANow, AThen: TDateTime): Double;
1346
Result:=Abs(ANow-AThen);
1350
Function HourSpan(const ANow, AThen: TDateTime): Double;
1352
Result:=Abs(ANow-AThen)*HoursPerDay;
1356
Function MinuteSpan(const ANow, AThen: TDateTime): Double;
1358
Result:=Abs(ANow-AThen)*MinsPerDay;
1362
Function SecondSpan(const ANow, AThen: TDateTime): Double;
1364
Result:=Abs(ANow-AThen)*SecsPerDay;
1368
Function MilliSecondSpan(const ANow, AThen: TDateTime): Double;
1370
Result:=Abs(ANow-AThen)*MSecsPerDay;
1374
{ ---------------------------------------------------------------------
1375
Increment/decrement functions.
1376
---------------------------------------------------------------------}
1379
Function IncYear(const AValue: TDateTime; const ANumberOfYears: Integer ): TDateTime;
1382
Y,M,D,H,N,S,MS : Word;
1386
DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
1387
Y:=Y+ANumberOfYears;
1388
If (M=2) and (D=29) And (Not IsLeapYear(Y)) then
1390
Result:=EncodeDateTime(Y,M,D,H,N,S,MS);
1394
Function IncYear(const AValue: TDateTime): TDateTime; // ; const ANumberOfYears: Integer = 1)
1396
Result:=IncYear(Avalue,1);
1400
Function IncWeek(const AValue: TDateTime; const ANumberOfWeeks: Integer): TDateTime;
1402
Result:=AValue+ANumberOfWeeks*7;
1406
Function IncWeek(const AValue: TDateTime): TDateTime; // ; const ANumberOfWeeks: Integer = 1)
1408
Result:=IncWeek(Avalue,1);
1412
Function IncDay(const AValue: TDateTime; const ANumberOfDays: Integer): TDateTime;
1414
Result:=AValue+ANumberOfDays;
1418
Function IncDay(const AValue: TDateTime): TDateTime; //; const ANumberOfDays: Integer = 1)
1420
Result:=IncDay(Avalue,1);
1424
Function IncHour(const AValue: TDateTime; const ANumberOfHours: Int64): TDateTime;
1426
Result:=AValue+ANumberOfHours/HoursPerDay;
1430
Function IncHour(const AValue: TDateTime): TDateTime; //; const ANumberOfHours: Int64 = 1
1432
Result:=IncHour(AValue,1);
1436
Function IncMinute(const AValue: TDateTime; const ANumberOfMinutes: Int64): TDateTime;
1438
Result:=Result+ANumberOfMinutes / MinsPerDay;
1442
Function IncMinute(const AValue: TDateTime): TDateTime; // ; const ANumberOfMinutes: Int64 = 1
1444
Result:=IncMinute(AValue,1);
1448
Function IncSecond(const AValue: TDateTime; const ANumberOfSeconds: Int64): TDateTime;
1450
Result:=Result+ANumberOfSeconds / SecsPerDay;
1454
Function IncSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfSeconds: Int64 = 1
1456
Result:=IncSecond(Avalue,1);
1460
Function IncMilliSecond(const AValue: TDateTime; const ANumberOfMilliSeconds: Int64): TDateTime;
1462
Result:=Result+ANumberOfMilliSeconds/MSecsPerDay;
1466
Function IncMilliSecond(const AValue: TDateTime): TDateTime; // ; const ANumberOfMilliSeconds: Int64 = 1
1468
Result:=IncMilliSecond(AValue,1);
1472
{ ---------------------------------------------------------------------
1473
Encode/Decode of complete timestamp
1474
---------------------------------------------------------------------}
1477
Function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
1479
If Not TryEncodeDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond,Result) then
1480
InvalidDateTimeError(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond)
1484
Procedure DecodeDateTime(const AValue: TDateTime; var AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word);
1486
DecodeDate(AValue,AYear,AMonth,ADay);
1487
DecodeTime(AValue,AHour,AMinute,ASecond,AMilliSecond);
1491
Function TryEncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AValue: TDateTime): Boolean;
1497
Result:=TryEncodeDate(AYear,AMonth,ADay,AValue);
1498
Result:=Result and TryEncodeTime(AHour,AMinute,ASecond,Amillisecond,Tmp);
1503
{ ---------------------------------------------------------------------
1504
Encode/decode date, specifying week of year and day of week
1505
---------------------------------------------------------------------}
1507
Function EncodeDateWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
1509
If Not TryEncodeDateWeek(AYear,AWeekOfYear,Result,ADayOfWeek) then
1510
InvalidDateWeekError(AYear,AWeekOfYear,ADayOfWeek);
1514
Function EncodeDateWeek(const AYear, AWeekOfYear: Word): TDateTime; //; const ADayOfWeek: Word = 1
1516
EncodeDateWeek(AYear,AWeekOfYear,1);
1520
Procedure DecodeDateWeek(const AValue: TDateTime; var AYear, AWeekOfYear, ADayOfWeek: Word);
1522
NotYetImplemented('DecodeDateWeek');
1526
Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime; const ADayOfWeek: Word): Boolean;
1533
Result:=IsValidDateWeek(Ayear,AWeekOfYear,ADayOfWeek);
1536
AValue:=EncodeDate(AYear,1,1)+(7*(AWeekOfYear-1));
1537
DOW:=DayOfTheWeek(AValue);
1538
Rest:=ADayOfWeek-DOW;
1541
AValue:=AValue+Rest;
1546
Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime): Boolean; //; const ADayOfWeek: Word = 1
1548
Result:=TryEncodeDateWeek(AYear,AWeekOfYear,AValue,1);
1551
{ ---------------------------------------------------------------------
1552
Encode/decode date, specifying day of year
1553
---------------------------------------------------------------------}
1555
Function EncodeDateDay(const AYear, ADayOfYear: Word): TDateTime;
1557
If Not TryEncodeDateDay(AYear,ADayOfYear,Result) then
1558
InvalidDateDayError(AYear,ADayOfYear);
1562
Procedure DecodeDateDay(const AValue: TDateTime; var AYear, ADayOfYear: Word);
1568
DecodeDate(AValue,AYear,M,D);
1569
ADayOfyear:=Trunc(AValue-EncodeDate(AYear,1,1))+1;
1573
Function TryEncodeDateDay(const AYear, ADayOfYear: Word; var AValue: TDateTime): Boolean;
1575
Result:=(ADayOfYear<>0) and (ADayOfYear<=DaysPerYear [IsleapYear(AYear)]);
1577
AValue:=EncodeDate(AYear,1,1)+ADayOfYear-1;
1581
{ ---------------------------------------------------------------------
1582
Encode/decode date, specifying week of month
1583
---------------------------------------------------------------------}
1586
Function EncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): TDateTime;
1588
If Not TryEncodeDateMonthWeek(Ayear,AMonth,AWeekOfMonth,ADayOfWeek,Result) then
1589
InvalidDateMonthWeekError(AYear,AMonth,AWeekOfMonth,ADayOfWeek);
1592
Procedure DecodeDateMonthWeek(const AValue: TDateTime; var AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
1596
SOM,EOM : TdateTime;
1599
DecodeDate(AValue,AYear,AMonth,D);
1600
ADayOfWeek:=DayOfTheWeek(AValue);
1601
SOM:=EncodeDate(Ayear,Amonth,1);
1602
SDOM:=DayOfTheWeek(SOM);
1606
// Too early in the month. First full week is next week, day is after thursday.
1608
DecodeDateMonthWeek(SOM-1,AYear,AMonth,AWeekOfMonth,D)
1611
AWeekOfMonth:=(DOM div 7)+Ord((DOM mod 7)<>0);
1612
EDOM:=DayOfTheWeek(EndOfAMonth(Ayear,AMonth));
1613
// In last days of last long week, so in next month...
1614
If (EDOM<4) and ((DaysInAMonth(AYear,Amonth)-D)<EDOM) then
1628
Function TryEncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word; var AValue: TDateTime): Boolean;
1630
NotYetImplemented('TryEncodeDateMonthWeek');
1634
{ ---------------------------------------------------------------------
1635
Replace given element with supplied value.
1636
---------------------------------------------------------------------}
1639
LFAI = RecodeLeaveFieldAsIS; // Less typing, readable code
1641
Note: We have little choice but to implement it like Borland did:
1642
If AValue contains some 'wrong' value, it will throw an error.
1643
To simulate this we'd have to check in each function whether
1644
both arguments are correct. To avoid it, all is routed through
1645
the 'central' RecodeDateTime function as in Borland's implementation.
1648
Function RecodeYear(const AValue: TDateTime; const AYear: Word): TDateTime;
1651
RecodeDateTime(AValue,AYear,LFAI,LFAI,LFAI,LFAI,LFAI,LFAI);
1655
Function RecodeMonth(const AValue: TDateTime; const AMonth: Word): TDateTime;
1657
RecodeDateTime(AValue,LFAI,AMonth,LFAI,LFAI,LFAI,LFAI,LFAI);
1661
Function RecodeDay(const AValue: TDateTime; const ADay: Word): TDateTime;
1663
RecodeDateTime(AValue,LFAI,LFAI,ADay,LFAI,LFAI,LFAI,LFAI);
1667
Function RecodeHour(const AValue: TDateTime; const AHour: Word): TDateTime;
1669
RecodeDateTime(AValue,LFAI,LFAI,LFAI,AHour,LFAI,LFAI,LFAI);
1673
Function RecodeMinute(const AValue: TDateTime; const AMinute: Word): TDateTime;
1675
RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,AMinute,LFAI,LFAI);
1679
Function RecodeSecond(const AValue: TDateTime; const ASecond: Word): TDateTime;
1681
RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,LFAI,ASecond,LFAI);
1685
Function RecodeMilliSecond(const AValue: TDateTime; const AMilliSecond: Word): TDateTime;
1687
RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,LFAI,LFAI,AMilliSecond);
1691
Function RecodeDate(const AValue: TDateTime; const AYear, AMonth, ADay: Word): TDateTime;
1693
RecodeDateTime(AValue,AYear,AMonth,ADay,LFAI,LFAI,LFAI,LFAI);
1697
Function RecodeTime(const AValue: TDateTime; const AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
1699
RecodeDateTime(AValue,LFAI,LFAI,LFAI,AHour,AMinute,ASecond,AMilliSecond);
1703
Function RecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;
1705
If Not TryRecodeDateTime(AValue,AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,Result) then
1706
InvalidDateTimeError(AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,AValue);
1710
Function TryRecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AResult: TDateTime): Boolean;
1712
Procedure FV (Var FV : Word; Arg : Word);
1720
Y,M,D,H,N,S,MS : Word;
1723
DecodeDateTime(AValue,Y,M,D,H,N,S,MS);
1731
Result:=TryEncodeDateTime(Y,M,D,H,N,S,MS,AResult);
1734
{ ---------------------------------------------------------------------
1735
Comparision of date/time
1736
---------------------------------------------------------------------}
1739
Function CompareDateTime(const A, B: TDateTime): TValueRelationship;
1741
If SameDateTime(A,B) then
1744
Result:=GreaterThanValue
1746
Result:=LessThanValue
1750
Function CompareDate(const A, B: TDateTime): TValueRelationship;
1752
If SameDate(A,B) then
1755
Result:=LessThanValue
1757
Result:=GreaterThanValue;
1761
Function CompareTime(const A, B: TDateTime): TValueRelationship;
1764
If SameTime(A,B) then
1766
else If Frac(A)<Frac(B) then
1767
Result:=LessThanValue
1769
Result:=GreaterThanValue;
1773
Function SameDateTime(const A, B: TDateTime): Boolean;
1775
Result:=Abs(A-B)<OneMilliSecond;
1779
Function SameDate(const A, B: TDateTime): Boolean;
1781
Result:=Trunc(A)=Trunc(B);
1785
Function SameTime(const A, B: TDateTime): Boolean;
1788
Result:=Frac(Abs(A-B))<OneMilliSecond;
1792
Function NthDayOfWeek(const AValue: TDateTime): Word;
1795
Result:=(DayOfTheMonth(AValue)-1) div 7 + 1;
1799
Procedure DecodeDayOfWeekInMonth(const AValue: TDateTime; var AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
1802
NotYetImplemented('DecodeDayOfWeekInMonth');
1806
Function EncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word): TDateTime;
1808
If Not TryEncodeDayOfWeekInMonth(AYear,AMonth,ANthDayOfWeek,ADayOfWeek,Result) then
1809
InvalidDayOfWeekInMonthError(AYear,AMonth,ANthDayOfWeek,ADayOfWeek);
1813
Function TryEncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word; var AValue: TDateTime): Boolean;
1819
SOM:=DayOfTheWeek(EncodeDate(Ayear,AMonth,1));
1820
D:=1+ADayOfWeek-SOM+7*(ANthDayOfWeek-1);
1821
If SOM>ADayOfWeek then
1822
D:=D+7; // Clearer would have been Inc(ANthDayOfweek) but it's a const
1823
Result:=TryEncodeDate(Ayear,AMonth,D,AValue);
1826
{ ---------------------------------------------------------------------
1827
Exception throwing routines
1828
---------------------------------------------------------------------}
1832
Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; const ABaseDate: TDateTime);
1834
Function DoField(Arg,Def : Word; Unknown: String) : String;
1838
Result:=Format('%.*d',[Length(Unknown),Arg])
1839
else if (ABaseDate=0) then
1842
Result:=Format('%.*d',[Length(Unknown),Arg]);
1846
Y,M,D,H,N,S,MS : Word;
1850
DecodeDateTime(ABasedate,Y,M,D,H,N,S,MS);
1851
Msg:=DoField(AYear,Y,'????');
1852
Msg:=Msg+DateSeparator+DoField(AMonth,M,'??');
1853
Msg:=Msg+DateSeparator+DoField(ADay,D,'??');
1854
Msg:=Msg+' '+DoField(AHour,H,'??');
1855
Msg:=Msg+TimeSeparator+DoField(AMinute,N,'??');
1856
Msg:=Msg+TimeSeparator+Dofield(ASecond,S,'??');
1857
Msg:=Msg+DecimalSeparator+DoField(AMilliSecond,MS,'???');
1858
Raise EConvertError.CreateFmt(SErrInvalidTimeStamp,[Msg]);
1862
Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word); // const ABaseDate: TDateTime = 0
1864
InvalidDateTimeError(AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,0);
1868
Procedure InvalidDateWeekError(const AYear, AWeekOfYear, ADayOfWeek: Word);
1870
Raise EConvertError.CreateFmt(SErrInvalidDateWeek,[AYear,AWeekOfYear,ADayOfWeek]);
1874
Procedure InvalidDateDayError(const AYear, ADayOfYear: Word);
1876
Raise EConvertError.CreateFmt(SErrInvalidDayOfYear,[AYear,ADayOfYear]);
1880
Procedure InvalidDateMonthWeekError(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
1882
Raise EConvertError.CreateFmt(SErrInvalidDateMonthWeek,[Ayear,AMonth,AWeekOfMonth,ADayOfWeek]);
1886
Procedure InvalidDayOfWeekInMonthError(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);
1889
Raise EConvertError.CreateFmt(SErrInvalidDayOfWeekInMonth,[AYear,AMonth,ANthDayOfWeek,ADayOfWeek]);
1893
{ ---------------------------------------------------------------------
1894
Julian and Modified Julian Date conversion support
1895
---------------------------------------------------------------------}
1898
Function DateTimeToJulianDate(const AValue: TDateTime): Double;
1900
NotYetImplemented('DateTimeToJulianDate');
1904
Function JulianDateToDateTime(const AValue: Double): TDateTime;
1906
NotYetImplemented('JulianDateToDateTime');
1910
Function TryJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;
1913
NotYetImplemented('TryJulianDateToDateTime');
1917
Function DateTimeToModifiedJulianDate(const AValue: TDateTime): Double;
1919
NotYetImplemented('DateTimeToModifiedJulianDate');
1923
Function ModifiedJulianDateToDateTime(const AValue: Double): TDateTime;
1925
NotYetImplemented('ModifiedJulianDateToDateTime');
1929
Function TryModifiedJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;
1932
NotYetImplemented('TryModifiedJulianDateToDateTime');
1935
{ ---------------------------------------------------------------------
1936
Unix timestamp support.
1937
---------------------------------------------------------------------}
1939
Function DateTimeToUnix(const AValue: TDateTime): Int64;
1941
NotYetImplemented('DateTimeToUnix');
1945
Function UnixToDateTime(const AValue: Int64): TDateTime;
1948
NotYetImplemented('UnixToDateTime');
1954
$Log: dateutil.inc,v $
1955
Revision 1.2 2004/01/10 19:35:17 michael
1956
+ Moved all resource strings to rtlconst/sysconst
1958
Revision 1.1 2003/12/06 18:03:46 hajny
1959
* include file for dateutil(s)
1961
Revision 1.2 2003/01/19 14:37:06 michael
1962
+ Much more functions implemented