cakephp / chronos

A standalone DateTime library originally based off of Carbon

Home Page:http://book.cakephp.org/chronos

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

return $this->setDate($this->year, $value, $this->day);

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 👍