-
Notifications
You must be signed in to change notification settings - Fork 1
/
ibutil.pas
134 lines (113 loc) · 3.29 KB
/
ibutil.pas
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
{
TBUDF - Tecnobyte UDF for InterBase and FireBird
Author...: Daniel Pereira Guimarães
E-mail...: [email protected]
Home-Page: www.ulbrajp.com.br/~tecnobyte
This library is Open-Source!
}
unit ibutil;
interface
uses
sysutils, consts;
type
TIBDate = integer;
TIBTime = Cardinal;
TIBTimeStamp = packed record
Date: TIBDate;
Time: TIBTime;
end;
PIBDate = ^TIBDate;
PIBTime = ^TIBTime;
PIBTimeStamp = ^TIBTimeStamp;
procedure IBDecodeTime(IBTime: TIBTime; var Hour, Min, Sec: SmallInt);
function IBEncodeTime(Hour, Min, Sec: SmallInt): TIBTime;
procedure IBDecodeDate(IBDate: TIBDate; var Year, Month, Day: SmallInt);
function IBEncodeDate(Year, Month, Day: SmallInt): TIBDate;
function IBTimeSpan(T1, T2: TIBTimeStamp): int64;
function IBTimeAsSec(IBTime: TIBTime): integer;
implementation
procedure IBDecodeTime(IBTime: TIBTime; var Hour, Min, Sec: SmallInt);
{ In InterBase: 1 second = 10000 }
var
TotalSeconds: Cardinal;
begin
TotalSeconds := IBTime div ISC_TIME_SECONDS_PRECISION;
Hour := TotalSeconds div SECONDS_PER_HOUR;
Min := (TotalSeconds div SECONDS_PER_MINUTE) mod SECONDS_PER_MINUTE;
Sec := TotalSeconds mod SECONDS_PER_MINUTE;
end;
function IBEncodeTime(Hour, Min, Sec: SmallInt): TIBTime;
begin
Result := (Hour * SECONDS_PER_HOUR + Min * SECONDS_PER_MINUTE + Sec)
* ISC_TIME_SECONDS_PRECISION;
end;
procedure IBDecodeDate(IBDate: TIBDate; var Year, Month, Day: SmallInt);
{ The IBDecodeDate procedure is based in ndate() function of
gds.cpp (FireBird API) }
var
Century, Y, M, D: integer;
begin
IBDate := IBDate - (1721119 - 2400001);
Century := (4 * IBDate - 1) div 146097;
IBDate := 4 * IBDate - 1 - 146097 * Century;
D := IBDate div 4;
IBDate := (4 * D + 3) div 1461;
D := 4 * D + 3 - 1461 * IBDate;
D := (D + 4) div 4;
M := (5 * D - 3) div 153;
D := 5 * D - 3 - 153 * M;
D := (D + 5) div 5;
Y := 100 * Century + IBDate;
if M < 10 then
M := M + 3
else begin
M := M - 9;
Y := Y + 1;
end;
Year := Y;
Month := M;
Day := D;
end;
function IBEncodeDate(Year, Month, Day: SmallInt): TIBDate;
{ The IBEncodeDate function is based in nday() function of
gds.cpp (FireBird API) }
var
Century, ShortYear: integer;
begin
if Month > 2 then
Month := Month - 3
else begin
Month := Month + 9;
Year := Year - 1;
end;
Century := Year div 100;
ShortYear := Year - 100 * Century;
Result :=
(146097 * Century) div 4 +
(1461 * ShortYear) div 4 +
(153 * Month + 2) div 5 + Day + 1721119 - 2400001;
end;
function IBTimeSpan(T1, T2: TIBTimeStamp): int64;
var
Days, Seconds: integer;
begin
if T1.Date < T2.Date then begin
Days := T2.Date - T1.Date - 1;
Seconds := SECONDS_PER_DAY - IBTimeAsSec(T1.Time) + IBTimeAsSec(T2.Time);
end else if T1.Date > T2.Date then begin
Days := T1.Date - T2.Date - 1;
Seconds := SECONDS_PER_DAY - IBTimeAsSec(T1.Time) + IBTimeAsSec(T1.Time);
end else begin { T1.Date = T2.Date }
Days := 0;
if T1.Time < T2.Time then
Seconds := IBTimeAsSec(T2.Time) - IBTimeAsSec(T1.Time)
else
Seconds := IBTimeAsSec(T1.Time) - IBTimeAsSec(T2.Time);
end;
Result := int64(Days) * SECONDS_PER_DAY + int64(Seconds);
end;
function IBTimeAsSec(IBTime: TIBTime): integer;
begin
Result := IBTime div ISC_TIME_SECONDS_PRECISION;
end;
end.