Skip to content

Reduce reliance on Timex and use native time API where feasible#5712

Merged
zoldar merged 20 commits intomasterfrom
plausible-times
Sep 10, 2025
Merged

Reduce reliance on Timex and use native time API where feasible#5712
zoldar merged 20 commits intomasterfrom
plausible-times

Conversation

@zoldar
Copy link
Copy Markdown
Contributor

@zoldar zoldar commented Sep 9, 2025

Changes

This PR replaces uses of Timex with native date and time APIs and moves all the remaining Timex references behind Plausible.Times and Plausible.Timezones. All cases where either :gap or :ambiguous time computation result can occur are handled gracefully (the wrapped Timex end_of and beginning_of functions handle that internally already).

TODO

  • intentionally break each new function in Times and Timezones to see if tests cover them at least

Tests

  • Automated tests have been updated

Comment thread lib/plausible/times.ex Outdated
@zoldar zoldar changed the title Wrap Timex calls and handle gaps and ambiguous cases gracefully Reduce reliance on Timex and use native time API where feasible Sep 9, 2025
case report["rows"] do
[%{"dimensionValues" => [%{"value" => date_str}]}] ->
Timex.parse!(date_str, "%Y%m%d", :strftime) |> NaiveDateTime.to_date()
Plausible.Times.parse!(date_str, "%Y%m%d", :strftime) |> NaiveDateTime.to_date()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

The news outlet we need

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@zoldar zoldar force-pushed the plausible-times branch 3 times, most recently from 91d2301 to 1bed7d1 Compare September 9, 2025 18:18
@zoldar zoldar marked this pull request as ready for review September 10, 2025 09:05
@zoldar zoldar requested a review from a team September 10, 2025 09:06
@zoldar zoldar added the preview label Sep 10, 2025
@github-actions
Copy link
Copy Markdown

Preview environment👷🏼‍♀️🏗️
PR-5712

Comment thread lib/plausible/billing/subscriptions.ex Outdated
with %Date{} = from <- Keyword.get(opts, :from),
%Date{} = to <- Keyword.get(opts, :to),
true <- abs(Timex.diff(from, to, :months)) > 12 do
true <- abs(Plausible.Times.diff(from, to, :month)) > 12 do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wtf, DateTime.diff has no :month? :(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, unfortunately day is the highest possible unit when diffing.

@zoldar zoldar added this pull request to the merge queue Sep 10, 2025
Merged via the queue into master with commit 8d6d828 Sep 10, 2025
16 checks passed
@zoldar zoldar deleted the plausible-times branch September 10, 2025 10:25
zoldar added a commit that referenced this pull request Sep 10, 2025
RobertJoonas pushed a commit that referenced this pull request Sep 10, 2025
)

* Replace usages of `Timex.to_unix` with native API

* Wrap call to `Timex.is_valid_timezone?`

* Wrap calls to `Timex.today(tz)`

* Replace `Timex.today()` with `Date.utc_today()`

* Replace `Timex.now()` with `DateTime.utc_now()`

* Replace `Timex.compare` with `Date.compare`

* Wrap `Timex.diff` calls

* Replace `Timex.Timezone.convert` with `DateTime.shift_zone!`

* Wrap `Timex.parse!`

* Replace `Timex.to_date` with native API calls

* Replace `Timex.beginning|end_of...` with native API calls for Date

* Wrap `Timex.beginning|end_of...` for DateTimes and Dates for years

* Replace `Timex.format(!)` with native API calls

* Replace `Timex.to_naive_datetime` with native API calls

* Wrap time humanizing routines using Timex

* Remove unnecessary `use Timex` instances

* Replace `Timex.shift` with native API calls

* Make `QueryParser.parse_date` handle gaps and ambiguities gracefully

* Replace `Timex.now(tz)` with `DateTime.now!(tz)`

* Use a more suitable Date function for comparison (h/t @aerosol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants