-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Reduce reliance on Timex and use native time API where feasible
#5712
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b8bd803
Replace usages of `Timex.to_unix` with native API
zoldar f12d185
Wrap call to `Timex.is_valid_timezone?`
zoldar 74e514f
Wrap calls to `Timex.today(tz)`
zoldar a0cff3f
Replace `Timex.today()` with `Date.utc_today()`
zoldar 5e3254d
Replace `Timex.now()` with `DateTime.utc_now()`
zoldar b215c34
Replace `Timex.compare` with `Date.compare`
zoldar 1c8c9f0
Wrap `Timex.diff` calls
zoldar aa09a69
Replace `Timex.Timezone.convert` with `DateTime.shift_zone!`
zoldar 5459fbf
Wrap `Timex.parse!`
zoldar 5020a4b
Replace `Timex.to_date` with native API calls
zoldar 488a61c
Replace `Timex.beginning|end_of...` with native API calls for Date
zoldar 89993bc
Wrap `Timex.beginning|end_of...` for DateTimes and Dates for years
zoldar e5a9827
Replace `Timex.format(!)` with native API calls
zoldar 98c7fd7
Replace `Timex.to_naive_datetime` with native API calls
zoldar 34254dd
Wrap time humanizing routines using Timex
zoldar 36828e3
Remove unnecessary `use Timex` instances
zoldar 044c16b
Replace `Timex.shift` with native API calls
zoldar d978010
Make `QueryParser.parse_date` handle gaps and ambiguities gracefully
zoldar d8c3c99
Replace `Timex.now(tz)` with `DateTime.now!(tz)`
zoldar 4ac9ae2
Use a more suitable Date function for comparison (h/t @aerosol)
zoldar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,7 @@ defmodule Plausible.Stats.Interval do | |
| """ | ||
| def default_for_date_range(%DateTimeRange{first: first, last: last}) do | ||
| cond do | ||
| Timex.diff(last, first, :months) > 0 -> | ||
| Plausible.Times.diff(last, first, :month) > 0 -> | ||
| "month" | ||
|
|
||
| DateTime.diff(last, first, :day) > 0 -> | ||
|
|
@@ -75,15 +75,15 @@ defmodule Plausible.Stats.Interval do | |
| table = | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wtf,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, unfortunately |
||
| Map.replace(@valid_by_period, "custom", ["week", "month"]) | ||
| else | ||
| _ -> | ||
| @valid_by_period | ||
| end | ||
|
|
||
| with %Date{} = stats_start <- Plausible.Sites.stats_start_date(site), | ||
| true <- abs(Timex.diff(Date.utc_today(), stats_start, :months)) > 12 do | ||
| true <- abs(Plausible.Times.diff(Date.utc_today(), stats_start, :month)) > 12 do | ||
| Map.replace(table, "all", ["week", "month"]) | ||
| else | ||
| _ -> | ||
|
|
||
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| defmodule Plausible.Times do | ||
| @moduledoc """ | ||
| API for working with time wrapping around external libraries where necessary. | ||
| """ | ||
|
|
||
| @spec today(String.t()) :: Date.t() | ||
| def today(tz) do | ||
| tz | ||
| |> DateTime.now!() | ||
| |> DateTime.to_date() | ||
| end | ||
|
|
||
| @spec diff(Date.t() | DateTime.t(), Date.t() | DateTime.t(), :month | :week) :: integer() | ||
| def diff(a, b, unit) do | ||
| unit = | ||
| case unit do | ||
| :week -> :weeks | ||
| :month -> :months | ||
| end | ||
|
|
||
| Timex.diff(a, b, unit) | ||
| end | ||
|
|
||
| @spec parse!(String.t(), String.t(), :default | :strftime) :: DateTime.t() | NaiveDateTime.t() | ||
| def parse!(str, format, tokenizer \\ :default) | ||
|
|
||
| def parse!(str, "{RFC1123}" = format, :default) do | ||
| Timex.parse!(str, format) | ||
| end | ||
|
|
||
| def parse!(str, format, :strftime) do | ||
| Timex.parse!(str, format, :strftime) | ||
| end | ||
|
|
||
| @spec beginning_of_week(DateTime.t()) :: DateTime.t() | ||
| def beginning_of_week(dt) do | ||
| Timex.beginning_of_week(dt) | ||
| end | ||
|
|
||
| @spec beginning_of_month(DateTime.t()) :: DateTime.t() | ||
| def beginning_of_month(dt) do | ||
| Timex.beginning_of_month(dt) | ||
| end | ||
|
|
||
| @spec beginning_of_year(Date.t()) :: Date.t() | ||
| def beginning_of_year(d) do | ||
| Timex.beginning_of_year(d) | ||
| end | ||
|
|
||
| @spec end_of_week(DateTime.t()) :: DateTime.t() | ||
| def end_of_week(dt) do | ||
| Timex.end_of_week(dt) | ||
| end | ||
|
|
||
| @spec end_of_month(DateTime.t()) :: DateTime.t() | ||
| def end_of_month(dt) do | ||
| Timex.end_of_month(dt) | ||
| end | ||
|
|
||
| @spec end_of_year(Date.t()) :: Date.t() | ||
| def end_of_year(t) do | ||
| Timex.end_of_year(t) | ||
| end | ||
|
|
||
| @spec humanize(DateTime.t()) :: String.t() | ||
| def humanize(%DateTime{} = dt) do | ||
| Timex.Format.DateTime.Formatters.Relative.format!(dt, "{relative}") | ||
| end | ||
|
|
||
| @spec humanize_seconds(pos_integer()) :: String.t() | ||
| def humanize_seconds(seconds) do | ||
| seconds | ||
| |> Timex.Duration.from_seconds() | ||
| |> Timex.format_duration(Timex.Format.Duration.Formatters.Humanized) | ||
| end | ||
| end |
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
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The news outlet we need
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.