Skip to content

Commit e56d33e

Browse files
authored
Merge pull request #29 from assoconnect/fix_timetraveler
Fix traveler for last day of month
2 parents 80a5ef7 + de37eba commit e56d33e

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/TimeTraveler.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class TimeTraveler
1414
*
1515
* (new AbsoluteDate(2020-01-31))->modify('+1 month') = 2020-02-31 => 2020-03-02 so more than a month
1616
* addMonth(new AbsoluteDate(2020-01-31)) = 2020-02-29
17+
*
18+
* (new AbsoluteDate(2020-06-30))->modify('+1 month') = 2020-07-30 so less than a month
19+
* addMonth(new AbsoluteDate(2020-06-30)) = 2020-07-31
1720
*/
1821
public function addMonth(AbsoluteDate $from): AbsoluteDate
1922
{
@@ -22,7 +25,8 @@ public function addMonth(AbsoluteDate $from): AbsoluteDate
2225
while ($expectedMonth !== intval($next->format('n'))) {
2326
$next = $next->modify('-1 day');
2427
}
25-
return $next;
28+
29+
return $this->modifyForTheLastDayOfThisMonthIfNeedBe($next, $from);
2630
}
2731

2832
/**
@@ -51,7 +55,10 @@ public function addMonthWithReference(AbsoluteDate $reference, AbsoluteDate $fro
5155
intval($next->modify('last day of this month')->format('j'))
5256
);
5357
$dayString = str_pad(strval($day), 2, '0', STR_PAD_LEFT);
54-
return new AbsoluteDate($next->format('Y-m') . '-' . $dayString);
58+
return $this->modifyForTheLastDayOfThisMonthIfNeedBe(
59+
new AbsoluteDate($next->format('Y-m') . '-' . $dayString),
60+
$reference
61+
);
5562
}
5663

5764
/**
@@ -68,4 +75,13 @@ public function addYear(AbsoluteDate $from): AbsoluteDate
6875
}
6976
return $next;
7077
}
78+
79+
private function modifyForTheLastDayOfThisMonthIfNeedBe(AbsoluteDate $result, AbsoluteDate $reference): AbsoluteDate
80+
{
81+
$pattern = 'last day of this month';
82+
if ($reference->equalsTo($reference->modify($pattern))) {
83+
return $result->modify($pattern);
84+
}
85+
return $result;
86+
}
7187
}

tests/TimeTravelerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public function provideMonths(): iterable
3131
yield ['2020-01-29', '2020-02-29'];
3232
yield ['2020-01-30', '2020-02-29'];
3333
yield ['2020-01-31', '2020-02-29'];
34-
yield ['2020-02-29', '2020-03-29'];
34+
yield ['2020-02-29', '2020-03-31'];
35+
yield ['2020-06-30', '2020-07-31'];
3536
}
3637

3738
/** @dataProvider provideMonthsWithReference */
@@ -55,6 +56,9 @@ public function provideMonthsWithReference(): iterable
5556
yield ['2020-01-30', '2020-02-29', '2020-03-30'];
5657
yield ['2020-01-31', '2020-02-29', '2020-03-31'];
5758
yield ['2020-01-31', '2020-03-31', '2020-04-30'];
59+
yield ['2020-06-30', '2020-06-30', '2020-07-31'];
60+
yield ['2020-06-30', '2020-07-31', '2020-08-31'];
61+
yield ['2020-06-30', '2020-08-31', '2020-09-30'];
5862
}
5963

6064
/** @dataProvider provideMonthsWithReferenceOverYear */
@@ -78,6 +82,7 @@ public function provideMonthsWithReferenceOverYear(): iterable
7882
yield ['2020-01-29', '2021-01-29'];
7983
yield ['2020-01-30', '2021-01-30'];
8084
yield ['2020-01-31', '2021-01-31'];
85+
yield ['2020-06-30', '2021-06-30'];
8186
}
8287

8388
/** @dataProvider provideYears */
@@ -92,5 +97,6 @@ public function provideYears(): iterable
9297
yield ['2020-01-01', '2021-01-01'];
9398
yield ['2020-01-31', '2021-01-31'];
9499
yield ['2020-02-29', '2021-02-28'];
100+
yield ['2020-06-30', '2021-06-30'];
95101
}
96102
}

0 commit comments

Comments
 (0)