-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
TimeTest.php
1021 lines (786 loc) · 27.9 KB
/
TimeTest.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
namespace CodeIgniter\I18n;
use DateTime;
use DateTimeZone;
use IntlDateFormatter;
class TimeTest extends \CodeIgniter\Test\CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();
helper('date');
\Locale::setDefault('America/Chicago');
}
public function testNewTimeNow()
{
$formatter = new IntlDateFormatter(
'en_US', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, 'America/Chicago', // Default for CodeIgniter
IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd HH:mm:ss'
);
$time = new Time(null, 'America/Chicago');
$this->assertEquals($formatter->format($time), (string) $time);
}
public function testTimeWithTimezone()
{
$formatter = new IntlDateFormatter(
'en_US', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, 'Europe/London', // Default for CodeIgniter
IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd HH:mm:ss'
);
$time = new Time('now', 'Europe/London');
$this->assertEquals($formatter->format($time), (string) $time);
}
public function testTimeWithTimezoneAndLocale()
{
$formatter = new IntlDateFormatter(
'fr_FR', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, 'Europe/London', // Default for CodeIgniter
IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd HH:mm:ss'
);
$time = new Time('now', 'Europe/London', 'fr_FR');
$this->assertEquals($formatter->format($time), (string) $time);
}
public function testTimeWithDateTimeZone()
{
$formatter = new IntlDateFormatter(
'fr_FR', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, 'Europe/London', IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd HH:mm:ss'
);
$time = new Time('now', new \DateTimeZone('Europe/London'), 'fr_FR');
$this->assertEquals($formatter->format($time), (string) $time);
}
public function testToDateTime()
{
$time = new Time();
$obj = $time->toDateTime();
$this->assertInstanceOf(\DateTime::class, $obj);
}
public function testNow()
{
$time = Time::now();
$time1 = new \DateTime();
$this->assertInstanceOf(Time::class, $time);
$this->assertEquals($time->getTimestamp(), $time1->getTimestamp());
}
public function testParse()
{
$time = Time::parse('next Tuesday', 'America/Chicago');
$time1 = new \DateTime('now', new \DateTimeZone('America/Chicago'));
$time1->modify('next Tuesday');
$this->assertEquals($time->getTimestamp(), $time1->getTimestamp());
}
public function testToDateTimeString()
{
$time = Time::parse('2017-01-12 00:00', 'America/Chicago');
$this->assertEquals('2017-01-12 00:00:00', (string) $time);
$this->assertEquals('2017-01-12 00:00:00', $time->toDateTimeString());
}
public function testToDateTimeStringWithTimeZone()
{
$time = Time::parse('2017-01-12 00:00', 'Europe/London');
$expects = new \DateTime('2017-01-12', new \DateTimeZone('Europe/London'));
$this->assertEquals($expects->format('Y-m-d H:i:s'), $time->toDateTimeString());
}
public function testToday()
{
$time = Time::today();
$this->assertEquals(date('Y-m-d 00:00:00'), $time->toDateTimeString());
}
public function testTodayLocalized()
{
$time = Time::today('Europe/London');
$this->assertEquals(date('Y-m-d 00:00:00'), $time->toDateTimeString());
}
public function testYesterday()
{
$time = Time::yesterday();
$this->assertEquals(date('Y-m-d 00:00:00', strtotime('-1 day')), $time->toDateTimeString());
}
public function testTomorrow()
{
$time = Time::tomorrow();
$this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 day')), $time->toDateTimeString());
}
public function testCreateFromDate()
{
$time = Time::createFromDate(2017, 03, 05, 'America/Chicago');
$this->assertEquals(date('Y-m-d 00:00:00', strtotime('2017-03-05 00:00:00')), $time->toDateTimeString());
}
public function testCreateFromDateLocalized()
{
$time = Time::createFromDate(2017, 03, 05, 'Europe/London');
$this->assertEquals(date('Y-m-d 00:00:00', strtotime('2017-03-05 00:00:00')), $time->toDateTimeString());
}
public function testCreateFromTime()
{
$time = Time::createFromTime(10, 03, 05, 'America/Chicago');
$this->assertEquals(date('Y-m-d 10:03:05'), $time->toDateTimeString());
}
public function testCreateFromTimeEvening()
{
$time = Time::createFromTime(20, 03, 05, 'America/Chicago');
$this->assertEquals(date('Y-m-d 20:03:05'), $time->toDateTimeString());
}
public function testCreateFromTimeLocalized()
{
$time = Time::createFromTime(10, 03, 05, 'Europe/London');
$this->assertCloseEnoughString(date('Y-m-d 10:03:05'), $time->toDateTimeString());
}
public function testCreateFromFormat()
{
$now = new \DateTime('now');
Time::setTestNow($now);
$time = Time::createFromFormat('F j, Y', 'January 15, 2017', 'America/Chicago');
$this->assertCloseEnoughString(date('2017-01-15 H:i:s', $now->getTimestamp()), $time->toDateTimeString());
Time::setTestNow();
}
public function testCreateFromFormatWithTimezoneString()
{
$time = Time::createFromFormat('F j, Y', 'January 15, 2017', 'Europe/London');
$this->assertCloseEnoughString(date('2017-01-15 H:i:s'), $time->toDateTimeString());
}
public function testCreateFromFormatWithTimezoneObject()
{
$tz = new \DateTimeZone('Europe/London');
$time = Time::createFromFormat('F j, Y', 'January 15, 2017', $tz);
$this->assertCloseEnoughString(date('2017-01-15 H:i:s'), $time->toDateTimeString());
}
public function testCreateFromTimestamp()
{
$time = Time::createFromTimestamp(strtotime('2017-03-18 midnight'));
$this->assertEquals(date('2017-03-18 00:00:00'), $time->toDateTimeString());
}
public function testTestNow()
{
$this->assertFalse(Time::hasTestNow());
$this->assertCloseEnoughString(date('Y-m-d H:i:s', time()), Time::now()->toDateTimeString());
$t = new Time('2000-01-02');
Time::setTestNow($t);
$this->assertTrue(Time::hasTestNow());
$this->assertEquals('2000-01-02 00:00:00', Time::now()->toDateTimeString());
Time::setTestNow();
$this->assertCloseEnoughString(date('Y-m-d H:i:s', time()), Time::now()->toDateTimeString());
}
//--------------------------------------------------------------------
public function testMagicIssetTrue()
{
$time = Time::parse('January 1, 2016');
$this->assertTrue(isset($time->year));
}
public function testMagicIssetFalse()
{
$time = Time::parse('January 1, 2016');
$this->assertFalse(isset($time->foobar));
}
//--------------------------------------------------------------------
public function testGetYear()
{
$time = Time::parse('January 1, 2016');
$time2 = Time::parse('December 31, 2019');
$this->assertEquals(2016, $time->year);
$this->assertEquals(2019, $time2->year);
}
public function testGetMonth()
{
$time = Time::parse('August 1, 2016');
$this->assertEquals(8, $time->month);
}
public function testGetDay()
{
$time = Time::parse('August 12, 2016');
$this->assertEquals(12, $time->day);
}
public function testGetHour()
{
$time = Time::parse('August 12, 2016 4:15pm');
$this->assertEquals(16, $time->hour);
}
public function testGetMinute()
{
$time = Time::parse('August 12, 2016 4:15pm');
$this->assertEquals(15, $time->minute);
}
public function testGetSecond()
{
$time = Time::parse('August 12, 2016 4:15:23pm');
$this->assertEquals(23, $time->second);
}
public function testGetDayOfWeek()
{
$time = Time::parse('August 12, 2016 4:15:23pm');
$this->assertEquals(6, $time->dayOfWeek);
}
public function testGetDayOfYear()
{
$time = Time::parse('August 12, 2016 4:15:23pm');
$this->assertEquals(225, $time->dayOfYear);
}
public function testGetWeekOfMonth()
{
$time = Time::parse('August 12, 2016 4:15:23pm');
$this->assertEquals(2, $time->weekOfMonth);
}
public function testGetWeekOfYear()
{
$time = Time::parse('August 12, 2016 4:15:23pm');
$this->assertEquals(33, $time->weekOfYear);
}
public function testGetTimestamp()
{
$time = Time::parse('August 12, 2016 4:15:23pm');
$expected = strtotime('August 12, 2016 4:15:23pm');
$this->assertEquals($expected, $time->timestamp);
}
public function testGetAge()
{
$time = Time::parse('5 years ago');
$this->assertEquals(5, $time->age);
}
public function testAgeNow()
{
$time = new Time();
$this->assertEquals(0, $time->age);
}
public function testAgeFuture()
{
$time = Time::parse('August 12, 2116 4:15:23pm');
$this->assertEquals(0, $time->age);
}
public function testGetQuarter()
{
$time = Time::parse('April 15, 2015');
$this->assertEquals(2, $time->quarter);
}
public function testGetDST()
{
// America/Chicago. DST from early March -> early Nov
$time = Time::createFromDate(2012, 1, 1);
$this->assertFalse($time->dst);
$time = Time::createFromDate(2012, 9, 1);
$this->assertTrue($time->dst);
}
public function testGetDSTUnobserved()
{
// Asia/Shanghai. DST not observed
$tz = new DateTimeZone('Asia/Shanghai');
$time = Time::createFromDate(2012, 1, 1, $tz, 'Asia/Shanghai');
$this->assertFalse($time->dst);
}
public function testGetLocal()
{
$this->assertTrue(Time::now()->local);
$this->assertFalse(Time::now('Europe/London')->local);
}
public function testGetUtc()
{
$this->assertFalse(Time::now('America/Chicago')->utc);
$this->assertTrue(Time::now('UTC')->utc);
}
public function testGetTimezone()
{
$instance = Time::now()->getTimezone();
$this->assertInstanceOf(\DateTimeZone::class, $instance);
}
public function testGetTimezonename()
{
$this->assertEquals('America/Chicago', Time::now('America/Chicago')->getTimezoneName());
$this->assertEquals('Europe/London', Time::now('Europe/London')->timezoneName);
}
public function testSetYear()
{
$time = Time::parse('May 10, 2017');
$time2 = $time->setYear(2015);
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2015-05-10 00:00:00', $time2->toDateTimeString());
}
public function testSetMonthNumber()
{
$time = Time::parse('May 10, 2017');
$time2 = $time->setMonth(4);
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2017-04-10 00:00:00', $time2->toDateTimeString());
}
public function testSetMonthLongName()
{
$time = Time::parse('May 10, 2017');
$time2 = $time->setMonth('April');
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2017-04-10 00:00:00', $time2->toDateTimeString());
}
public function testSetMonthShortName()
{
$time = Time::parse('May 10, 2017');
$time2 = $time->setMonth('Feb');
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2017-02-10 00:00:00', $time2->toDateTimeString());
}
public function testSetDay()
{
$time = Time::parse('May 10, 2017');
$time2 = $time->setDay(15);
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2017-05-15 00:00:00', $time2->toDateTimeString());
}
public function testSetDayOverMaxInCurrentMonth()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('Feb 02, 2009');
$time->setDay(29);
}
public function testSetDayNotOverMaxInCurrentMonth()
{
$time = Time::parse('Feb 02, 2012');
$time2 = $time->setDay(29);
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2012-02-29 00:00:00', $time2->toDateTimeString());
}
public function testSetHour()
{
$time = Time::parse('May 10, 2017');
$time2 = $time->setHour(15);
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2017-05-10 15:00:00', $time2->toDateTimeString());
}
public function testSetMinute()
{
$time = Time::parse('May 10, 2017');
$time2 = $time->setMinute(30);
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2017-05-10 00:30:00', $time2->toDateTimeString());
}
public function testSetSecond()
{
$time = Time::parse('May 10, 2017');
$time2 = $time->setSecond(20);
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2017-05-10 00:00:20', $time2->toDateTimeString());
}
public function testSetMonthTooSmall()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('May 10, 2017');
$time->setMonth(-5);
}
public function testSetMonthTooBig()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('May 10, 2017');
$time->setMonth(30);
}
public function testSetDayTooSmall()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('May 10, 2017');
$time->setDay(-5);
}
public function testSetDayTooBig()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('May 10, 2017');
$time->setDay(80);
}
public function testSetHourTooSmall()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('May 10, 2017');
$time->setHour(-5);
}
public function testSetHourTooBig()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('May 10, 2017');
$time->setHour(80);
}
public function testSetMinuteTooSmall()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('May 10, 2017');
$time->setMinute(-5);
}
public function testSetMinuteTooBig()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('May 10, 2017');
$time->setMinute(80);
}
public function testSetSecondTooSmall()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('May 10, 2017');
$time->setSecond(-5);
}
public function testSetSecondTooBig()
{
$this->expectException('CodeIgniter\I18n\Exceptions\I18nException');
$time = Time::parse('May 10, 2017');
$time->setSecond(80);
}
public function testSetTimezone()
{
$time = Time::parse('May 10, 2017', 'America/Chicago');
$time2 = $time->setTimezone('Europe/London');
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('America/Chicago', $time->getTimezoneName());
$this->assertEquals('Europe/London', $time2->getTimezoneName());
}
public function testSetTimestamp()
{
$time = Time::parse('May 10, 2017', 'America/Chicago');
$stamp = strtotime('April 1, 2017');
$time2 = $time->setTimestamp($stamp);
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2017-04-01 00:00:00', $time2->toDateTimeString());
}
public function testToDateString()
{
$time = Time::parse('May 10, 2017', 'America/Chicago');
$this->assertEquals('2017-05-10', $time->toDateString());
}
public function testToFormattedDateString()
{
$time = Time::parse('2017-05-10', 'America/Chicago');
$this->assertEquals('May 10, 2017', $time->toFormattedDateString());
}
/**
* Unfortunately, ubuntu 14.04 (on TravisCI) fails this test and
* shows a numeric version of the month instead of the textual version.
* Confirmed on CentOS 7 as well.
* Example: format 'MMM' for November returns 'M02' instead of 'Nov'
* Not sure what the fix is just yet....
*/
// public function testToFormattedDateString()
// {
// $time = Time::parse('February 10, 2017', 'America/Chicago');
// $this->assertEquals('Feb 10, 2017', $time->toFormattedDateString());
// }
public function testToTimeString()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$this->assertEquals('13:20:33', $time->toTimeString());
}
//--------------------------------------------------------------------
// Add/Subtract
//--------------------------------------------------------------------
public function testCanAddSeconds()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->addSeconds(10);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2017-01-10 13:20:43', $newTime->toDateTimeString());
}
public function testCanAddMinutes()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->addMinutes(10);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2017-01-10 13:30:33', $newTime->toDateTimeString());
}
public function testCanAddHours()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->addHours(3);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2017-01-10 16:20:33', $newTime->toDateTimeString());
}
public function testCanAddDays()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->addDays(3);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2017-01-13 13:20:33', $newTime->toDateTimeString());
}
public function testCanAddMonths()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->addMonths(3);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2017-04-10 13:20:33', $newTime->toDateTimeString());
}
public function testCanAddMonthsOverYearBoundary()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->addMonths(13);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2018-02-10 13:20:33', $newTime->toDateTimeString());
}
public function testCanAddYears()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->addYears(3);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2020-01-10 13:20:33', $newTime->toDateTimeString());
}
public function testCanSubtractSeconds()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->subSeconds(10);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2017-01-10 13:20:23', $newTime->toDateTimeString());
}
public function testCanSubtractMinutes()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->subMinutes(10);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2017-01-10 13:10:33', $newTime->toDateTimeString());
}
public function testCanSubtractHours()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->subHours(3);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2017-01-10 10:20:33', $newTime->toDateTimeString());
}
public function testCanSubtractDays()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->subDays(3);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2017-01-07 13:20:33', $newTime->toDateTimeString());
}
public function testCanSubtractMonths()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->subMonths(3);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2016-10-10 13:20:33', $newTime->toDateTimeString());
}
public function testCanSubtractYears()
{
$time = Time::parse('January 10, 2017 13:20:33', 'America/Chicago');
$newTime = $time->subYears(3);
$this->assertEquals('2017-01-10 13:20:33', $time->toDateTimeString());
$this->assertEquals('2014-01-10 13:20:33', $newTime->toDateTimeString());
}
//--------------------------------------------------------------------
// Comparison
//--------------------------------------------------------------------
public function testEqualWithDifferent()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$time2 = Time::parse('January 11, 2017 03:50:00', 'Europe/London');
$this->assertTrue($time1->equals($time2));
}
public function testEqualWithSame()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$time2 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$this->assertTrue($time1->equals($time2));
}
public function testEqualWithDateTime()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$time2 = new \DateTime('January 11, 2017 03:50:00', new \DateTimeZone('Europe/London'));
$this->assertTrue($time1->equals($time2));
}
public function testEqualWithSameDateTime()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$time2 = new \DateTime('January 10, 2017 21:50:00', new \DateTimeZone('America/Chicago'));
$this->assertTrue($time1->equals($time2));
}
public function testEqualWithString()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$this->assertTrue($time1->equals('January 11, 2017 03:50:00', 'Europe/London'));
}
public function testEqualWithStringAndNotimezone()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$this->assertTrue($time1->equals('January 10, 2017 21:50:00'));
}
public function testSameSuccess()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$time2 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$this->assertTrue($time1->sameAs($time2));
}
public function testSameFailure()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$time2 = Time::parse('January 11, 2017 03:50:00', 'Europe/London');
$this->assertFalse($time1->sameAs($time2));
}
public function testSameSuccessAsString()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$this->assertTrue($time1->sameAs('January 10, 2017 21:50:00', 'America/Chicago'));
}
public function testSameFailAsString()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$this->assertFalse($time1->sameAs('January 11, 2017 03:50:00', 'Europe/London'));
}
public function testBefore()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$time2 = Time::parse('January 11, 2017 03:50:00', 'America/Chicago');
$this->assertTrue($time1->isBefore($time2));
$this->assertFalse($time2->isBefore($time1));
}
public function testAfter()
{
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$time2 = Time::parse('January 11, 2017 03:50:00', 'America/Chicago');
$this->assertFalse($time1->isAfter($time2));
$this->assertTrue($time2->isAfter($time1));
}
public function testHumanizeYearsSingle()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 9, 2016 12:00:00', 'America/Chicago');
$this->assertEquals('1 year ago', $time->humanize());
}
public function testHumanizeYearsPlural()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 9, 2014 12:00:00', 'America/Chicago');
$this->assertEquals('3 years ago', $time->humanize());
}
public function testHumanizeYearsForward()
{
Time::setTestNow('January 1, 2017', 'America/Chicago');
$time = Time::parse('January 1, 2018 12:00:00', 'America/Chicago');
$this->assertEquals('in 1 year', $time->humanize());
}
public function testHumanizeMonthsSingle()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('February 9, 2017', 'America/Chicago');
$this->assertEquals('1 month ago', $time->humanize());
}
public function testHumanizeMonthsPlural()
{
Time::setTestNow('March 1, 2017', 'America/Chicago');
$time = Time::parse('January 1, 2017', 'America/Chicago');
$this->assertEquals('2 months ago', $time->humanize());
}
public function testHumanizeMonthsForward()
{
Time::setTestNow('March 1, 2017', 'America/Chicago');
$time = Time::parse('April 1, 2017', 'America/Chicago');
$this->assertEquals('in 1 month', $time->humanize());
}
public function testHumanizeDaysSingle()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 8, 2017', 'America/Chicago');
$this->assertEquals('2 days ago', $time->humanize());
}
public function testHumanizeDaysPlural()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 8, 2017', 'America/Chicago');
$this->assertEquals('2 days ago', $time->humanize());
}
public function testHumanizeDaysForward()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 12, 2017', 'America/Chicago');
$this->assertEquals('in 2 days', $time->humanize());
}
public function testHumanizeDaysTomorrow()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 11, 2017', 'America/Chicago');
$this->assertEquals('Tomorrow', $time->humanize());
}
public function testHumanizeDaysYesterday()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 9, 2017', 'America/Chicago');
$this->assertEquals('Yesterday', $time->humanize());
}
public function testHumanizeHoursAsTime()
{
Time::setTestNow('March 10, 2017 12:00', 'America/Chicago');
$time = Time::parse('March 10, 2017 14:00', 'America/Chicago');
$this->assertEquals('in 2 hours', $time->humanize());
}
public function testHumanizeHoursAWhileAgo()
{
Time::setTestNow('March 10, 2017 12:00', 'America/Chicago');
$time = Time::parse('March 10, 2017 8:00', 'America/Chicago');
$this->assertEquals('4 hours ago', $time->humanize());
}
public function testHumanizeMinutesSingle()
{
Time::setTestNow('March 10, 2017 12:30', 'America/Chicago');
$time = Time::parse('March 10, 2017 12:29', 'America/Chicago');
$this->assertEquals('1 minute ago', $time->humanize());
}
public function testHumanizeMinutesPlural()
{
Time::setTestNow('March 10, 2017 12:30', 'America/Chicago');
$time = Time::parse('March 10, 2017 12:28', 'America/Chicago');
$this->assertEquals('2 minutes ago', $time->humanize());
}
public function testHumanizeMinutesForward()
{
Time::setTestNow('March 10, 2017 12:30', 'America/Chicago');
$time = Time::parse('March 10, 2017 12:31', 'America/Chicago');
$this->assertEquals('in 1 minute', $time->humanize());
}
public function testHumanizeWeeksSingle()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 2, 2017', 'America/Chicago');
$this->assertEquals('1 week ago', $time->humanize());
}
public function testHumanizeWeeksPlural()
{
Time::setTestNow('March 30, 2017', 'America/Chicago');
$time = Time::parse('March 15, 2017', 'America/Chicago');
$this->assertEquals('2 weeks ago', $time->humanize());
}
public function testHumanizeWeeksForward()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 18, 2017', 'America/Chicago');
$this->assertEquals('in 2 weeks', $time->humanize());
}
public function testHumanizeNow()
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
$time = Time::parse('March 10, 2017', 'America/Chicago');
$this->assertEquals('Just now', $time->humanize());
}
public function testSetTimezoneDate()
{
$time = Time::parse('13 May 2020 10:00', 'GMT');
$time2 = $time->setTimezone('GMT+8');
$this->assertEquals('2020-05-13 10:00:00', $time->toDateTimeString());
$this->assertEquals('2020-05-13 18:00:00', $time2->toDateTimeString());
}
//--------------------------------------------------------------------
// Missing tests
public function testInstance()
{
$datetime = new DateTime();
$time = Time::instance($datetime);
$this->assertTrue($time instanceof Time);