Support a configurable display timezone for comment timestamps#104
Open
ItsMalikJones wants to merge 2 commits into
Open
Support a configurable display timezone for comment timestamps#104ItsMalikJones wants to merge 2 commits into
ItsMalikJones wants to merge 2 commits into
Conversation
Adds `commentions.timezone` config and a `Config::resolveTimezoneUsing(Closure)` helper for per-request resolution (e.g. the authenticated user's preferred timezone). The closure wins over the static config value; both default to null, leaving timestamps in the storage timezone. `Comment::getCreatedAt/getUpdatedAt` and the matching getters on `RenderableComment` now route through `Config::applyTimezone()`, which clones the date before mutating to avoid affecting the underlying Eloquent attributes.
The closure result now takes precedence, falling back to the config value when null is returned. Empty string timezones are treated as no timezone. Added proper type imports and updated tests to cover the fallback behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #17.
Summary
Comment timestamps are stored in the app's default timezone and, until now, were
always rendered in that same timezone. This PR adds an opt-in way to render them
in a different timezone — either a fixed app-wide value or a per-request value such
as the authenticated user's timezone.
What changed
commentions.timezone(defaults tonull).ConfigAPI:Config::resolveTimezoneUsing(?Closure $callback = null)— register a closurefor per-request resolution; passing
nullclears it.Config::getTimezone(): ?string— resolves the effective timezone.Config::applyTimezone(DateTime|CarbonInterface $dt)— returns a timezone-adjustedcopy of a date.
Comment::getCreatedAt()/getUpdatedAt()and the matchingRenderableCommentgetters now pass their value through
Config::applyTimezone().README.mdandconfig/commentions.php.Behaviour details
nullit falls back to the
commentions.timezoneconfig value; when neither yields avalue, dates render in the storage timezone (unchanged behaviour).
applyTimezone()returns a copy (->copy()for Carbon,cloneforDateTime) and never mutates the source instance.users.timezonecolumn) istreated the same as
null, so it can't throw anInvalidTimeZoneExceptionatrender time.
Usage
Testing
Added
tests/TimezoneTest.php(7 tests) covering: no-timezone passthrough, fixedconfig conversion, the
RenderableCommentpath, closure-over-config precedence,config fallback when the closure returns
null, empty-string handling, andsource-instance non-mutation.