-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPDate.pas
494 lines (446 loc) · 9.41 KB
/
PDate.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
unit PDate;
{$MODE Delphi}
(**
* @name : PDate
* @programmer : A1Gard
* @time : 6 Aug 2011
* update : 09 Aug 2016
* @vertion : 1.4
*)
interface
uses
SysUtils ;
function PersiantoGer(PersianD: ShortString; ResultKind: Byte = 0): ShortString;
function GerToPersian(tt:tdatetime):String;
function UIntToDateTime (const Number : UInt64 ):TDateTime ;
function GerToWord (const Date : string ) : LongWord;
function PersianToInt (const DateSTr : string ) :Int64 ;
function DateTimeToUnix(dtDate: TDateTime): Longint;
function UnixToDateTime(USec: Longint): TDateTime;
function GetPersianDayWeek(const dt: TDateTime):byte;
function GetTheFirstDayOfThisMonth(const dt: TDateTime):TDateTime;
function GetTheLastDayOfThisMonth(const dt: TDateTime):TDateTime;
implementation
const
UnixStartDate: TDateTime = 25569.0; // 01/01/1970
(**
* @todo: convert standrad windows datetime to Unix time stamp
* @param (TDateTime|float) standrad windows datetime format
* @result (Longint)
**)
function DateTimeToUnix(dtDate: TDateTime): Longint;
begin
Result := Round((dtDate - UnixStartDate) * 86400);
end;
(**
* @todo: Unix time stamp to stamp convert standrad windows datetime
* @param (Longint)
* @result (TDateTime|float) standrad windows datetime format
**)
function UnixToDateTime(USec: Longint): TDateTime;
begin
Result := (Usec / 86400) + UnixStartDate;
end;
(**
* @todo: convert Persian date to Gregorian date
* @param (shortstring) PersianD the string of persian date exp : 1391/12/07
* @param (byte) what is format of result
* @result (string) Gregorian date
**)
function PersiantoGer(PersianD: ShortString; ResultKind: Byte = 0): ShortString;
function TrueTo1(co: Boolean): Integer;
begin
if co then TrueTo1 := 1
else
TrueTo1 := 0;
end;
const
Conm_mons: array[0..11] of Byte = (31,28,31,30,31,30,31,31,30,31,30,31);
monthes: array[0..11] of ShortString = ('Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
type
date = record
da_day, da_mon, da_year: Integer;
end;
var
m_mons: array[0..11] of BYTE;
LeapYearSh: array[0..1000] of UInt16 ;
LeapYearMi: array[0..1000] of UInt16 ;
LastDayCountSh, LastDayCountMi: integer;
a, b: date;
sYY, sMM, sDD: ShortString;
I: Integer;
begin
if Length(PersianD) < 9 then
begin
Result := '1970/01/01';
Exit;
end;
for i := 0 to 999 do
begin
LeapYearSh[i] := (i * 4) + 1275 ;
LeapYearMi[i] := (i * 4) + 1896 ;
end;
for I := Low(Conm_mons) to High(Conm_mons) do
m_mons[I] := Conm_mons[I];
a.da_day := StrToInt(Copy(PersianD, 9, 2));
a.da_mon := StrToInt(Copy(PersianD, 6, 2));
a.da_year := StrToInt(Copy(PersianD, 1, 4));
b.da_year := a.da_year + 621;
Inc(b.da_year, TrueTo1(((a.da_mon > 10) or ((a.da_mon = 10) and (a.da_day >= 12)))
or ((LeapYearSh[(a.da_year - 1275) div 4] <> a.da_year) and
((a.da_mon = 10) and (a.da_day = 11)))));
Inc(m_mons[1], TrueTo1(LeapYearMi[(b.da_year - 1896) div 4] = b.da_year));
if (a.da_mon <= 7) then LastDayCountSh := ((a.da_mon - 1) * 31 + a.da_day)
else
LastDayCountSh := (186 + (a.da_mon - 7) * 30 + a.da_day);
if (b.da_year = (a.da_year + 622)) then LastDayCountMi :=
LastDayCountSh - 286 - TrueTo1(LeapYearSh[(a.da_year - 1275) div 4] = a.da_year)
else
LastDayCountMi := (LastDayCountSh + 79);
b.da_day := LastDayCountMi;
b.da_mon := 0;
while (LastDayCountMi > m_mons[b.da_mon]) do
begin
Dec(LastDayCountMi, m_mons[b.da_mon]);
Inc(b.da_mon);
b.da_day := LastDayCountMi;
end;
Inc(b.da_mon);
sYY := '';
sMM := '';
sDD := '';
if b.da_year < 1000 then sYY := sYY + '0';
if b.da_year < 100 then sYY := sYY + '0';
if b.da_year < 10 then sYY := sYY + '0';
sYY := sYY + IntToStr(b.da_year);
if b.da_mon < 10 then sMM := sMM + '0';
sMM := sMM + IntToStr(b.da_mon);
if b.da_day < 10 then sDD := sDD + '0';
sDD := sDD + IntToStr(b.da_day);
case ResultKind of
0: Result := sYY + '/' + sMM + '/' + sDD;
1: Result := sYY + ' ' + monthes[b.da_mon - 1] + ' ' + sDD;
end;
end;
(**
* @todo: Gregorian to Persian Date
* @param (TDateTime|float) standrad windows datetime format
* @result (string) persian date by this format exp : 1367/04/19
**)
function GerToPersian(tt:TDateTime):String;
var
str,y,m,d:string;
yi,mi,di,ytmp:integer;
begin
str:=formatdatetime('yyyy,mm,dd',tt);
y:=copy(str,1,4);
m:=copy(str,6,2);
d:=copy(str,9,2);
yi:=strtoint(y);
mi:=strtoint(m);
di:=strtoint(d);
if (yi mod 4=0) then
if mi>2 then
begin
tt:=tt+1;
str:=formatdatetime('yyyy,mm,dd',tt);
y:=copy(str,1,4);
m:=copy(str,6,2);
d:=copy(str,9,2);
yi:=strtoint(y);
mi:=strtoint(m);
di:=strtoint(d);
end;
if ((mi<3) or ((mi=3) and (di<21))) then
begin
yi:=yi-622;
end
else
begin
yi:=yi-621;
end;
case mi of
1:
if di<21 then
begin
mi:=10;
di:=di+10;
end
else
begin
mi:=11;
di:=di-20;
end;
2:
if di<20 then
begin
mi:=11;
di:=di+11;
end
else
begin
mi:=12;
di:=di-19;
end;
3:
if di<21 then
begin
mi:=12;
di:=di+9;
end
else
begin
mi:=1;
di:=di-20;
end;
4:
if di<21 then
begin
mi:=1;
di:=di+11;
end
else
begin
mi:=2;
di:=di-20;
end;
5:
if di<22 then
begin
mi:=mi-3;
di:=di+10;
end
else
begin
mi:=mi-2;
di:=di-21;
end;
6:
if di<22 then
begin
mi:=mi-3;
di:=di+10;
end
else
begin
mi:=mi-2;
di:=di-21;
end;
7:
if di<23 then
begin
mi:=mi-3;
di:=di+9;
end
else
begin
mi:=mi-2;
di:=di-22;
end;
8:
if di<23 then
begin
mi:=mi-3;
di:=di+9;
end
else
begin
mi:=mi-2;
di:=di-22;
end;
9:
if di<23 then
begin
mi:=mi-3;
di:=di+9;
end
else
begin
mi:=mi-2;
di:=di-22;
end;
10:
if di<23 then
begin
mi:=7;
di:=di+8;
end
else
begin
mi:=8;
di:=di-22;
end;
11:
if di<22 then
begin
mi:=mi-3;
di:=di+9;
end
else
begin
mi:=mi-2;
di:=di-21;
end;
12:
if di<22 then
begin
mi:=mi-3;
di:=di+9;
end
else
begin
mi:=mi-2;
di:=di-21;
end;
end;
ytmp := yi - 1279 ;
ytmp := ytmp mod 4 ;
if (mi = 12) and (ytmp=0 )then
begin
Inc(di);
end;
y:=inttostr(yi);
m:=inttostr(mi);
if (length(m)=1) then
m:='0'+m;
d:=inttostr(di);
if length(d)=1 then
d:='0'+d;
Result :=y+'/'+m+'/'+d ;
end;
(**
* @todo: customint to windows standard DateTime
* @param (unsigned int64) customint for convert
* @result (TDateTime|float)
**)
function UIntToDateTime (const Number : UInt64 ):TDateTime ;
begin
Result := (Number / 100000);
end;
(**
* @todo: Gregorian date to customint
* @param (string) input for convert
* @result (LongWord)
**)
function GerToWord (const Date : string ) : LongWord;
var dy : Word;
dm,dd : Byte;
begin
dy := StrToIntDef(Copy(Date,1,4),2000);
dm := StrToIntDef(Copy(Date,6,2),0);
if (dm = 0) or (dm > 12) then
begin
Result := 0 ;
Exit;
end;
dd := StrToIntDef(Copy(Date,9,2),0);
if (dd = 0) or (dd > 31) then
begin
Result := 0 ;
Exit;
end;
Result := Round(EncodeDate(dy,dm,dd));
end;
(**
* @todo: Persian date to customint
* @param (string) input for convert
* @result (Int64)
**)
function PersianToInt (const DateSTr:string): Int64 ;
var diy,dim,did: Integer ;
i1,i2,i3,itotal : Int64;
begin
diy := StrToIntDef(Copy(DateSTr,1,4),0);
dim := StrToIntDef(Copy(DateSTr,6,2),0);
did := StrToIntDef(Copy(DateSTr,9,2),0);
i1 := diy - 1279 ;
Inc(i1);
itotal := i1 * 365 ;
i2 := i1 div 4 ; // + days ;
case dim of
1: i1 := 0 ;
2: i1 := 31 ;
3: i1 := 62 ;
4: i1 := 93 ;
5: i1 := 124 ;
6: i1 := 155 ;
7: i1 := 186 ;
8: i1 := 216 ;
9: i1 := 246 ;
10: i1 := 276 ;
11: i1 := 306 ;
12: i1 := 336 ;
end;
//Dec(did);
i3 := (i1 + did + i2)-285;
if itotal > 0 then
begin
itotal := itotal + i3 ;
end
else
begin
itotal := itotal - i3 ;
end;
Result := itotal ;
end;
(**
* @todo: return Persian day week
* @param (TDateTime) dt
* @result (byte)
**)
function GetPersianDayWeek(const dt: TDateTime):byte;
var
b : byte;
begin
b := DayOfWeek(dt);
if (b < 7) then
inc(b)
else
b := 1 ;
Result := b ;;
end;
(**
* @todo: Find the first Persian week from input date
* @param (TDateTime) to find persian date
* @result (TDateTime)
**)
function GetTheFirstDayOfThisMonth(const dt: TDateTime):TDateTime;
var
b : byte;
begin
b := StrToInt(Copy(GerToPersian(dt),9,2));
if b = 1 then
begin
Result := dt;
Exit;
end;
dec(b);
// ShowMessage(FloatToStr(dt));
Result := dt - b ;
// ShowMessage(GerToPersian(Result));
end;
(**
* @todo: Find the first Persian month from input date
* @param (TDateTime) to find persian date
* @result (TDateTime)
**)
function GetTheLastDayOfThisMonth(const dt: TDateTime):TDateTime;
var
b : byte;
i : integer;
dtt : TDateTime;
begin
b := StrToInt(Copy(GerToPersian(dt),9,2));
for i := 1 to 31 do
begin
dtt := dt + i;
b := StrToInt(Copy(GerToPersian(dtt),9,2));
if b = 1 then
begin
Result := dtt - 1;
Break;
end;
end;
end;
end.