Add helpers to convert datetimes.
This commit is contained in:
+24
-1
@@ -2,7 +2,7 @@ defmodule DecentralisedBookIndex.Helpers do
|
||||
@moduledoc "Helpers for tests"
|
||||
|
||||
@doc """
|
||||
Rerurn a new map with same keys as in map_with_keys.
|
||||
Return a new map with same keys as in map_with_keys.
|
||||
"""
|
||||
def get_submap(map, map_with_keys) when is_map(map_with_keys) do
|
||||
Map.take(map, Map.keys(map_with_keys))
|
||||
@@ -17,4 +17,27 @@ defmodule DecentralisedBookIndex.Helpers do
|
||||
get_submap(map, keys)
|
||||
end)
|
||||
end
|
||||
|
||||
def datetime_from_iso_8601(datetime_string) do
|
||||
case DateTime.from_iso8601(datetime_string) do
|
||||
{:ok, datetime, 0} -> datetime
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Return a new map with updated keys :inserted_at and :updated_at.
|
||||
Those keys will be transformed from string to DateTime.
|
||||
"""
|
||||
def datetime_from_iso8601_for_map(map) do
|
||||
map
|
||||
|> Map.update(:inserted_at, nil, &datetime_from_iso_8601/1)
|
||||
|> Map.update(:updated_at, nil, &datetime_from_iso_8601/1)
|
||||
end
|
||||
|
||||
def datetime_add_second_as_string(datetime, seconds) do
|
||||
datetime
|
||||
|> DateTime.add(seconds)
|
||||
|> DateTime.to_iso8601()
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user