-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"Confinement" de la librairie Timex (#4385)
* Extract iso-extended parsing to a central place * Add TODO * Add wrapper for "{YYYY}{0M}{0D}" * Add TODO * Move test calls * Wrap now() and shift() * Wrap Timex conversion calls * Add doctest wrapper * Add note * Fix credo warnings * Add doc * Update time_wrapper.ex * Forbid direct use of Timex
- Loading branch information
Showing
8 changed files
with
70 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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,48 @@ | ||
defmodule TimeWrapper do | ||
@moduledoc """ | ||
This module concentrates all the calls to `Timex` in a single place. | ||
The idea behind this module is 1. to reduce our dependency on `Timex`, and | ||
2. to ideally gradually replace calls by built-in Elixir `DateTime` calls, since | ||
`Timex` filled a void in the language that has been partially filled now. | ||
""" | ||
|
||
# credo:disable-for-this-file Credo.Check.Warning.ForbiddenModule | ||
|
||
def parse!(date_as_string, "{ISO:Extended}" = param) do | ||
Timex.parse!(date_as_string, param) | ||
end | ||
|
||
def parse!(date_as_string, "{YYYY}{0M}{0D}" = param) do | ||
Timex.parse!(date_as_string, param) | ||
end | ||
|
||
# NOTE: try not to use this, we will remove it. This is rfc2822 ; | ||
# Plug encodes it, but there is no built-in decoder. | ||
def parse!(datetime_as_string, "{WDshort}, {D} {Mshort} {YYYY} {h24}:{m}:{s} GMT" = param) do | ||
Timex.parse!(datetime_as_string, param) | ||
end | ||
|
||
def diff(first, second, :hours = param) do | ||
Timex.diff(first, second, param) | ||
end | ||
|
||
def now do | ||
Timex.now() | ||
end | ||
|
||
def shift(dt, months: months) do | ||
Timex.shift(dt, months: months) | ||
end | ||
|
||
def convert(dt, "UTC") do | ||
Timex.Timezone.convert(dt, "UTC") | ||
end | ||
|
||
def convert_to_paris_time(dt) do | ||
case Timex.Timezone.convert(dt, "Europe/Paris") do | ||
%Timex.AmbiguousDateTime{after: dt} -> dt | ||
%DateTime{} = dt -> dt | ||
end | ||
end | ||
end |
This file contains 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,4 @@ | ||
defmodule TimeWrapperTest do | ||
use ExUnit.Case, async: true | ||
doctest TimeWrapper | ||
end |
This file contains 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 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 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 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