Phoenix/Ecto - 将 ISO 字符串转换为 utc_datetime 原始类型
Phoenix/Ecto - converting ISO string into utc_datetime primitive type
在我的 Phoenix 应用程序中,我试图将事件记录插入到数据库中,该数据库具有 start_time
和 end_time
的字段 - 日期时间数据已经转换为 ISO 字符串格式客户端并作为 JSON 数据传递给 Phoenix API,但这在我尝试插入时给我带来了一些麻烦 - 模型期望这些值为 :utc_datetime
所以我需要转换它们 - 我通读了文档,但我仍然不确定...
首先,这是模型的架构:
@primary_key {:id, :string, []}
@derive {Phoenix.Param, key: :id}
schema "calendar_event" do
field :start_time, :utc_datetime
field :end_time, :utc_datetime
field :description, :string
timestamps()
end
JSON 来自客户端的数据如下所示:
{
"start_time": "2017-09-28T18:31:32.223Z",
"end_time": "2017-09-28T19:31:32.223Z",
"description": "Test insert"
}
如果我(错误地)尝试按原样插入此数据,语句将如下所示:
MyApp.Repo.insert(%MyApp.CalendarEvent{id: "calendar_event:test1", start_time:
"2017-09-28T18:31:32.223Z", end_time: "2017-09-28T19:31:32.223Z",
description: "Test insert"})
正如预期的那样,这引发了我的日期时间数据 does not match type :utc_datetime
的错误。好的,这很酷,但我的问题是,数据已经在 ISO 字符串中,我如何将其转换为 Elixir/Ecto 将其识别为有效的 :utc_datetime
?
你想要一个 DateTime
结构用于你的 :utc_datetime
字段,你可以在此处的文档中看到:https://hexdocs.pm/ecto/Ecto.Schema.html#module-primitive-types
您可以像上面这样从 iso 字符串中获取 DateTime
:
iex> {:ok, dt, 0} = DateTime.from_iso8601("2017-09-28T18:31:32.223Z")
iex> dt
#DateTime<2017-09-28 18:31:32.223Z>
(元组中的零是UTC偏移量)
注意:Ecto 2.1 中引入了对 Elixir 日历类型的支持。 DateTime.from_iso8601/1
是在 Elixir 1.4 中引入的。
在我的 Phoenix 应用程序中,我试图将事件记录插入到数据库中,该数据库具有 start_time
和 end_time
的字段 - 日期时间数据已经转换为 ISO 字符串格式客户端并作为 JSON 数据传递给 Phoenix API,但这在我尝试插入时给我带来了一些麻烦 - 模型期望这些值为 :utc_datetime
所以我需要转换它们 - 我通读了文档,但我仍然不确定...
首先,这是模型的架构:
@primary_key {:id, :string, []}
@derive {Phoenix.Param, key: :id}
schema "calendar_event" do
field :start_time, :utc_datetime
field :end_time, :utc_datetime
field :description, :string
timestamps()
end
JSON 来自客户端的数据如下所示:
{
"start_time": "2017-09-28T18:31:32.223Z",
"end_time": "2017-09-28T19:31:32.223Z",
"description": "Test insert"
}
如果我(错误地)尝试按原样插入此数据,语句将如下所示:
MyApp.Repo.insert(%MyApp.CalendarEvent{id: "calendar_event:test1", start_time:
"2017-09-28T18:31:32.223Z", end_time: "2017-09-28T19:31:32.223Z",
description: "Test insert"})
正如预期的那样,这引发了我的日期时间数据 does not match type :utc_datetime
的错误。好的,这很酷,但我的问题是,数据已经在 ISO 字符串中,我如何将其转换为 Elixir/Ecto 将其识别为有效的 :utc_datetime
?
你想要一个 DateTime
结构用于你的 :utc_datetime
字段,你可以在此处的文档中看到:https://hexdocs.pm/ecto/Ecto.Schema.html#module-primitive-types
您可以像上面这样从 iso 字符串中获取 DateTime
:
iex> {:ok, dt, 0} = DateTime.from_iso8601("2017-09-28T18:31:32.223Z")
iex> dt
#DateTime<2017-09-28 18:31:32.223Z>
(元组中的零是UTC偏移量)
注意:Ecto 2.1 中引入了对 Elixir 日历类型的支持。 DateTime.from_iso8601/1
是在 Elixir 1.4 中引入的。