Date::create doesn't always returns the expected
MolbioUnige opened this issue · comments
Hi,
Chronos doesn't seems to create correct February 1st dates.
debug(Date::create()->year(2000)->month(2)->day(1));
debug(Date::create()->year(2001)->month(2)->day(1));
debug(Date::create()->year(2002)->month(2)->day(1));
debug(Date::create()->year(2019)->month(2)->day(1));
debug(Date::create()->year(2020)->month(2)->day(1));
Returns
object(Cake\Chronos\Date) {
'hasFixedNow' => false,
'date' => '2000-02-01'
}
object(Cake\Chronos\Date) {
'hasFixedNow' => false,
'date' => '2001-03-01'. <- March 1st
}
object(Cake\Chronos\Date) {
'hasFixedNow' => false,
'date' => '2002-03-01' <- March 1st
}
object(Cake\Chronos\Date) {
'hasFixedNow' => false,
'date' => '2019-03-01' <- March 1st
}
object(Cake\Chronos\Date) {
'hasFixedNow' => false,
'date' => '2020-02-01'
}
If you use a day that doesn't exist in February, it will advance to the next month.
chronos/src/Traits/ModifierTrait.php
Line 199 in 30baea5
Sure, but February 1st always exists, no?
month()
uses the current day.
Could you please tell me what
Date::create()->year(2001)->month(2)->day(1)
should return?
That depends what day create() returns.
Sorry to be slow, in your opinion, should this below return true or false?
Date::create()->year(2019)->month(1)->day(31)->addDay(1) == Date::create()->year(2019)->month(2)->day(1)
Intermediate instances always contain valid dates.
Date::create() // today 2020-09-29
->year(2001) // 2001-09-29
->month(2) // 2001-02-29 doesn't exist - moves forward to next existing date 2001-03-01
->day(1) // 2001-03-01
In your opinion, what should contain object after month()
call?
You can create date object atomically with Date::create(2001, 2, 1)
Now I get it, thank you very much 👍