Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7 - timezone handling #15

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
vendor
composer.lock
index.php
.php-cs-fixer.cache
2 changes: 1 addition & 1 deletion composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"require-dev": {
"blumilksoftware/codestyle": "^3.1",
"phpunit/phpunit": "^11.1.3"
"phpunit/phpunit": "^11.2"
},
"authors": [
{
Expand Down
15 changes: 14 additions & 1 deletion src/HeatmapBuilder.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Blumilk\HeatmapBuilder\Contracts\TimeGroupable;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Carbon\CarbonTimeZone;

class HeatmapBuilder
{
Expand All @@ -24,7 +25,10 @@ public function __construct(
protected ?Decorator $decorator = null,
protected bool $alignedToStartOfPeriod = false,
protected bool $alignedToEndOfPeriod = false,
) {}
protected ?CarbonTimeZone $timezone = null,
) {
$this->timezone ??= new CarbonTimeZone("UTC");
}

/**
* @param iterable<array|ArrayAccess|TimeGroupable> $data
Expand All @@ -42,18 +46,21 @@ public function build(iterable $data): array
PeriodInterval::Monthly => $date->format("Y-m"),
PeriodInterval::Weekly => $date->format("Y:W"),
PeriodInterval::Daily => $date->format("Y-m-d"),
PeriodInterval::Hourly => $date->format("H:i"),
},
count: count(
array_filter($data, fn(Carbon $item): bool => match ($this->periodInterval) {
PeriodInterval::Monthly => $item->isSameMonth($date),
PeriodInterval::Weekly => $item->isSameWeek($date),
PeriodInterval::Daily => $item->isSameDay($date),
PeriodInterval::Hourly => $item->isSameHour($date),
}),
),
isToday: match ($this->periodInterval) {
PeriodInterval::Monthly => $date->isCurrentMonth(),
PeriodInterval::Weekly => $date->isCurrentWeek(),
PeriodInterval::Daily => $date->isCurrentDay(),
PeriodInterval::Hourly => $date->isCurrentHour(),
},
inFuture: $date->isFuture(),
);
Expand Down Expand Up @@ -125,10 +132,13 @@ protected function mapToCarbon(array|ArrayAccess|TimeGroupable $item): Carbon
? Carbon::parse($item->getTimeGroupableIndicator())
: Carbon::parse($item[$this->arrayAccessIndex]);

$date->setTimezone($this->timezone);
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved

return match ($this->periodInterval) {
PeriodInterval::Monthly => $date->startOfMonth(),
PeriodInterval::Weekly => $date->startOfWeek(),
PeriodInterval::Daily => $date->startOfDay(),
PeriodInterval::Hourly => $date->startOfHour(),
};
}

Expand All @@ -140,6 +150,7 @@ protected function getPeriod(): CarbonPeriod
PeriodInterval::Monthly => $this->now->copy()->subYear(),
PeriodInterval::Weekly => $this->now->copy()->subMonth(),
PeriodInterval::Daily => $this->now->copy()->subWeek(),
PeriodInterval::Hourly => $this->now->copy()->subDay(),
},
$this->periodInterval->value,
$this->now->copy(),
Expand All @@ -152,6 +163,7 @@ protected function getPeriod(): CarbonPeriod
PeriodInterval::Monthly => $from->startOfYear(),
PeriodInterval::Weekly => $from->startOfMonth(),
PeriodInterval::Daily => $from->startOfWeek(),
PeriodInterval::Hourly => $from->startOfDay(),
};

$period->setStartDate($from);
Expand All @@ -163,6 +175,7 @@ protected function getPeriod(): CarbonPeriod
PeriodInterval::Monthly => $to->endOfYear(),
PeriodInterval::Weekly => $to->endOfMonth(),
PeriodInterval::Daily => $to->endOfWeek(),
PeriodInterval::Hourly => $to->endOfDay(),
};

$period->setEndDate($to);
Expand Down
1 change: 1 addition & 0 deletions src/PeriodActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function forNumberOfTiles(int $number): static
$start = $this->now->copy()->sub(
$number,
match ($this->periodInterval) {
PeriodInterval::Hourly => "hours",
PeriodInterval::Daily => "days",
PeriodInterval::Weekly => "weeks",
PeriodInterval::Monthly => "months",
Expand Down
1 change: 1 addition & 0 deletions src/PeriodInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

enum PeriodInterval: string
{
case Hourly = "1 hour";
case Daily = "1 day";
case Weekly = "1 week";
case Monthly = "1 month";
Expand Down
Loading