-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDATETIME.PAS
351 lines (312 loc) · 8.35 KB
/
DATETIME.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
{$IFDEF debug}
{$A+,B-,D+,E+,F+,G+,I+,L+,N+,O-,P+,Q+,R+,S+,V+,X+,Y+}
{$ELSE}
{$A+,B-,D-,E+,F+,G+,I+,L-,N+,O-,P+,Q-,R-,S-,V+,X+,Y-}
{$ENDIF}
unit DateTime;
interface
uses
Dos,
Utils,
Objects;
type
TWeekDay =
(
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
);
TWeekDays = set of TWeekDay;
TDateTime = object(TBaseObject)
private
_ticks: comp; { 100th of second ticks since midnight 1/1/0001 }
_second, _minute, _hour, _hundredthSecond: byte;
_month, _day: byte;
_year: word;
public
constructor Create;
constructor CreateTicks(newTicks: comp);
constructor CreateDateTime(newYear, newMonth, newDay, newHour, newMinute, newSecond, newHundredthSecond: word);
constructor CreateDate(newYear, newMonth, newDay: word);
constructor CreateTime(newHour, newMinute, newSecond, newHundredthSecond: word);
constructor CreateCurrent;
constructor CreateCurrentDate;
constructor CreateCurrentTime;
procedure SetDate(newYear, newMonth, newDay: word); virtual;
procedure SetTime(newHour, newMinute, newSecond, newHundredthSecond: word); virtual;
procedure SetDateTime(newYear, newMonth, newDay, newHour, newMinute, newSecond, newHundredthSecond: word); virtual;
procedure SetTicks(newTicks: comp); virtual;
function Ticks: comp; virtual;
function Day: byte; virtual;
function Month: byte; virtual;
function Year: word; virtual;
function HundredthSecond: byte; virtual;
function Second: byte; virtual;
function Minute: byte; virtual;
function Hour: byte; virtual;
end;
TTimeSpan = object(TBaseObject)
end;
TTimer = object(TObject)
StartTime: TDateTime;
EndTime: TDateTime;
end;
TDate = TDateTime;
TTime = TDateTime;
function DaysInMonth(year: word; month: byte): byte;
function IsLeapYear(year: word): boolean;
function DaysInYear(year: word): word;
function LeapYearsBetween(year1, year2: word): word;
function OneTick: comp;
function OneSecond: comp;
function OneMinute: comp;
function OneHour: comp;
function OneDay: comp;
implementation
constructor TDateTime.Create;
begin
_ticks := 0;
_hundredthSecond := 0;
_second := 0;
_minute := 0;
_hour := 0;
_month := 0;
_day := 0;
_year := 0;
end;
constructor TDateTime.CreateTicks(newTicks: comp);
begin
SetTicks(newTicks);
end;
constructor TDateTime.CreateDateTime(newYear, newMonth, newDay, newHour, newMinute, newSecond, newHundredthSecond: word);
begin
SetDateTime(newYear, newMonth, newDay, newHour, newMinute, newSecond, newHundredthSecond);
end;
constructor TDateTime.CreateDate(newYear, newMonth, newDay: word);
begin
SetDate(newYear, newMonth, newDay);
end;
constructor TDateTime.CreateTime(newHour, newMinute, newSecond, newHundredthSecond: word);
begin
SetTime(newHour, newMinute, newSecond, newHundredthSecond);
end;
constructor TDateTime.CreateCurrent;
var
newYear, newMonth, newDay, dayOfWeek: word;
newHour, newMinute, newSecond, newHundredthSecond: word;
begin
Dos.GetDate(newYear, newMonth, newDay, dayOfWeek);
Dos.GetTime(newHour, newMinute, newSecond, newHundredthSecond);
SetDateTime(newYear, newMonth, newDay, newHour, newMinute, newSecond, newHundredthSecond);
end;
constructor TDateTime.CreateCurrentDate;
var
newYear, newMonth, newDay, dayOfWeek: word;
begin
Dos.GetDate(newYear, newMonth, newDay, dayOfWeek);
SetDate(newYear, newMonth, newDay);
end;
constructor TDateTime.CreateCurrentTime;
var
newHour, newMinute, newSecond, newHundredthSecond: word;
begin
Dos.GetTime(newHour, newMinute, newSecond, newHundredthSecond);
SetTime(newHour, newMinute, newSecond, newHundredthSecond);
end;
procedure TDateTime.SetDate(newYear, newMonth, newDay: word);
var
dayOfWeek, leapYears: word;
begin
leapYears := LeapYearsBetween(1, newYear - 1);
_ticks := (newDay - 1) * OneDay;
_ticks := _ticks + (newMonth - 1) * DaysInMonth(newYear, newMonth) * OneDay;
_ticks := _ticks + (newYear - 1 - leapYears) * 365 * OneDay;
_ticks := _ticks + (leapYears) * 366 * OneDay;
_hundredthSecond := 0;
_second := 0;
_minute := 0;
_hour := 0;
_month := newMonth;
_day := newDay;
_year := newYear;
end;
procedure TDateTime.SetTime(newHour, newMinute, newSecond, newHundredthSecond: word);
begin
_ticks := newHundredthSecond;
_ticks := _ticks + newSecond * OneSecond;
_ticks := _ticks + newMinute * OneMinute;
_ticks := _ticks + newHour * OneHour;
_hundredthSecond := newHundredthSecond;
_second := newSecond;
_minute := newMinute;
_hour := newHour;
_month := 0;
_day := 0;
_year := 0;
end;
procedure TDateTime.SetDateTime(newYear, newMonth, newDay, newHour, newMinute, newSecond, newHundredthSecond: word);
var
dayOfWeek: word;
leapYears: longint;
begin
leapYears := LeapYearsBetween(1, newYear - 1);
_ticks := newHundredthSecond;
_ticks := _ticks + newSecond * OneSecond;
_ticks := _ticks + newMinute * OneMinute;
_ticks := _ticks + newHour * OneHour;
_ticks := _ticks + (newDay - 1) * OneDay;
_ticks := _ticks + (newMonth - 1) * DaysInMonth(newYear, newMonth) * OneDay;
_ticks := _ticks + (newYear - 1 - leapYears) * 365 * OneDay;
_ticks := _ticks + (leapYears) * 366 * OneDay;
_hundredthSecond := newHundredthSecond;
_second := newSecond;
_minute := newMinute;
_hour := newHour;
_month := newMonth;
_day := newDay;
_year := newYear;
end;
procedure TDateTime.SetTicks(newTicks: comp);
var
index: integer;
currentTicks: comp;
days: word;
intTicks: longint;
h: longint;
begin
_ticks := newTicks;
currentTicks := _ticks;
_year := 1;
repeat
days := 365;
if IsLeapYear(_year + 1) then
days := 366;
if (currentTicks - (days * OneDay) <= 0) then
break;
currentTicks := currentTicks - (days * OneDay);
Inc(_year);
until currentTicks <= 0;
_month := 1;
repeat
if (currentTicks - (DaysInMonth(_year, _month) * OneDay) < 0) then
break;
currentTicks := currentTicks - (DaysInMonth(_year, _month) * OneDay);
Inc(_month);
until currentTicks <= 0;
_day := 1;
repeat
if (currentTicks - (_day * OneDay) <= 0) then
break;
currentTicks := currentTicks - (_day * OneDay);
Inc(_day);
until currentTicks <= 0;
intTicks := round(currentTicks);
h := intticks div Trunc(OneHour);
_hour := intTicks mod trunc(OneHour);
intTicks := intTicks div trunc(OneHour);
_minute := intTicks mod trunc(OneMinute);
intTicks := intTicks div trunc(OneMinute);
_second := intTicks mod trunc(OneSecond);
intTicks := intTicks div trunc(OneSecond);
_hundredthSecond := intTicks;
end;
function TDateTime.Ticks: comp;
begin
Ticks := _ticks;
end;
function TDateTime.Day: byte;
begin
Day := _day;
end;
function TDateTime.Month: byte;
begin
Month := _month;
end;
function TDateTime.Year: word;
begin
Year := _year;
end;
function TDateTime.HundredthSecond: byte;
begin
HundredthSecond := _hundredthSecond;
end;
function TDateTime.Second: byte;
begin
Second := _second;
end;
function TDateTime.Minute: byte;
begin
Minute := _minute;
end;
function TDateTime.Hour: byte;
begin
Hour := _hour;
end;
function OneTick: comp;
begin
OneTick := 1;
end;
function OneSecond: comp;
begin
OneSecond := OneTick * 100;
end;
function OneMinute: comp;
begin
OneMinute := OneSecond * 60;
end;
function OneHour: comp;
begin
OneHour := OneMinute * 60;
end;
function OneDay: comp;
begin
OneDay := OneHour * 24;
end;
function DaysInMonth(year: word; month: byte): byte;
var
result: byte;
begin
case month of
1: result := 31;
2: result := 28;
3: result := 31;
4: result := 30;
5: result := 31;
6: result := 30;
7: result := 31;
8: result := 31;
9: result := 30;
10: result := 31;
11: result := 30;
12: result := 31;
end;
if IsLeapYear(year) then
result := 29;
DaysInMonth := result;
end;
function DaysInYear(year: word): word;
begin
DaysInYear := 365;
if (IsLeapYear(year)) then
DaysInYear := 366;
end;
function IsLeapYear(year: word): boolean;
begin
IsLeapYear := (year mod 4 = 0) and not ((year mod 100 = 0) and not (year mod 400 = 0));
end;
{ Number of leap years between 1 and year, inclusive }
function CalcLeapYears(year: word): word;
begin
CalcLeapYears := (year div 4) - (year div 100) + (year div 400);
end;
{ Number of leap years between year1 and year2 inclusive }
function LeapYearsBetween(year1, year2: word): word;
begin
Dec(year1);
LeapYearsBetween := CalcLeapYears(year2) - CalcLeapYears(year1);
end;
end.