Created
August 30, 2017 02:05
-
-
Save walter/a2321c8aa95a13fbd7280cfb8db271f6 to your computer and use it in GitHub Desktop.
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
defmodule Junk.SensibleDateTimeType do | |
use Timex | |
require Logger | |
@behaviour Ecto.Type | |
def type, do: :naive_datetime | |
def load(%NaiveDateTime{} = value), do: value | |
def load(_), do: :error | |
def dump(%NaiveDateTime{} = naive_datetime) do | |
{:ok, naive_datetime} | |
end | |
def dump(_), do: :error | |
def cast(%NaiveDateTime{} = naive_datetime), do: naive_datetime | |
def cast(string) when is_binary(string) do | |
case Timex.parse(string, "{ISO:Extended}") do | |
{:ok, datetime} -> {:ok, Timex.to_naive_datetime(datetime)} | |
_ -> :error | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment