fix sunday being first day of week (#2483)

This commit is contained in:
Kevin Papst
2021-04-19 18:32:21 +02:00
committed by GitHub
parent b6462b406d
commit 7ddcde62dd
17 changed files with 136 additions and 71 deletions

View File

@@ -9,6 +9,7 @@
namespace App\Timesheet;
use App\Entity\User;
use DateTime;
use DateTimeZone;
@@ -18,16 +19,26 @@ class DateTimeFactory
* @var DateTimeZone
*/
private $timezone;
/**
* @var bool
*/
private $startOnSunday;
public function __construct(?DateTimeZone $timezone = null)
public static function createByUser(User $user): self
{
return new DateTimeFactory(new \DateTimeZone($user->getTimezone()), $user->isFirstDayOfWeekSunday());
}
public function __construct(?DateTimeZone $timezone = null, bool $startOnSunday = false)
{
if (null === $timezone) {
$timezone = new \DateTimeZone(date_default_timezone_get());
}
$this->setTimezone($timezone);
$this->startOnSunday = $startOnSunday;
}
public function setTimezone(DateTimeZone $timezone)
protected function setTimezone(DateTimeZone $timezone)
{
$this->timezone = $timezone;
}
@@ -51,7 +62,20 @@ class DateTimeFactory
$date = $this->createDateTime('now');
}
return $this->createWeekDateTime($date->format('o'), $date->format('W'), 1, 0, 0, 0);
$from = clone $date;
$year = $from->format('o');
$week = $from->format('W');
$firstDay = 1;
if ($this->startOnSunday) {
$from->modify('-1 week');
$year = $from->format('o');
$week = $from->format('W');
$firstDay = 7;
}
return $this->createWeekDateTime($year, $week, $firstDay, 0, 0, 0);
}
public function getEndOfWeek(?DateTime $date = null): DateTime
@@ -60,7 +84,9 @@ class DateTimeFactory
$date = $this->createDateTime('now');
}
return $this->createWeekDateTime($date->format('o'), $date->format('W'), 7, 23, 59, 59);
$lastDay = $this->startOnSunday ? 6 : 7;
return $this->createWeekDateTime($date->format('o'), $date->format('W'), $lastDay, 23, 59, 59);
}
public function getEndOfMonth(): DateTime