-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDATETIME.PAS
607 lines (536 loc) · 15 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
{$I COMPILER.INC}
unit DateTime;
interface
uses
AplObj,
AplUtils,
Dos;
const
DateTimeFormat = 'M/DD/YYYY H:mm ap';
DateTimeFormatD = 'M-DD-YYYY HH:mm ap';
DmyDateTimeFormat = 'D/M/YYYY H:mm ap';
DmyDateTimeFormatD = 'D-M-YYYY H:mm ap';
LongDateFormat = 'M/DD/YYYY';
ShortDateFormat = 'M/DD/YY';
ShortDateFormatD = 'M-DD-YY';
LongDashFormatD = 'M-DD-YYYY';
LongDmyDateFormat = 'D/M/YYYY';
LongDmyDateFormatD = 'D-M-YYYY';
ShortDmyDateFormat = 'D/M/YY';
ShortDmyDateFormatD = 'D-MM-YY';
DefaultTimeFormat = 'H:mm:ss ap';
Time24HourFormat = 'HH:mm:ss';
var
DefaultDateFormat: string;
type
PDateTime = ^TDateTime;
PTimeSpan = ^TTimeSpan;
TWeekDay = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
TWeekDays = set of TWeekDay;
TDateTime = object(TObject)
private
public
Ticks: double; { 1/100th seconds since Jan 1, 0001 12:00 am }
constructor CreateDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond,
AHundredths: integer);
constructor CreateNow;
constructor CreateTicks(ATicks: double);
destructor Free; virtual;
procedure Init; virtual;
procedure AddDays(ADays: double);
procedure AddYears(AYears: double);
procedure AddMonths(AMonths: integer);
procedure AddHours(AHours: double);
procedure AddMinutes(AMinutes: double);
procedure AddSeconds(ASeconds: double);
procedure AddHundredths(AHundredths: double);
procedure AddTicks(ATickCount: double);
procedure Add(ASpan: PTimeSpan);
procedure GetDateParts(var AYear: word; var AMonth: byte; var ADay: byte);
procedure GetTimeParts(var AHour, AMinute, ASecond, AHundredth: byte);
procedure SetNow;
procedure Assign(var ASource: TObject); virtual;
function ToString: string; virtual;
function ToStringFormat(const AFormat: string): string;
function GetTicks(AYear, AMonth, ADay, AHour, AMinute, ASecond,
AHundredth: integer): double;
function IsLeapYear(AYear: integer): boolean;
function DaysToYear(AYear: integer): double;
function DayOfWeek: TWeekDay;
function Year: word;
function Month: byte;
function Day: byte;
function Hour: byte;
function Minute: byte;
function Second: byte;
function Hundredth: byte;
end;
TTimeSpan = object(TDateTime)
public
constructor CreateSpan(ATimeSpan: TTimeSpan);
constructor CreateHours(AHours: double);
constructor CreateMinutes(AMinutes: double);
constructor CreateSeconds(ASeconds: double);
constructor CreateHundredths(AHundredths: double);
constructor CreateTicks(ATicks: double);
constructor CreateAll(ADays, AHours, AMinutes, ASeconds, AHundredths: double);
function TotalHundredths: double;
function TotalSeconds: double;
function TotalMinutes: double;
function TotalHours: double;
function TotalDays: double;
function ToString: string; virtual;
procedure Init; virtual;
procedure GetDuration(var ATimeSpan: TTimeSpan);
public
end;
TStopWatch = object(TObject)
private
public
StartTime: TDateTime;
EndTime: TDateTime;
function ElapsedTicks: double;
procedure Init; virtual;
procedure GetElapsed(var ATimeSpan: TTimeSpan);
procedure Start;
procedure Stop;
procedure Reset;
end;
implementation
uses
AplStr;
type
MonthArray = array[1..12] of integer;
DaysToMonthArray = array[0..12] of integer;
const
TicksPerSecond = 100;
TicksPerMinute = TicksPerSecond * 60;
TicksPerHour = TicksPerMinute * 60;
TicksPerDay = TicksPerHour * 24;
TicksPer6Hours = TicksPerHour * 6;
DaysPerYear = 365;
DaysPer4Years = DaysPerYear * 4 + 1;
DaysPer100Years = DaysPer4Years * 25 - 1;
DaysPer400Years = DaysPer100Years * 4 + 1;
DaysTo1601 = DaysPer400Years * 4;
DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367;
DaysTo1970 = DaysPer400Years * 4 + DaysPer100Years * 3 + DaysPer4Years * 17 + DaysPerYear;
DaysInMonth365: MonthArray = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
DaysInMonth366: MonthArray = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
DaysToMonth365: DaysToMonthArray = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);
DaysToMonth366: DaysToMonthArray = (0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366);
constructor TDateTime.CreateDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond,
AHundredths: integer);
begin
inherited Create;
Ticks := GetTicks(AYear, AMonth, ADay, AHour, AMinute, ASecond, AHundredths);
end;
constructor TDateTime.CreateTicks(ATicks: double);
begin
inherited Create;
Ticks := ATicks;
end;
procedure TDateTime.SetNow;
var
y, m, d, dw, h, mi, s, sh: word;
begin
GetDate(y, m, d, dw);
GetTime(h, mi, s, sh);
Ticks := GetTicks(y, m, d, h, mi, s, sh);
end;
constructor TDateTime.CreateNow;
begin
inherited Create;
SetNow;
end;
procedure TDateTime.Init;
begin
inherited Init;
Ticks := 0;
end;
destructor TDateTime.Free;
begin
inherited Free;
end;
function TDateTime.IsLeapYear(AYear: integer): boolean;
begin
if (AYear and 3 <> 0) then begin
IsLeapYear := false;
exit;
end;
if (AYear and 15 = 0) then begin
IsLeapYear := true;
exit;
end;
IsLeapYear := AYear mod 25 <> 0;
end;
function TDateTime.DaysToYear(AYear: integer): double;
var
y, cent: double;
days: double;
begin
y := AYear - 1.0;
cent := Int(y / 100.0);
days := Int((y * 1461.0) / 4.0) - cent + Int(cent / 4.0);
DaysToYear := days;
end;
function TDateTime.GetTicks(AYear, AMonth, ADay, AHour, AMinute, ASecond,
AHundredth: integer): double;
var
days: DaysToMonthArray;
result: double;
begin
if (IsLeapYear(AYear)) then days := DaysToMonth366
else days := DaysToMonth365;
result := DaysToYear(AYear) + days[AMonth - 1] + ADay - 1.0;
result := result * (TicksPerDay * 1.0);
result := result + (AHour * 1.0) * TicksPerHour;
result := result + (AMinute * 1.0) * TicksPerMinute;
result := result + (ASecond * 1.0) * TicksPerSecond;
result := result + AHundredth;
GetTicks := result;
end;
function TDateTime.DayOfWeek: TWeekDay;
begin
DayOfWeek := TWeekDay((Trunc(Ticks / TicksPerDay) + 1) mod 7);
end;
procedure TDateTime.AddDays(ADays: double);
begin
Ticks := Ticks + ADays * TicksPerDay;
end;
procedure TDateTime.GetDateParts(var AYear: word; var AMonth: byte; var ADay: byte);
var
num: double;
y400, y100, y4, y1: double;
days: DaysToMonthArray;
m: word;
begin
num := Int(Ticks / TicksPerDay);
y400 := Int(num / DaysPer400Years);
num := num - y400 * DaysPer400Years;
y100 := Int(num / DaysPer100Years);
if (y100 = 4) then y100 := 3;
num := Int(num - y100 * DaysPer100Years);
y4 := Int(num / DaysPer4Years);
num := Int(num - y4 * DaysPer4Years);
y1 := Int(num / DaysPerYear);
if (y1 = 4) then y1 := 3;
AYear := Trunc(y400 * 400 + y100 * 100 + y4 * 4 + y1 + 1);
num := num - y1 * DaysPerYear;
if (Trunc(y1) = 3) and ((Trunc(y4) <> 24) or (Trunc(y100) = 3)) then
days := DaysToMonth366
else
days := DaysToMonth365;
m := Trunc(num) shr 5 + 1;
while num >= days[m] do
Inc(m);
AMonth := byte(m);
ADay := Trunc(num - days[m - 1] + 1);
end;
procedure TDateTime.GetTimeParts(var AHour, AMinute, ASecond, AHundredth: byte);
var
time: double;
begin
time := Frac(Ticks / TicksPerDay) * 24;
AHour := Trunc(time);
time := (time - AHour) * 60;
AMinute := Trunc(time);
time := (time - AMinute) * 60;
ASecond := Trunc(time);
time := (time - ASecond) * 100;
AHundredth := Round(time);
end;
procedure TDateTime.AddYears(AYears: double);
var
aYear: word;
aMonth, aDay: byte;
y, m, d: integer;
n: double;
begin
Init;
GetDateParts(aYear, aMonth, aDay);
y := aYear + Round(AYears);
n := DaysToYear(Round(y));
m := month - 1;
d := day - 1;
if IsLeapYear(Round(y)) then
n := n + DaysInMonth366[aMonth]
else begin
if (d = 28) or (m = 1) then
Dec(d);
n := n + DaysInMonth365[aMonth];
end;
n := n + d;
Ticks := n * TicksPerDay;
end;
procedure TDateTime.AddMonths(AMonths: integer);
var
aYear: word;
aMonth, aDay: byte;
y, m, d, q, dayCount: integer;
n: double;
days: DaysToMonthArray;
daysToMonth: integer;
begin
GetDateParts(aYear, aMonth, aDay);
y := aYear;
d := aDay;
m := aMonth + AMonths;
if m > 0 then
q := (m - 1) div 12
else
q := m div 12 - 1;
Inc(y, q);
Dec(m, q * 12);
if (IsLeapYear(y)) then
days := DaysToMonth366
else
days := DaysToMonth365;
daysToMonth := days[m - 1];
dayCount := days[m] - daysToMonth;
if (d > dayCount) then
d := dayCount;
n := DaysToYear(y) + daysToMonth + d - 1;
Ticks := TicksPerDay * (n + Frac(Ticks / TicksPerDay));
end;
procedure TDateTime.AddHours(AHours: double);
begin
Ticks := Ticks + AHours * TicksPerHour;
end;
procedure TDateTime.AddMinutes(AMinutes: double);
begin
Ticks := Ticks + AMinutes * TicksPerMinute;
end;
procedure TDateTime.AddSeconds(ASeconds: double);
begin
Ticks := Ticks + ASeconds * TicksPerSecond;
end;
procedure TDateTime.AddHundredths(AHundredths: double);
begin
Ticks := Ticks + AHundredths;
end;
procedure TDateTime.AddTicks(ATickCount: double);
begin
Ticks := Ticks + ATickCount;
end;
procedure TDateTime.Add(ASpan: PTimeSpan);
begin
Ticks := Ticks + ASpan^.Ticks;
end;
function TDateTime.Year: word;
var
aYear: word;
aMonth, aDay: byte;
begin
GetDateParts(aYear, aMonth, aDay);
Year := aYear;
end;
function TDateTime.Month: byte;
var
aYear: word;
aMonth, aDay: byte;
begin
GetDateParts(aYear, aMonth, aDay);
Month := aMonth;
end;
function TDateTime.Day: byte;
var
aYear: word;
aMonth, aDay: byte;
begin
GetDateParts(aYear, aMonth, aDay);
Day := aDay;
end;
function TDateTime.Hour: byte;
var
aHour, aMinute, aSecond, aHundredth: byte;
begin
GetTimeParts(aHour, aMinute, aSecond, aHundredth);
Hour := aHour;
end;
function TDateTime.Minute: byte;
var
aHour, aMinute, aSecond, aHundredth: byte;
begin
GetTimeParts(aHour, aMinute, aSecond, aHundredth);
Minute := aMinute;
end;
function TDateTime.Second: byte;
var
aHour, aMinute, aSecond, aHundredth: byte;
begin
GetTimeParts(aHour, aMinute, aSecond, aHundredth);
Second := aSecond;
end;
function TDateTime.Hundredth: byte;
var
aHour, aMinute, aSecond, aHundredth: byte;
begin
GetTimeParts(aHour, aMinute, aSecond, aHundredth);
Hundredth := aHundredth;
end;
function TDateTime.ToString: string;
begin
ToString := ToStringFormat(DateTimeFormat);
end;
procedure TDateTime.Assign(var ASource: TObject);
var
source: PDateTime;
begin
inherited Assign(ASource);
source := PDateTime(@ASource);
Ticks := source^.Ticks;
end;
function TDateTime.ToStringFormat(const AFormat: string): string;
var
aYear: word;
aMonth, aDay: byte;
aHour, aMinute, aSecond, aHundredth: byte;
apPos: word;
amPmString: string;
is24Hour: boolean;
builder: PStringBuilder;
str: PChar;
begin
builder := New(PStringBuilder, CreateString(AFormat));
GetDateParts(aYear, aMonth, aDay);
GetTimeParts(aHour, aMinute, aSecond, aHundredth);
is24Hour := true;
str := builder^.FindString('ap', 0, [scIgnoreCase], apPos);
if Assigned(str) then begin
amPmString := 'AM';
is24Hour := false;
if aHour >= 12 then begin
amPmString := 'PM';
Dec(aHour, 12);
end;
end;
builder^
.ReplaceString('YYYY', ZeroPad(aYear, 4), [])^
.ReplaceString('YY', ZeroPad(aYear mod 100, 2), [])^
.ReplaceString('MM', ZeroPad(aMonth, 2), [])^
.ReplaceString('M', IntToStr(aMonth), [])^
.ReplaceString('DD', ZeroPad(aDay, 2), [])^
.ReplaceString('D', IntToStr(aDay), [])^
.ReplaceString('HH', ZeroPad(aHour, 2), [])^
.ReplaceString('H', IntToStr(aHour), [])^
.ReplaceString('mm', ZeroPad(aMinute, 2), [])^
.ReplaceString('m', IntToStr(aMinute), [])^
.ReplaceString('ss', ZeroPad(aSecond, 2), [])^
.ReplaceString('s', IntToStr(aSecond), [])^
.ReplaceString('hh', ZeroPad(aHundredth, 2), [])^
.ReplaceString('h', IntToStr(aHundredth), [])^
.ReplaceString('{AP}', amPmString, [])^
.ReplaceString('ap', LowerCase(amPmString), [])^
.ReplaceString('AP', amPmString, []);
if not Builder^.HasException then
ToStringFormat := builder^.ToString
else
ToStringFormat := '';
FreeAndNil(Builder);
end;
constructor TTimeSpan.CreateSpan(ATimeSpan: TTimeSpan);
begin
inherited Create;
Ticks := ATimeSpan.Ticks;
end;
constructor TTimeSpan.CreateHours(AHours: double);
begin
inherited Create;
Ticks := AHours * TicksPerHour;
end;
constructor TTimeSpan.CreateMinutes(AMinutes: double);
begin
inherited Create;
Ticks := AMinutes * TicksPerMinute;
end;
constructor TTimeSpan.CreateSeconds(ASeconds: double);
begin
inherited Create;
Ticks := ASeconds * TicksPerSecond;
end;
constructor TTimeSpan.CreateHundredths(AHundredths: double);
begin
inherited Create;
Ticks := AHundredths;
end;
constructor TTimeSpan.CreateTicks(ATicks: double);
begin
inherited Create;
Ticks := ATicks;
end;
constructor TTimeSpan.CreateAll(ADays, AHours, AMinutes, ASeconds, AHundredths: double);
begin
inherited Create;
Ticks := 0;
Ticks := ADays * TicksPerDay;
Ticks := Ticks + AHours * TicksPerHour;
Ticks := Ticks + AMinutes * TicksPerMinute;
Ticks := Ticks + ASeconds * TicksPerSecond;
Ticks := Ticks + AHundredths;
end;
function TTimeSpan.TotalHundredths: double;
begin
TotalHundredths := Ticks;
end;
function TTimeSpan.TotalSeconds: double;
begin
TotalSeconds := Ticks / TicksPerSecond;
end;
function TTimeSpan.TotalMinutes: double;
begin
TotalMinutes := Ticks / TicksPerMinute;
end;
function TTimeSpan.TotalHours: double;
begin
TotalHours := Ticks / TicksPerHour;
end;
function TTimeSpan.TotalDays: double;
begin
TotalDays := Ticks / TicksPerDay;
end;
procedure TTimeSpan.Init;
begin
inherited Init;
Ticks := 0;
end;
procedure TTimeSpan.GetDuration(var ATimeSpan: TTimeSpan);
begin
ATimeSpan.Ticks := Abs(Ticks);
end;
function TTimeSpan.ToString: string;
begin
ToString := ToStringFormat('HH:mm:ss:hh');
end;
function TStopWatch. ElapsedTicks: double;
begin
ElapsedTicks := EndTime.Ticks - StartTime.Ticks;
end;
procedure TStopWatch.GetElapsed(var ATimeSpan: TTimeSpan);
begin
EndTime.SetNow;
ATimeSpan.Ticks := EndTime.Ticks - StartTime.Ticks;
end;
procedure TStopWatch.Init;
begin
inherited Init;
StartTime.CreateTicks(0);
EndTime.CreateTicks(0);
end;
procedure TStopWatch.Start;
begin
StartTime.CreateNow;
EndTime.CreateTicks(StartTime.Ticks);
end;
procedure TStopWatch.Stop;
begin
EndTime.CreateNow;
end;
procedure TStopWatch.Reset;
begin
StartTime.CreateTicks(0);
EndTime.CreateTicks(0);
end;
begin
DefaultDateFormat := LongDateFormat;
end.