-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpcheck.jspit.dt.php
2295 lines (1829 loc) · 83 KB
/
phpcheck.jspit.dt.php
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
//last modify: 2023-02-07
use Jspit\Dt;
//error_reporting(-1);
error_reporting(E_ALL ^ (E_WARNING | E_USER_WARNING));
ini_set('display_errors', 1);
header('Content-Type: text/html; charset=UTF-8');
//setlocale(LC_ALL, "german.UTF8"); //win
require __DIR__.'/../class/phpcheck.php';
require __DIR__.'/../class/class.debug.php';
//if check with macros
require __DIR__.'/../class/Macro.php';
$t = new PHPcheck(); //need min 1.3.17
//call with ?error=1 print only errors
$t->setOutputOnlyErrors(!empty($_GET['error']));
/*
* Tests Class
*/
$classFile = __DIR__.'/../class/Jspit/Dt.php';
require $classFile;
$useMacros = array_key_exists('Macro',class_uses(new Dt,false));
//für class.dt.php ab version 1.97
$t->start('check class version');
$info = $t->getClassVersion(Dt::class);
$t->check($info, !empty($info) AND $info >= 2.2);
$t->start('get hash of file');
$result = hash_file('crc32',$classFile);
$t->checkNotEqual($result,false);
$t->start('get numbers of lines');
$result = $t->getNumberOfLines($classFile);
$t->checkNotEqual($result,false);
$t->start('Check if extension IntlDateFormatter exists');
$msg = extension_loaded("Intl")
? 'ok'
: 'missing Intl extension'
;
$t->checkEqual($msg,'ok');
$t->start('set default Timezone and Language');
Dt::default_timezone_set('Europe/Berlin');
$result = Dt::setDefaultLanguage('en');
$t->checkEqual($result,true);
$t->start('get default Language');
$result =Dt::getDefaultLanguage();
$t->checkEqual($result,'en');
//create
$t->start('Create current Time (Now)');
$dt = Dt::create('now');
$ok = $dt->format('Y-m-d H:i:s') == date_create('now')->format('Y-m-d H:i:s');
$t->check($dt, $ok);
$t->start('cast dt object to string');
$dt = Dt::create('now');
$result = (string)$dt;
$t->checkEqual($result, $dt->format(Dt::TO_STRING_FORMAT));
$t->start('Create this Date 00:00 (Today)');
$dt = Dt::create('Today');
$t->check($dt, $dt == date_create('Today'));
$t->start('Create a fix Date dd.mm.yyyy HH:ii');
$date = Dt::create('6.11.1875 04:09');
$t->checkEqual((string)$date, '1875-11-06 04:09:00');
$t->start('Create a fix Date dd.mm.yy');
// German language required for this
Dt::setDefaultLanguage('de');
$date = Dt::create('5.6.15');
$t->checkEqual((string)$date, '2015-06-05 00:00:00');
$t->start('Create a fix Date dd.mm.');
$date = Dt::create('5.6.');
$t->checkEqual((string)$date, date('Y').'-06-05 00:00:00');
$t->start('Create a fix Date dd.mm.yy HH:ii');
$date = Dt::create('3.4.81 6:9');
$t->checkEqual((string)$date, '1981-04-03 06:09:00');
$t->start('Create a Date with Microseconds');
$date = Dt::create('1981-04-03 06:09:25.160000');
$t->checkEqual($date->toStringWithMicro(), '1981-04-03 06:09:25.160000');
$t->start('create Date B.C.(before christ)');
$date = Dt::create('-400-05-06');
$t->checkEqual((string)$date, '-0400-05-06 00:00:00');
$t->start('create Date B.C.(before christ)');
$date = Dt::create('-0036/05/06');
$t->checkEqual((string)$date, '-0036-05-06 00:00:00');
$t->start('Create a fix Date with english month');
$date = Dt::create('March 4, 2016');
$t->checkEqual((string)$date, '2016-03-04 00:00:00');
$t->start('Create a fix Date with german month');
$date = Dt::create('4.März 2016');
$t->checkEqual((string)$date, '2016-03-04 00:00:00');
$t->start('Create Date from year and Week-Number');
Dt::setDefaultLanguage('en');
$date = Dt::create('2015W50');
$t->checkEqual((string)$date, '2015-12-07 00:00:00');
$t->start('Create a Date easter monday 2016 from chain of strings');
$date = Dt::create('2016-1-1|{{easter}}|+1 Day');
$t->checkEqual((string)$date, '2016-03-28 00:00:00');
$t->start('Date easter monday 2016 from chain with alternative spelling');
$date = Dt::create('2016-1-1 and {{easter}} and +1 Day');
$t->checkEqual((string)$date, '2016-03-28 00:00:00');
$t->start('Create Date from DateTime-Object');
$tz = new DateTimeZone("UTC");
$dateTime = date_create('2000-01-02 08:00', $tz);
$date = Dt::create($dateTime);
$expect = Dt::create('2000-01-02 08:00:00',$tz);
$t->checkEqual(json_encode($date), json_encode($expect));
$t->start('Create Date with new Timezone from DateTime-Object');
$tz = new DateTimeZone("UTC");
$dateTime = date_create('2000-01-02 08:00', $tz);
$date = Dt::create($dateTime, "Europe/Berlin");
$expect = Dt::create('2000-01-02 09:00:00',"Europe/Berlin");
$t->checkEqual(json_encode($date), json_encode($expect));
$t->start('Create Date Timezone UTC');
$date = Dt::create('1.1.2014 00:00','UTC');
$t->checkEqual($date->formatL('Y-m-d H:i O'), '2014-01-01 00:00 +0000');
$t->start('Create Date Timezone New York');
$date = Dt::create('1.1.2014 00:00','America/New_York');
$t->checkEqual($date->formatL('Y-m-d H:i O'), '2014-01-01 00:00 -0500');
$t->start('create from Format Y-m-dTH:i:s.uZ');
$date = Dt::create('2020-02-06T17:26:38.27774Z');
$t->checkEqual($date->format('Y-m-d H:i:s.u e'), '2020-02-06 17:26:38.277740 Z');
$t->start('create from influxdb time format');
$date = Dt::create('2020-02-06T17:26:38.277740846Z');
$t->checkEqual($date->format('Y-m-d H:i:s.u e'), '2020-02-06 17:26:38.277741 Z');
$t->start('Create Date from Integer-Timestamp');
$timeStamp = strtotime('2015-12-24');
$date = Dt::create($timeStamp);
$t->checkEqual((string)$date, '2015-12-24 00:00:00');
$t->start('Create Date from Float-Timestamp');
$floatTimeStamp = strtotime('2015-12-25') + 2.5;
$date = Dt::create($floatTimeStamp);
$expected = '2015-12-25 00:00:02.500000';
$t->checkEqual($date->formatL('Y-m-d H:i:s.u'), $expected);
$t->start('Create Date from a big Float Timestamp with Milliseconds');
$date = Dt::create(35570432325.25);
$expected = '3097-03-07 20:38:45.250000';
$t->checkEqual($date->formatL('Y-m-d H:i:s.u'), $expected);
$t->start('Create Date from Millisecond Timestamp');
$milliseconds = '1601735792198';
$date = Dt::create($milliseconds/1000);
$expected = "2020-10-03T16:36:32.198000+02:00";
$t->checkEqual($date->formatL('Y-m-d\TH:i:s.uP'), $expected);
$t->start('Create Date from a big negative Float Timestamp');
$date = Dt::create(-63292409608.0);
$t->checkEqual((string)$date,'-0036-05-06 00:00:00');
$t->start('Create Date from a big negative Float Timestamp with @');
$date = Dt::create('@-2636450170.75','UTC');
$t->checkEqual($date->toStringWithMicro(),"1886-06-15 12:43:49.250000");
$t->start('Create Date from a Javascript Milliseconds Timestamp');
$timeStamp = 1393161787500;
$date = Dt::create($timeStamp/1000);
$t->checkEqual($date->toStringWithMicro(),"2014-02-23 14:23:07.500000");
$t->start('Create Date from a big negativ Float Timestamp');
$date = Dt::create(-22144579200.,'UTC');
$t->checkEqual((string)$date, '1268-04-07 00:00:00');
$t->start('Create from int arguments yaer, month..');
$dt = Dt::create(2019,5,1,12,23,45,12345);
$t->checkEqual($dt->toStringWithMicro(),"2019-05-01 12:23:45.012345");
$t->start('Create from arguments yaer=null, month, day + hour');
$dt = Dt::create(null,12,24,18); //24.12 18:00 this year
$t->checkEqual($dt->format('Y-m-d H:i:s'),date("Y")."-12-24 18:00:00");
$t->start('Create today at 18:30');
$dt = Dt::create(null,null,null,18,30);
$expected = Dt::create('today 18:30'); //better readable
$t->check((string)$dt, $dt == $expected);
$t->start('Create from int arguments yaer, month, day, timezone');
$dt = Dt::create(2019,5,1,"Europe/Moscow");
$t->checkEqual($dt->format('Y-m-d H:i:s e'),"2019-05-01 00:00:00 Europe/Moscow");
$t->start('Create from int arguments H:i curr.Time + timezone UTC');
$dt = Dt::create(2019,5,1,null,null,"UTC");
$t->checkEqual($dt->format('H:i'),gmdate('H:i'));
$t->start('Create from int arguments with negative year');
$dt = Dt::create(-432,5,1,"UTC");
$t->checkEqual($dt->format('Y-m-d e'),'-0432-05-01 UTC');
//create with wildcards
$t->start('Create Now with Wildcards');
$date = Dt::create("{{Y-m-d}}");
$t->checkEqual((string)$date, date("Y-m-d H:i:s"));
$t->start('Create Today with Wildcards');
$date = Dt::create("{{Y-m-d}} 00:00");
$t->checkEqual((string)$date, (string)Dt::create("Today"));
$t->start('Create easter date this year');
$date = Dt::create("{{easter}}");
$expected = Dt::create("Today")->setEasterDate();
$t->checkEqual((string)$date, (string)$expected);
$t->start('last Monday of October this year with Wildcard');
$date = Dt::create("last Monday of October {{year}}");
$expected = Dt::create("last Monday of October this year");
$t->checkEqual((string)$date, (string)$expected);
$t->start('Basis of Daynumber of 2017-08-01 (date("z")');
$date = Dt::create("2017-08-17 00:00|-{{z}} Days");
$expected = Dt::create("2017-01-01 00:00");
$t->checkEqual((string)$date, (string)$expected);
$t->start("if 2018-07-29 is Sunday (yes), take the next Monday");
$date = Dt::create("2018-07-29|{{?D=Sun}}next Monday");
$t->checkEqual((string)$date, "2018-07-30 00:00:00");
$t->start("if 2018-07-28 is Sunday (no), take the next Monday");
$date = Dt::create("2018-07-28|{{?D=Sun}}next Monday");
$t->checkEqual((string)$date, "2018-07-28 00:00:00");
$t->start('2 Month after 2017-06-15 13:30, Day 5 of this Month, same Time');
$date = Dt::create("2017-06-15 13:30|+2 month|{{Y}}-{{m}}-05");
$t->checkEqual((string)$date, "2017-08-05 13:30:00");
$t->start('crate dt with language en');
Dt::setDefaultLanguage('en');
$dt = Dt::create('2017-03-03T09:06:41.187');
$t->checkEqual($dt->toStringWithMicro(), "2017-03-03 09:06:41.187000");
$t->start('Date with relative Format');
$dt = Dt::create('first Sunday of Jun 2018 23:16:15.4');
$t->checkEqual((string)$dt, "2018-06-03 23:16:15");
$t->start('Date with relative Format language de');
Dt::setDefaultLanguage('de');
$dt = Dt::create('first Sunday of Jun 2018 23:16:15.4');
$t->checkEqual((string)$dt, "2018-06-03 23:16:15");
$t->start('Date with pm time');
$dt = Dt::create('Nov 7 2008 1:45 pm');
$t->checkEqual((string)$dt, "2008-11-07 13:45:00");
//create Dt from format
$t->start('crate dt from format');
$input = "Apr 19 '15 at 13:56";
$format = "M j 'y ?? H:i";
$dt = Dt::createDtFromFormat($format, $input);
$result = $dt ? (string)$dt : $dt;
$t->checkEqual($result, "2015-04-19 13:56:00");
$t->start('crate dt from format');
$input = "2020-01-07T11:55:34:438 GMT+0600";
$format = 'Y-m-d\Th:i:s:u \G\M\TO';
$dt = Dt::createDtFromFormat($format, $input);
$result = $dt ? $dt->format($format) : $dt;
$t->checkEqual($result, "2020-01-07T11:55:34:438000 GMT+0600");
$t->start('create from day of year starting with 1 and year (DOY)');
$input = "76 2021";
$dt = Dt::createDtFromFormat("!z Y",$input)->modify("-1 Day");
$t->checkEqual((string)$dt, "2021-03-17 00:00:00");
$t->start('crate dt from format with array of formats');
$input = '7/1/2018';
$formats = ['!Y/m/d','!m/d/Y'];
$dt = Dt::createDtFromFormat($formats,$input);
$t->checkEqual((string)$dt, "2018-07-01 00:00:00");
$t->start('crate dt from format strict mode');
Dt::setStrictModeCreate(true);
$input = "13 2017"; //13 is a invalid month
$dt = Dt::createDtFromFormat("!n Y", $input);
$t->checkEqual($dt, false);
$t->start('crate dt from format non strict mode');
Dt::setStrictModeCreate(false); //default false
$input = "13 2017";
$dt = Dt::createDtFromFormat("!n Y", $input);
$result = $dt ? (string)$dt : $dt;
$t->checkEqual($result, "2018-01-01 00:00:00");
$t->start('crate dt from format with french Month');
$input = "7 Août 2020";
Dt::setDefaultLanguage('fr');
$dt = Dt::createDtFromFormat("!d M Y", $input);
$result = $dt ? (string)$dt : $dt;
$t->checkEqual($result, "2020-08-07 00:00:00");
$t->start('Create from the Japanese');
//UTF-8 multibyte characters need * instead of ?
$input ='2008年11月13日';
$dt = Dt::createDtFromFormat('!Y*m*d*',$input);
$t->checkEqual((string)$dt, "2008-11-13 00:00:00");
//reset DefaultLanguage
Dt::setDefaultLanguage('en');
if(extension_loaded("Intl")) {
$t->start('createFromIntlFormat');
$input = "21 Août 2020";
//http://userguide.icu-project.org/formatparse/datetime
$dt = Dt::createFromIntlFormat('d MMMM yyyy',$input,'Europe/Paris','fr');
$result = $dt ? (string)$dt : $dt;
$t->checkEqual($result, "2020-08-21 00:00:00");
$t->start('createFromIntlFormat th@calendar=buddhist');
$input = "17 มกราคม 2507"; //January 17, 1964
$dt = Dt::createFromIntlFormat('d MMMM yyyy',$input,"Asia/Bangkok",'th@calendar=buddhist');
$result = $dt ? (string)$dt : $dt;
$t->checkEqual($result, "1964-01-17 00:00:00");
$t->start('createFromIntlFormat ja_JP@calendar=japanese');
$input = "平成30年8月1日"; //1 August 2018
$dt = Dt::createFromIntlFormat('LONG+NONE',$input,"Asia/Tokyo",'ja_JP@calendar=japanese');
$result = $dt ? (string)$dt : $dt;
$t->checkEqual($result, "2018-08-01 00:00:00");
$t->start('timeStampFromIntlFormat');
$input = "8 Août 20"; //2020-08-08
$ts = Dt::timeStampFromIntlFormat('d MMMM yy',$input,'Europe/Paris','fr');
$expected = date_create('2020-08-08',new DateTimeZone('Europe/Paris'))
->getTimeStamp();
$t->checkEqual($ts, $expected);
} //IntlDateFormatter
$t->start('crate from Julian Date Number');
$date = Dt::createFromJD(2458294.65168,"UTC");
$t->checkEqual((string)$date, "2018-06-25 03:38:25");
$t->start('crate from Julian Date Number (int)');
$jdNumber = 2345678;
$date = Dt::createFromJD($jdNumber,"UTC");
$arr = cal_from_jd($jdNumber,CAL_GREGORIAN);
$t->check($date->format('Y-m-d'), $arr['date'] === $date->format('n/j/Y'));
$t->start('crate from Microsoft Excel Timestamp (days since Dec 31 1899)');
$date = Dt::createFromMsTimestamp(43317.54,"UTC");
$t->checkEqual((string)$date, "2018-08-05 12:57:36");
$t->start('crate from Microsoft Excel Timestamp with Milliseconds');
$date = Dt::createFromMsTimestamp(5273.57305851856,"UTC");
$expected = "1914-06-08 13:45:12.256000";
$t->checkEqual($date->toStringWithMicro(), $expected);
$t->start('crate from Microsoft Excel string timestamp with comma');
//$strNumber = cell('10.09.2022') + cell('10:12:13,250')
$strNumber = '44814,4251533565';
$date = Dt::createFromMsTimestamp($strNumber,"UTC");
$expected = "2022-09-10 10:12:13.250000";
$t->checkEqual($date->toStringWithMicro(), $expected);
//createFromSystemTime
$t->start('createFromSystemTime');
//create a LabVIEW Timestamp from base 1 Jan 1904 with resulution 1ms
$testDate = "2006-12-13 09:45:55";
$basisDate = "1904-1-1";
$resolution = 0.001; //1ms
$timeStamp = -Dt::create($testDate,'UTC')->diffTotal($basisDate,"Seconds")/$resolution;
//createFromSystemTime
$date = Dt::createFromSystemTime($timeStamp,$basisDate,$resolution,"UTC");
$t->checkEqual((string)$date, $testDate);
$t->start('create dt from a LDAP Timestamp');
$timeStamp = 130981536000000000;
$date = Dt::createFromSystemTime($timeStamp,'1601-1-1',1.E-7,"UTC");
$expected = "2016-01-25 00:00:00";
$t->checkEqual((string)$date, $expected);
$t->start('create dt from a LDAP Timestamp with Microseconds');
$timeStamp ="132497313049180481";
$date = Dt::createFromSystemTime($timeStamp,'1601-1-1',1.E-7,"UTC");
$expected = Dt::create('2020-11-13 08:55:04.9180481','UTC');
$ok = abs($date->diffTotal($expected,'Sec')) < 0.00001; //tolerance 10 µs
$t->check($date->toStringWithMicro(), $ok);
$t->start('create dt from Mac Timestamp: seconds since Jan 1 1904');
$timeStamp = 3662360215;
$date = Dt::createFromSystemTime($timeStamp,'Jan 1 1904',1.0,"UTC");
$expected = "2020-01-20 10:16:55";
$t->checkEqual((string)$date, $expected);
$t->start('create dt from Microsoft Timestamp: days since Dec 31 1899');
$timeStamp = 43850.428414352;
$date = Dt::createFromSystemTime($timeStamp,'Dec 30 1899','1 Day',"UTC");
$expected = "2020-01-20 10:16:55";
$t->checkEqual((string)$date, $expected);
$t->start('create dt from Epoche Day: days since 1970-01-01');
$epocheDay = 18740.25;
$date = Dt::createFromSystemTime($epocheDay,'1970-01-01',86400,"UTC");
$expected = "2021-04-23 06:00:00";
$t->checkEqual((string)$date, $expected);
$t->start('calculate Epoche Day from Dt');
$dt = Dt::create('2021-04-23 12:00:00','UTC');
$epocheDay = Dt::create('1970-01-01','UTC')->diffTotal($dt,'Days');
$t->checkEqual($epocheDay, 18740.5);
$t->start('convert c# DateTime.ToBinary() to DateTime');
$numFromCsharp = -8586307756854775808; //2 bit kind + 62 bit ticks
//remove kind bits
$removeKindBits = function($num){
if(PHP_INT_SIZE > 4) return (int)$num & 0x3fffffffffffffff;
if($num < 0) $num += 9223372036854775808;
$b2 = 4611686018427387903;
if($num > $b2) $num -= $b2;
return $num;
};
$ticks = $removeKindBits($numFromCsharp);
$date = Dt::createFromSystemTime($ticks,'0001-1-1',1.E-7,"UTC");
$expected = "2019-10-11 22:00:00";
$t->checkEqual((string)$date, $expected);
//
$t->start('Create Date from not valid String');
$t->setErrorLevel(0);//error reporting off for this test
$date = Dt::create('31.0x.2015');
$t->restoreErrorLevel();
$t->checkEqual($date, false);
$t->start('Get last Errormassage');
$errorInfo = Dt::getLastErrorsAsString() ;
$t->check($errorInfo, $errorInfo != '');
$t->start('Parse a invalid Date: 31.02.');
$date = Dt::create('31.02.2015'); //set to 2015-03-03
$t->checkEqual((string)$date, '2015-03-03 00:00:00');
$t->start('getErrorInfo() ');
$errInfo = $date->getErrorInfo();
$t->check($errInfo, !empty($errInfo));
$t->start('Parse a invalid Date with StrictMode');
Dt::setStrictModeCreate(true); //default false
$date = Dt::create('31.02.2015');
$t->checkEqual($date, false);
$t->start('Create Date with regular Expressions');
$str = '24 December';
$regEx = '~(?<d>\d{1,2}) (?<F>\w+)~';
$dateTemplate = "1.1.2010 18:00";
$date = Dt::createFromRegExFormat($regEx,$str,null,$dateTemplate);
$t->checkEqual((string)$date,'2010-12-24 18:00:00');
$t->start('Create Date with regular Expressions');
$str = '2.5 19 Uhr 30';
$regEx = '~(?<d>\d{1,2})\.(?<m>\d{1,2}) ?(?<H>\d{1,2}) Uhr (?<i>\d{1,2})~';
$date = Dt::createFromRegExFormat($regEx,$str);
$t->checkEqual((string)$date,date('Y').'-05-02 19:30:00');
$t->start('Create Date with regular Expressions and negative Year');
$str = '-45/8';
$regEx = '~^(?<Y>-?[\d]{1,4})/(?<m>\d{1,2})$~';
$dateTemplate = "0000-01-01";
$date = Dt::createFromRegExFormat($regEx,$str,null,$dateTemplate);
$t->checkEqual((string)$date,'-0045-08-01 00:00:00');
$t->start('get matches from last RegEx ');
$matches = $date->getMatchLastCreateRegEx();
$expected = [
0 => "-45/8",
'Y' => "-45",
1 => "-45",
'm' => "8",
2 => "8",
];
$t->checkEqual($matches,$expected);
$t->start('Create date using a array of regular expressions');
$regEx = [
'~^(?<d>\d{2})/(?<m>\d{2})/(?<Y>\d{4})$~',
'~^(?<d>\d{2})/(?<M>\p{L}+)/(?<Y>\d{4})$~iu'
];
$date1 = Dt::createFromRegExFormat($regEx , "18/Feb/2020");
$date2 = Dt::createFromRegExFormat($regEx , "05/07/1968");
$result = $date1.'|'.$date2;
$expected = '2020-02-18 00:00:00|1968-07-05 00:00:00';
$t->checkEqual($result,$expected);
$t->start('createFromRegExFormat with strict Mode');
$t->setErrorLevel(0);//error reporting off for this test
Dt::setStrictModeCreate(true);
$regEx = '~^(?<d>\d{2})(?<m>\d{2})(?<Y>\d{4})$~';
$dt = Dt::createFromRegExFormat($regEx,'33112022');
Dt::setStrictModeCreate(false);
$t->restoreErrorLevel();
$t->checkEqual($dt,false);
//formatL
$t->start('format with German short Weekday');
$strDate = Dt::create('2014-12-20')->formatL('D, d.m.Y','de');
$t->checkEqual($strDate,'Sam, 20.12.2014');
$t->start('format with English short Weekday');
$strDate = Dt::create('2014-12-20')->formatL('D, d.m.Y','en');
$t->checkEqual($strDate,'Sat, 20.12.2014');
if(extension_loaded("Intl")){ //with IntlDateFormatter
$t->start('IntlDateFormatter exists: language "fr"');
Dt::setDefaultLanguage('en');
$strDate = Dt::create('14.1.2015')->formatL('l, d F Y','fr');
$t->checkEqual($strDate,'mercredi, 14 janvier 2015');
$t->start('format are IntlDateFormatter Constants');
$strDate = Dt::create('14.1.2015')->formatL('FULL+SHORT','pl');
$t->checkEqual($strDate,'środa, 14 stycznia 2015 00:00');
$t->start('format with buddhist calendar');
$strDate = Dt::create('14.1.2015 16:45')->formatL('FULL+SHORT','es_ES@calendar=buddhist');
$t->checkContains($strDate,'miércoles,14,enero,2558,BE,16:45');
$t->start('format with persian calendar + user-format');
$icuFormat = "yyyy-MM-dd"; //ICU must use if calendar not gregorian
$lang = "ir_IR@calendar=persian";
$date = Dt::create("2018-2-19")->formatL($icuFormat,$lang);
$t->checkEqual($date, "1396-11-30");
$t->start('format with persian calendar + user-format');
$icuFormat = "yyyy-MM-dd"; //ICU must use if calendar not gregorian
$lang = "ir_IR@calendar=persian";
$date = Dt::create("2018-2-19")->formatL($icuFormat,$lang);
$t->checkEqual($date, "1396-11-30");
$t->start('format fa_IR with persian calendar + user-format');
$icuFormat = "yyyy:MM:dd"; //ICU must use if calendar not gregorian
$lang = "fa_IR@calendar=persian";
$date = Dt::create("2018-2-19")->formatL($icuFormat,$lang);
$t->checkEqual($date, "۱۳۹۶:۱۱:۳۰");
$t->start('format with persian calendar,user-format');
$icuFormat = "MMMM d, yyyy"; //ICU must use if calendar not gregorian
$lang = "ir_IR@calendar=persian";
$date = Dt::create("2018-2-19")->formatL($icuFormat,$lang);
$t->checkEqual($date, "Bahman 30, 1396");
$t->start('formatIntl with quarter');
$date = Dt::create("2018-2-19")->formatIntl("qqqq y","en");
$t->checkEqual($date, '1st quarter 2018');
$t->start('formatIntl with persian calendar + user-format');
$icuFormat = "yyyy-MM-dd"; //must use if calendar not gregorian
$lang = "ir_IR@calendar=persian";
$date = Dt::create("2018-2-19")->formatIntl($icuFormat,$lang);
$t->checkEqual($date, "1396-11-30");
/*
* input other language
*/
$t->start('Input in Russian');
// date from strings given in various languages
Dt::setDefaultLanguage('ru');
$date = Dt::create("1 октября 1990");
$t->check($date, $date == Dt::create("1990-10-01"));
$t->start('Input in danish');
Dt::setDefaultLanguage('DA');
$date = Dt::create("3. maj 2018 06:39");
$t->check($date, $date == Dt::create("2018-05-03 06:39:00"));
$t->start('Input in Spanish');
Dt::setDefaultLanguage('es');
$date = Dt::create('2 de enero de 1992');
$t->check($date, $date == Dt::create("1992-01-02 00:00:00"));
$t->start('Input in Greek');
Dt::setDefaultLanguage('el');
$date = Dt::create('13 Δεκεμβρίου 2007 12:45 μ.μ.');
$t->check($date, $date == Dt::create("2007-12-13 12:45:00"));
$t->start('createDtFromFormat with with Finnish month name');
Dt::setDefaultLanguage('fi');
$input = "18/Helmi/2020";
$date = Dt::createDtFromFormat('|d/M/Y', $input);
$t->check($date, $date == Dt::create("2020-02-18 00:00:00"));
$t->start('createDtFromFormat with abbreviations of Italian month names');
//A list is required for names with less than 3 letters
$list = 'genn,febbr,mar,apr,magg,giugno,luglio,ag,sett,ott,nov,dic,
lunedì,martedì,mercoledì,giovedì,venerdì,sabato,domenica';
Dt::addLanguage("it",$list);
Dt::setDefaultLanguage('it');
$dt = dt::createDtFromFormat('!My','ag20');
$strDate = $dt->formatL('l j F Y');
$t->checkEqual($strDate, "sabato 1 ag 2020");
$t->start('createFromRegExFormat with with Estonian month name');
Dt::setDefaultLanguage('et'); //estnisch
$str = '26/Veebr/2005/13:45';
$regEx = '~(?<d>\d{1,2})/(?<M>\w+)/(?<Y>\d{4,4})/(?<H>\d{1,2}):(?<i>\d{1,2})~';
$date = Dt::createFromRegExFormat($regEx,$str);
$result = $date ? $date->formatL('d.F Y H:i') : "Error";
$expected = "26.veebruar 2005 13:45";
$t->checkEqual($result, $expected);
//set default for checks
Dt::setDefaultLanguage('en');
//end checke with IntlDateFormatter
}
else {
$t->start("IntlDateFormatter not available");
$s = "IntlDateFormatter not available, neeed for some test's";
$t->check($s,true);
}
//utcFormat
$t->start('utcFormat: time is UTC/GMT');
$dt = Dt::create('2019-10-10 03:00', 'Europe/Bucharest');
$utcTime = $dt->utcFormat();
$t->checkEqual($utcTime, "2019-10-10 00:00:00");
$t->start('utcFormat: time is UTC/GMT');
$dt = Dt::create('2019-07-10 02:45', 'Europe/Berlin');
$utcTime = $dt->utcFormat('d.m.Y H:i');
$t->checkEqual($utcTime, "10.07.2019 00:45");
$t->start('Add another language');
$list = "janvier,février,mars,avri,mai,juin,juillet,août,septembre,octobre,novembre,décembre,
lundi,mardi,mercredi,jeudi,vendredi,samedi,dimanche,
secondes,minutes,heures,jours,semaines,mois,années,an";
Dt::addLanguage("fr",$list);
$strDate = Dt::create('14.1.2015')->formatL('l, d F Y','fr');
$t->checkEqual($strDate,'mercredi, 14 janvier 2015');
//setTime
$t->start('setTime(): set the time for a date');
// Time manipulation with setTime
$date =Dt::create('4.6.2011 18:00')->setTime(9,15);
$t->checkEqual((string)$date,'2011-06-04 09:15:00');
$t->start('setTime(): set hour and minute to 0 ');
$date =Dt::create('4.6.2011 18:15:46')->setTime(9);
$t->checkEqual((string)$date,'2011-06-04 09:00:00');
$t->start('setTime(): set minute to 6, second to 0 ');
$date =Dt::create('4.6.2011 18:15:46')->setTime(null,6);
$t->checkEqual((string)$date,'2011-06-04 18:06:00');
$t->start('setTime(hh:mm): set the time for a date');
$date =Dt::create('4.6.2011 18:00')->setTime('10:14');
$t->checkEqual((string)$date,'2011-06-04 10:14:00');
$t->start('set to the start of day with setTime(hh:mm)');
$date =Dt::create('4.6.2011 18:00')->setTime('00:00');
$t->checkEqual((string)$date,'2011-06-04 00:00:00');
$t->start('setTime from another dt/date object');
$dateWithTime = date_create('2011-1-1 13:45');
$date =Dt::create('2012-12-08')->setTime($dateWithTime);
$t->checkEqual((string)$date,'2012-12-08 13:45:00');
$t->start('setTime with microseconds');
$date =Dt::create('2012-12-08')->setTime(12,30,15,12345);
$t->checkEqual($date->toStringWithMicro(), '2012-12-08 12:30:15.012345');
//setMicroSeconds
$t->start('set microseconds part');
$date = Dt::create('2011-1-1 13:45:10')->setMicroSeconds(250000);
$t->checkEqual($date->toStringWithMicro(), '2011-01-01 13:45:10.250000');
$t->start('set Seconds and Milliseconds');
$date = Dt::create('2011-1-1 13:45:10')->setSecond(3.5);
$t->checkEqual($date->toStringWithMicro(), '2011-01-01 13:45:03.500000');
$t->start('set Seconds and Milliseconds from dt object');
$dt = Dt::create('2016-10-09 10:00:55.567');
$date = Dt::create('2011-1-1 13:45:10')->setSecond($dt);
$t->checkEqual($date->toStringWithMicro(), '2011-01-01 13:45:55.567000');
$t->start('set Minute');
$date = Dt::create('2011-1-1 13:45:10')->setMinute(30);
$t->checkEqual((string)$date, '2011-01-01 13:30:10');
$t->start('set Hour');
$date = Dt::create('2011-1-1 13:45:10')->setHour(5);
$t->checkEqual((string)$date, '2011-01-01 05:45:10');
//setDate
$t->start('setDate: set the year');
// Manipulate the date with setDate
$date = Dt::create('2011-1-1 13:45')->setDate(2015);
$t->checkEqual((string)$date, '2015-01-01 13:45:00');
$t->start('setDate: set the month to May');
$date = Dt::create('2011-1-1 13:45')->setDate(null,5);
$t->checkEqual((string)$date, '2011-05-01 13:45:00');
$t->start('setDate: set the day to 16');
$date = Dt::create('2011-1-1 13:45')->setDate(null,null,16);
$t->checkEqual((string)$date, '2011-01-16 13:45:00');
$t->start('setDate: set year, month and day');
$date = Dt::create('2011-1-1 13:45')->setDate(2015,5,16);
$t->checkEqual((string)$date, '2015-05-16 13:45:00');
$t->start('setDate: set year, month and day from datestring');
$date = Dt::create('2011-1-1 13:45')->setDate("16.5.2015");
$t->checkEqual((string)$date, '2015-05-16 13:45:00');
$t->start('setDate: full date from dt/date-object');
$dateObject = Dt::create("2015/5/16");
$dateWithTime = Dt::create('2011-1-1 13:45')->setDate($dateObject);
$t->checkEqual((string)$dateWithTime, '2015-05-16 13:45:00');
$t->start('setYear: get year from dt/date-object');
$dateObject = Dt::create("2015/5/16");
$dateWithTime = Dt::create('2011-1-1 13:45')->setYear($dateObject);
$t->checkEqual((string)$dateWithTime, '2015-01-01 13:45:00');
$t->start('setYear: get year from a date string');
$strDate = "2016-12-24 18:54";
$dateWithTime = Dt::create('2011-3-12 11:45:10')->setYear($strDate);
$t->checkEqual((string)$dateWithTime, '2016-03-12 11:45:10');
$t->start('setYear: get year from a relative date string');
$dateWithTime = Dt::create('2011-3-12 11:45:10')->setYear('next year');
$expected = (date('Y')+1).'-03-12 11:45:10';
$t->checkEqual((string)$dateWithTime, $expected);
$t->start('setYear: use current year');
$dateWithTime = Dt::create('2011-06-12 14:15')->setYear();
$t->checkEqual((string)$dateWithTime, date('Y').'-06-12 14:15:00');
$t->start('setYear: get next year in future');
$dateWithTime = Dt::create('2011-01-01 00:00:56')->setYear(true);
$t->checkEqual((string)$dateWithTime, (date('Y')+1).'-01-01 00:00:56');
$t->start('setYear: set year with fix number');
$dateWithTime = Dt::create('2012-10-15 16:25')->setYear(2015);
$t->checkEqual((string)$dateWithTime, '2015-10-15 16:25:00');
$t->start('set the ISOweek');
$date =Dt::create('2014-5-6 07:04:30')->setISOweek(51);
$t->checkEqual((string)$date, '2014-12-15 07:04:30');
$t->start('setDateTimeFrom DateTime');
$dateTime = date_create('2019-08-31 UTC');
$date = Dt::create('1988-01-02 11:24','Europe/Berlin')
->setDateTimeFrom($dateTime);
$testOk = ($date == $dateTime AND $date !== $dateTime);
$t->check($date->format('Y-m-d H:i:s.u e'), $testOk);
//get..
$t->start('get Seconds of Day');
$seconds = Dt::create('2014-5-6 07:04:30')->getDaySeconds();
$t->checkEqual($seconds, 7*60*60 + 4*60 + 30);
$t->start('get Minutes of Day');
$seconds = Dt::create('2014-5-6 07:04:30')->getDayMinutes();
$t->checkEqual($seconds, 7*60 + 4);
$t->start('get microseconds part');
$microSeconds = Dt::create('2014-5-6 07:04:30.25')->getMicroSeconds();
$t->checkEqual($microSeconds, 250000);
$t->start('get count Days after 0001-01-01');
$dayCount = Dt::create('18.12.2014','UTC')->toDays();
$t->checkEqual($dayCount, 735585);
$t->start('get age in years');
$age = Dt::create('now -14 years')->age();
$t->checkEqual($age, 14);
$t->start('get age in years with time 23:59');
$date = Dt::create('now -14 years')->setTime('23:59:59');
$age = $date->age();
$t->checkEqual($age, 13);
$t->start('get age in years with time 23:59 and param true');
$date = Dt::create('now -14 years')->setTime('23:59:59');
$age = $date->age(true); //true set time for age to 00:00
$t->checkEqual($age, 14);
//countDaysTo
$t->start('get count Sundays between 31.8.-9.9.19');
$result = Dt::create('2019-8-31')->countDaysTo('Sun','2019-9-9');
$t->checkEqual($result, 2);
$t->start('get count Sundays between 1.9.-9.9.19');
$result = Dt::create('2019-9-1')->countDaysTo('Sun','2019-9-9');
$t->checkEqual($result, 2);
$t->start('get count Sat + Sundays between 30.8.-9.9.19');
$result = Dt::create('2019-8-30')->countDaysTo('Sat,Sun','2019-9-9');
$t->checkEqual($result, 4);
$t->start('get count Sundays between 1.9.-8.9.19');
$result = Dt::create('2019-9-1')->countDaysTo('Sun','2019-9-8');
$t->checkEqual($result, 2);
$t->start('get count Sundays between 2.9.-8.9.19');
$result = Dt::create('2019-9-2')->countDaysTo('Sun','2019-9-8');
$t->checkEqual($result, 1);
$t->start('get count Sundays between 1.9.-7.9.19');
$result = Dt::create('2019-9-1')->countDaysTo('Sun','2019-9-7');
$t->checkEqual($result, 1);
$t->start('get count Sundays between 2.9.-7.9.19');
$result = Dt::create('2019-9-2')->countDaysTo('Sun','2019-9-7');
$t->checkEqual($result, 0);
$t->start('get count Sundays between 9.9.-1.9.19');
$result = Dt::create('2019-9-9')->countDaysTo('Sun','2019-9-1');
$t->checkEqual($result, -2);
$t->start('get count the Weekday(current Day) from Today to +1 week exclude dateTo');
$result = Dt::create('Today')->countDaysTo('Today','+1 week', true);
$t->checkEqual($result, 1);
$t->start('Count all weekdays in October 2019, Mon-Fri as Numbers 1..5');
$result = Dt::create('2019-10-01')->countDaysTo('1,2,3,4,5','2019-10-31');
$t->checkEqual($result, 23);
$t->start('Count all days in October 2019');
$result = Dt::create('2019-10-01')->countDaysTo('all','2019-10-31');
$t->checkEqual($result, 31);
$t->start('get count with wrong day');
$t->setErrorLevel(0);//error reporting off for next 2 tests
$result = Dt::create('2019-9-9')->countDaysTo('Sam','2019-9-1');
$t->checkEqual($result, false);
$t->start('get count with wrong dateTo');
$result = Dt::create('2019-9-9')->countDaysTo('Sat','xx');
$t->restoreErrorLevel();
$t->checkEqual($result, false);
$t->start('get quarter number of 2015-3-31');
$result = Dt::create('2015-3-31')->getQuarter();
$t->checkEqual($result, 1);
$t->start('get quarter number of 2015-4-1');
$result = Dt::create('2015-4-1')->getQuarter();
$t->checkEqual($result, 2);
$t->start('get DateTime-Object');
$dateTime = Dt::create('2014-09-23','America/New_York')
->getDateTime()
;
$ok = $dateTime instanceof DateTime
&& !($dateTime instanceof dt)
&& $dateTime->format('Y-m-d e') === '2014-09-23 America/New_York'
;
$t->check($dateTime, $ok);
$t->setErrorLevel(0);//error reporting off for this test
$t->start('get unknown property : false');
/*
* Properties
*/
$closure = function(){
$result = Dt::create('2017-11-18')->unknown;
};
$t->checkException($closure);
$t->restoreErrorLevel();
$t->start('get year as integer');
$result = Dt::create('2015-4-1 06:15:46')->year;
$t->checkEqual($result, 2015);
$t->start('get month as integer');
$result = Dt::create('2015-4-1 06:15:46')->month;
$t->checkEqual($result, 4);
$t->start('get day as integer');
$result = Dt::create('2015-04-07 06:15:46')->day;
$t->checkEqual($result, 7);
$t->start('get short day name');
$result = Dt::create('2015-04-07 06:15:46')->dayName;
$t->checkEqual($result, "Tue");
$t->start('get full day name');
$result = Dt::create('2015-04-07 06:15:46')->fullDayName;
$t->checkEqual($result, "Tuesday");
$t->start('get hour as integer');
$result = Dt::create('2015-4-14 06:15:46')->hour;
$t->checkEqual($result, 6);
$t->start('get minute as integer');
$result = Dt::create('2015-4-14 06:15:46')->minute;
$t->checkEqual($result, 15);
$t->start('get second as integer');
$result = Dt::create('2015-4-14 06:15:46')->second;
$t->checkEqual($result, 46);
$t->start('get microSecond as integer');
$result = Dt::create('2015-4-14 06:15:46.123')->microSecond;
$t->checkEqual($result, 123000);
$t->start('get dayOfWeek ISO 8601 Monday: 1');
//ISO 8601 1: Monday .. 7: Sunday
$result = Dt::create('next Monday')->dayOfWeek;
$t->checkEqual($result, 1);
$t->start('get dayOfWeek ISO 8601 Saturday: 6');
//ISO 8601 1: Monday .. 7: Sunday
$result = Dt::create('next Saturday')->dayOfWeek;
$t->checkEqual($result, 6);
$t->start('get dayOfWeek ISO 8601 Sunday: 7');
//ISO 8601 1: Monday .. 7: Sunday
$result = Dt::create('next Sunday')->dayOfWeek;
$t->checkEqual($result, 7);
$t->start('get daysInMonth');
//Nov has 30 days
$result = Dt::create('2017-11-18')->daysInMonth;
$t->checkEqual($result, 30);
$t->start('get dayOfYear from 2017-11-18 as integer');
//Day of the year starts with 1 on 1.1.
$result = Dt::create('2017-11-18')->dayOfYear;
$t->checkEqual($result, 322);
$t->start('get weekOfMonth from 2017-11-18 as integer');
$result = Dt::create('2017-11-18')->weekOfMonth;
$t->checkEqual($result, 3);
$t->start('get weekOfMonth from Sun 2016-10-02 as integer');
$result = Dt::create('2016-10-02')->weekOfMonth;
$t->checkEqual($result, 1);
$t->start('get weekOfMonth from Mon 2016-10-03 as integer');
$result = Dt::create('2016-10-03')->weekOfMonth;
$t->checkEqual($result, 2);
$t->start('get weekOfMonth from 2016-10-31 as integer');
$result = Dt::create('2016-10-31')->weekOfMonth;
$t->checkEqual($result, 6);
$t->start('get weekOfMonth from Mon 2019-12-30 as integer');
$result = Dt::create('2019-12-30')->weekOfMonth;
$t->checkEqual($result, 6);
$t->start('application example for weekOfMonth');
$strDate= '18.3.2017';
$dt = Dt::create($strDate);
$str = $strDate.' ist der '
.$dt->weekOfMonth
.$dt->formatL('\. l \i\m F Y','de') //names in german
;
$t->checkContains($str, '3.,Sam');
$t->start('create a relative Date-String with weekOfMonth');
$no = [1 => 'first','second','third','fourth','fifth'];
$strDate = '18.3.2017';
$dt = Dt::create($strDate);
$str = $no[$dt->weekOfMonth].$dt->format(' l \o\f F Y');
//'third Saturday of March 2017'
$t->check($str, date_create($str) == date_create($strDate));
$t->start('get weekOfYear from 2017-11-18 as integer');
// ISO-8601 week number of year, weeks starting on Monday
$result = Dt::create('2017-11-18')->weekOfYear;
$t->checkEqual($result, 46);
$t->start('get weekOf1900 from 1900-01-01 as integer');
$result = Dt::create('1900-01-01')->weekOf1900;
$t->checkEqual($result, 1);
$t->start('get weeks from 1900 to 2013-01-16 06:48 as integer');