-
Notifications
You must be signed in to change notification settings - Fork 4
/
date_time.ex
58 lines (50 loc) · 1.3 KB
/
date_time.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
defmodule FatUtils.DateTime do
@moduledoc """
Takes integer and binary values to convert them to proper DateTime format.
"""
@doc """
Takes date time in the form of integer to convert to unix format and also in binary format to convert to iso8601.
"""
def parse(dtu) do
case parse!(dtu) do
nil -> :error
datetime -> {:ok, datetime}
end
end
@doc """
Takes date time in the form of integer to convert to unix format and also in binary format to convert to iso8601.
"""
def parse!(dtu) do
cond do
is_integer(dtu) && from_uni?(dtu) ->
{:ok, dt} = DateTime.from_unix(dtu)
dt
is_integer(dtu) && from_uni_ms?(dtu) ->
{:ok, dt} = DateTime.from_unix(dtu, :microsecond)
dt
is_binary(dtu) && from_iso8601?(dtu) ->
{:ok, datetime, _} = DateTime.from_iso8601(dtu)
datetime
true ->
nil
end
end
defp from_uni?(dtu) do
case DateTime.from_unix(dtu) do
{:ok, _dt} -> true
_other -> false
end
end
defp from_uni_ms?(dtu) do
case DateTime.from_unix(dtu, :microsecond) do
{:ok, _dt} -> true
_other -> false
end
end
defp from_iso8601?(dtu) do
case DateTime.from_iso8601(dtu) do
{:ok, _datetime, _} -> true
_other -> false
end
end
end