2
*********************************************************************
3
$Id: dati.inc,v 1.3 2004/05/02 13:40:55 marco Exp $
4
Copyright (C) 1997, 1998 Gertjan Schouten
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
*********************************************************************
21
System Utilities For Free Pascal
24
{==============================================================================}
25
{ internal functions }
26
{==============================================================================}
29
DayTable: array[Boolean, 1..12] of longint =
30
((0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334),
31
(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335));
33
Function DoEncodeDate(Year, Month, Day: Word): longint;
39
If TryEncodeDate(Year,Month,Day,D) then
45
function DoEncodeTime(Hour, Minute, Second, MilliSecond: word): longint;
51
If TryEncodeTime(Hour,Minute,Second,MilliSecond,T) then
52
Result:=trunc(T*MSecsPerDay)
57
{==============================================================================}
59
{==============================================================================}
61
{ DateTimeToTimeStamp converts DateTime to a TTimeStamp }
63
function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
65
result.Time := Trunc(Frac(DateTime) * MSecsPerDay);
66
result.Date := 1 + DateDelta + Trunc(System.Int(DateTime));
69
{ TimeStampToDateTime converts TimeStamp to a TDateTime value }
71
function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
73
result := (TimeStamp.Date - DateDelta - 1) + (TimeStamp.Time / MSecsPerDay);
78
function MSecsToTimeStamp(MSecs: comp): TTimeStamp;
80
result.Date := Round(msecs / msecsperday);
81
{$IFDEF VIRTUALPASCAL}
82
msecs:= msecs-result.date*msecsperday;
84
msecs:= comp(msecs-result.date*msecsperday);
86
result.Time := Round(MSecs);
91
function TimeStampToMSecs(const TimeStamp: TTimeStamp): comp;
93
result := TimeStamp.Time + timestamp.date*msecsperday;
96
Function TryEncodeDate(Year,Month,Day : Word; Var Date : TDateTime) : Boolean;
101
Result:=(Year>0) and (Year<10000) and
102
(Month in [1..12]) and
103
(Day>0) and (Day<=MonthDays[IsleapYear(Year),Month]);
115
Date := (146097*c) SHR 2 + (1461*ya) SHR 2 + (153*cardinal(Month)+2) DIV 5 + cardinal(Day) - 693900;
119
function TryEncodeTime(Hour, Min, Sec, MSec:word; Var Time : TDateTime) : boolean;
122
Result:=(Hour<24) and (Min<60) and (Sec<60) and (MSec<1000);
124
Time:=(Hour*3600000+Min*60000+Sec*1000+MSec)/MSecsPerDay;
127
{ EncodeDate packs three variables Year, Month and Day into a
128
TDateTime value the result is the number of days since 12/30/1899 }
130
function EncodeDate(Year, Month, Day: word): TDateTime;
133
If Not TryEncodeDate(Year,Month,Day,Result) then
134
Raise Exception.CreateFmt('%d-%d-%d is not a valid date specification',
138
{ EncodeTime packs four variables Hour, Minute, Second and MilliSecond into
141
function EncodeTime(Hour, Minute, Second, MilliSecond:word):TDateTime;
144
If not TryEncodeTime(Hour,Minute,Second,MilliSecond,Result) then
145
Raise Exception.CreateFmt('%d:%d:%d.%d is not a valid time specification',
146
[Hour,Minute,Second,MilliSecond]);
150
{ DecodeDate unpacks the value Date into three values:
151
Year, Month and Day }
153
procedure DecodeDate(Date: TDateTime; var Year, Month, Day: word);
157
j := pred((Trunc(System.Int(Date)) + 693900) SHL 2);
159
j:= j - 146097 * cardinal(Year);
161
j:=(Day SHL 2 + 3) DIV 1461;
162
Day:= (cardinal(Day) SHL 2 + 7 - 1461*j) SHR 2;
163
Month:=(5 * Day-3) DIV 153;
164
Day:= (5 * Day +2 - 153*Month) DIV 5;
165
Year:= 100 * cardinal(Year) + j;
176
function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean;
178
DecodeDate(DateTime,Year,Month,Day);
179
DOW:=DateTimeToTimeStamp(DateTime).Date mod 7+1;
180
Result:=IsLeapYear(Year);
184
{ DecodeTime unpacks Time into four values:
185
Hour, Minute, Second and MilliSecond }
187
procedure DecodeTime(Time: TDateTime; var Hour, Minute, Second, MilliSecond: word);
191
l := Round(Frac(time) * MSecsPerDay);
192
Hour := l div 3600000;
194
Minute := l div 60000;
196
Second := l div 1000;
201
{ DateTimeToSystemTime converts DateTime value to SystemTime }
203
procedure DateTimeToSystemTime(DateTime: TDateTime; var SystemTime: TSystemTime);
205
DecodeDate(DateTime, SystemTime.Year, SystemTime.Month, SystemTime.Day);
206
DecodeTime(DateTime, SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond);
209
{ SystemTimeToDateTime converts SystemTime to a TDateTime value }
211
function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
213
result := DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day) +
214
DoEncodeTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond) / MSecsPerDay;
217
{ DayOfWeek returns the Day of the week (sunday is day 1) }
219
function DayOfWeek(DateTime: TDateTime): integer;
221
Result := 1 + (Abs(Trunc(DateTime) - 1) mod 7);
224
{ Date returns the current Date }
226
function Date: TDateTime;
228
SystemTime: TSystemTime;
230
GetLocalTime(SystemTime);
231
result := DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
234
{ Time returns the current Time }
236
function Time: TDateTime;
238
SystemTime: TSystemTime;
240
GetLocalTime(SystemTime);
241
Result := DoEncodeTime(SystemTime.Hour,SystemTime.Minute,SystemTime.Second,SystemTime.MilliSecond) / MSecsPerDay;
244
{ Now returns the current Date and Time }
246
function Now: TDateTime;
248
SystemTime: TSystemTime;
250
GetLocalTime(SystemTime);
251
result := DoEncodeDate(SystemTime.Year,SystemTime.Month,SystemTime.Day) +
252
DoEncodeTime(SystemTime.Hour,SystemTime.Minute,SystemTime.Second,SystemTime.MilliSecond) / MSecsPerDay;
255
{ IncMonth increments DateTime with NumberOfMonths months,
256
NumberOfMonths can be less than zero }
258
function IncMonth(const DateTime: TDateTime; NumberOfMonths: integer): TDateTime;
260
Year, Month, Day: word;
263
If NumberOfMonths>=0 then
267
DecodeDate(DateTime, Year, Month, Day);
268
inc(Year,(NumberOfMonths div 12));
269
inc(Month,(NumberOfMonths mod 12)-1); // Mod result always positive
275
Inc(Month); { Months from 1 to 12 }
276
if (Month = 2) and (IsLeapYear(Year)) and (Day > 28) then
278
result := Frac(DateTime) + DoEncodeDate(Year, Month, Day);
281
{ IsLeapYear returns true if Year is a leap year }
283
function IsLeapYear(Year: Word): boolean;
285
Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
288
{ DateToStr returns a string representation of Date using ShortDateFormat }
290
function DateToStr(Date: TDateTime): string;
292
result := FormatDateTime('ddddd', Date);
295
{ TimeToStr returns a string representation of Time using ShortTimeFormat }
297
function TimeToStr(Time: TDateTime): string;
299
result := FormatDateTime('t', Time);
302
{ DateTimeToStr returns a string representation of DateTime using ShortDateTimeFormat }
304
function DateTimeToStr(DateTime: TDateTime): string;
306
result := FormatDateTime('c', DateTime);
309
{ StrToDate converts the string S to a TDateTime value
310
if S does not represent a valid date value
311
an EConvertError will be raised }
313
function StrToDate(const S: string): TDateTime;
318
{$IFDEF VIRTUALPASCAL}
323
dp,mp,yp,which : Byte;
325
values:array[1..3] of longint;
326
LocalTime:tsystemtime;
328
df := UpperCase(ShortDateFormat);
329
{ Determine order of D,M,Y }
335
while (i<Length(df)) and (Which<3) do
360
Raise EConvertError.Create('Illegal format string');
361
{ Get actual values }
366
for i := 1 to length(s) do
368
if (s[i] in ['0'..'9']) then
370
if (s[i] in [dateseparator,' ']) or (i = length(s)) then
374
Raise EConvertError.Create('Invalid date format');
375
val(s1, values[n], c);
377
Raise EConvertError.Create('Invalid date format');
382
getLocalTime(LocalTime);
383
ly := LocalTime.Year;
396
m := LocalTime.Month;
410
if (y >= 0) and (y < 100) then
412
ly := ly - TwoDigitYearCenturyWindow;
413
Inc(Y, ly div 100 * 100);
414
if (TwoDigitYearCenturyWindow > 0) and (Y < ly) then
417
Result := DoEncodeDate(y, m, d);
421
{ StrToTime converts the string S to a TDateTime value
422
if S does not represent a valid time value an
423
EConvertError will be raised }
425
function StrToTime(const s: string): TDateTime;
427
Len, Current: integer; PM: boolean;
429
function GetElement: integer;
432
{$IFDEF VIRTUALPASCAL}
440
while (result = -1) and (Current < Len) do begin
441
if S[Current] in ['0'..'9'] then begin
443
while (Current < Len) and (s[Current + 1] in ['0'..'9']) do
445
val(copy(S, j, 1 + Current - j), result, c);
447
else if (S[Current] = TimeAMString[1]) or (S[Current] in ['a', 'A']) then begin
450
else if (S[Current] = TimePMString[1]) or (S[Current] in ['p', 'P']) then begin
454
else if (S[Current] = TimeSeparator) or (S[Current] = ' ') then
457
raise EConvertError.Create('Invalid Time format');
463
TimeValues: array[0..4] of integer;
472
TimeValues[i] := GetElement;
473
while (i < 5) and (TimeValues[i] <> -1) do begin
475
TimeValues[i] := GetElement;
477
If (i<5) and (TimeValues[I]=-1) then
479
if PM then Inc(TimeValues[0], 12);
480
result := EncodeTime(TimeValues[0], TimeValues[1], TimeValues[2], TimeValues[3]);
483
{ StrToDateTime converts the string S to a TDateTime value
484
if S does not represent a valid date and time value
485
an EConvertError will be raised }
487
function StrToDateTime(const s: string): TDateTime;
491
if i > 0 then result := StrToDate(Copy(S, 1, i - 1)) + StrToTime(Copy(S, i + 1, length(S)))
492
else result := StrToDate(S);
495
{ FormatDateTime formats DateTime to the given format string FormatStr }
497
function FormatDateTime(FormatStr: string; DateTime: TDateTime): string;
500
ResultBuffer: array[0..255] of char;
501
ResultCurrent: pchar;
503
procedure StoreStr(Str: pchar; Len: integer);
505
if ResultLen + Len < SizeOf(ResultBuffer) then begin
506
StrMove(ResultCurrent, Str, Len);
507
ResultCurrent := ResultCurrent + Len;
508
ResultLen := ResultLen + Len;
512
procedure StoreString(const Str: string);
516
if ResultLen + Len < SizeOf(ResultBuffer) then begin
517
StrMove(ResultCurrent, pchar(Str), Len);
518
ResultCurrent := ResultCurrent + Len;
519
ResultLen := ResultLen + Len;
523
procedure StoreInt(Value, Digits: integer);
524
var S: string; Len: integer;
526
S := IntToStr(Value);
528
if Len < Digits then begin
529
S := copy('0000', 1, Digits - Len) + S;
532
StoreStr(pchar(@S[1]), Len);
535
Function TimeReFormat(Const S : string) : string;
536
// Change m into n for time formatting.
541
For I:=1 to Length(Result) do
542
If Result[i]='m' then
547
Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
549
procedure StoreFormat(const FormatStr: string);
552
FormatCurrent: pchar;
560
FormatCurrent := Pchar(FormatStr);
561
FormatEnd := FormatCurrent + Length(FormatStr);
564
while P < FormatEnd do begin
566
if Token in ['"', ''''] then begin
568
while (P < FormatEnd) and (P^ <> Token) do
571
else if Token = 'A' then begin
572
if (StrLIComp(P, 'A/P', 3) = 0) or
573
(StrLIComp(P, 'AMPM', 4) = 0) or
574
(StrLIComp(P, 'AM/PM', 5) = 0) then begin
581
while FormatCurrent < FormatEnd do begin
582
Token := UpCase(FormatCurrent^);
584
P := FormatCurrent + 1;
587
while (P < FormatEnd) and (p^ <> Token) do
590
Count := P - FormatCurrent;
591
StoreStr(FormatCurrent + 1, Count - 2);
594
if StrLIComp(FormatCurrent, 'AMPM', 4) = 0 then begin
596
if Hour < 12 then StoreString(TimeAMString)
597
else StoreString(TimePMString);
599
else if StrLIComp(FormatCurrent, 'AM/PM', 5) = 0 then begin
601
if Hour < 12 then StoreStr('am', 2)
602
else StoreStr('pm', 2);
604
else if StrLIComp(FormatCurrent, 'A/P', 3) = 0 then begin
606
if Hour < 12 then StoreStr('a', 1)
607
else StoreStr('p', 1);
610
Raise EConvertError.Create('Illegal character in format string');
612
'/': StoreStr(@DateSeparator, 1);
613
':': StoreStr(@TimeSeparator, 1);
614
' ', 'C', 'D', 'H', 'M', 'N', 'S', 'T', 'Y','Z' : begin
615
while (P < FormatEnd) and (UpCase(P^) = Token) do
617
Count := P - FormatCurrent;
619
' ': StoreStr(FormatCurrent, Count);
622
1: StoreInt(Year, 0);
623
2: StoreInt(Year mod 100, 2);
624
4: StoreInt(Year, 4);
629
1: StoreInt(Month, 0);
630
2: StoreInt(Month, 2);
631
3: StoreString(ShortMonthNames[Month]);
632
4: StoreString(LongMonthNames[Month]);
639
3: StoreString(ShortDayNames[DayOfWeek]);
640
4: StoreString(LongDayNames[DayOfWeek]);
641
5: StoreFormat(ShortDateFormat);
642
6: StoreFormat(LongDateFormat);
646
if Clock12 then begin
648
if tmp=0 then tmp:=12;
649
if Count = 1 then StoreInt(tmp, 0)
650
else StoreInt(tmp, 2);
653
if Count = 1 then StoreInt(Hour, 0)
654
else StoreInt(Hour, 2);
658
if Count = 1 then StoreInt(Minute, 0)
659
else StoreInt(Minute, 2);
662
if Count = 1 then StoreInt(Second, 0)
663
else StoreInt(Second, 2);
666
if Count = 1 then StoreInt(MilliSecond, 0)
667
else StoreInt(MilliSecond, 3);
670
if Count = 1 then StoreFormat(timereformat(ShortTimeFormat))
671
else StoreFormat(TimeReformat(LongTimeFormat));
675
StoreFormat(ShortDateFormat);
676
if (Hour<>0) or (Minute<>0) or (Second<>0) then
679
StoreFormat(TimeReformat(ShortTimeFormat));
687
FormatCurrent := FormatCurrent + Count;
692
DecodeDate(DateTime, Year, Month, Day);
693
DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
694
DayOfWeek := SysUtils.DayOfWeek(DateTime);
696
ResultCurrent := @ResultBuffer;
697
StoreFormat(FormatStr);
698
ResultBuffer[ResultLen] := #0;
699
result := StrPas(@ResultBuffer);
702
{ DateTimeToString formats DateTime to the given format in FormatStr }
704
procedure DateTimeToString(var Result: string; const FormatStr: string; const DateTime: TDateTime);
706
Result := FormatDateTime(FormatStr, DateTime);
710
Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
712
Var YY,MM,DD,H,m,s,msec : Word;
715
Decodedate (DateTime,YY,MM,DD);
716
If (YY<1980) or (YY>2099) then
720
DecodeTime (DateTime,h,m,s,msec);
721
Result:=(s shr 1) or (m shl 5) or (h shl 11);
722
Result:=Result or DD shl 16 or (MM shl 21) or ((YY-1980) shl 25);
727
Function FileDateToDateTime (Filedate : Longint) : TDateTime;
729
Var Date,Time : Word;
732
Date:=FileDate shr 16;
733
Time:=FileDate and $ffff;
734
Result:=EncodeDate((Date shr 9) + 1980,(Date shr 5) and 15, Date and 31) +
735
EncodeTime(Time shr 11, (Time shr 5) and 63, (Time and 31) shl 1,0);
740
Revision 1.3 2004/05/02 13:40:55 marco
741
* fix to AM/PM behaviour of formatdatetime around noon
743
Revision 1.2 2003/11/24 23:00:56 michael
746
Revision 1.1 2003/10/06 21:01:06 peter
747
* moved classes unit to rtl
749
Revision 1.10 2003/09/06 21:52:24 marco
752
Revision 1.9 2003/01/18 23:45:37 michael
753
+ Fixed EncodeDate/Time so they use TryEncodeDate/Time
755
Revision 1.8 2002/12/25 01:03:48 peter
756
* some date constants added
758
Revision 1.7 2002/09/07 21:06:51 carl
759
* bugfix 1867 (merged)
761
Revision 1.6 2002/09/07 16:01:22 peter
762
* old logs removed and tabs fixed