Skip to content

Support a configurable display timezone for comment timestamps#104

Open
ItsMalikJones wants to merge 2 commits into
mainfrom
feat/17-renderable-comment-timezone
Open

Support a configurable display timezone for comment timestamps#104
ItsMalikJones wants to merge 2 commits into
mainfrom
feat/17-renderable-comment-timezone

Conversation

@ItsMalikJones
Copy link
Copy Markdown

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

  • New config key commentions.timezone (defaults to null).
  • New Config API:
    • Config::resolveTimezoneUsing(?Closure $callback = null) — register a closure
      for per-request resolution; passing null clears it.
    • Config::getTimezone(): ?string — resolves the effective timezone.
    • Config::applyTimezone(DateTime|CarbonInterface $dt) — returns a timezone-adjusted
      copy of a date.
  • Comment::getCreatedAt() / getUpdatedAt() and the matching RenderableComment
    getters now pass their value through Config::applyTimezone().
  • Documentation added to README.md and config/commentions.php.

Behaviour details

  • Resolution order: the closure result takes precedence; when it returns null
    it falls back to the commentions.timezone config value; when neither yields a
    value, dates render in the storage timezone (unchanged behaviour).
  • Non-mutating: applyTimezone() returns a copy (->copy() for Carbon,
    clone for DateTime) and never mutates the source instance.
  • Blank-safe: an empty-string result (e.g. an unset users.timezone column) is
    treated the same as null, so it can't throw an InvalidTimeZoneException at
    render time.

Usage

// config/commentions.php — fixed, app-wide
'timezone' => 'America/Chicago',
// per-user resolution
use Kirschbaum\Commentions\Config;

Config::resolveTimezoneUsing(fn () => auth()->user()?->timezone);

Testing

Added tests/TimezoneTest.php (7 tests) covering: no-timezone passthrough, fixed
config conversion, the RenderableComment path, closure-over-config precedence,
config fallback when the closure returns null, empty-string handling, and
source-instance non-mutation.

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.
@ItsMalikJones ItsMalikJones self-assigned this May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ability to adjust timezone for display for RenderableComment dates (created and updated)

1 participant