时区信息的日期日期字段条带化

问题描述 投票:0回答:1

我有带有时区信息的日期变量。当我使用ecto将其插入到DB中时,它将剥离时区值并保存。在保存到数据库之前,不应该将其转换为UTC吗?如果这不是默认行为,是否可以配置它?

date = Timex.now("Asia/Kolkata")
user = %User{
    name: "Bob",
    last_login: date,
}
Repo.insert(user)

# user got saved with last_login field with date - striped off timezone info.
# While inserted_at and updated_at always saves the DateTime in UTC.

elixir phoenix-framework ecto
1个回答
0
投票

如果last_login字段的数据类型为:naive_datetime,则将剥离时区信息,而无需将时间转换为UTC。请改用:utc_datetime

示例:

替换以下内容

defmodule SomeModule.Profile do
  use Ecto.Schema

  schema "profiles" do
    field :login_time, :naive_datetime
    # ...
    timestamps()
  end

end

使用

defmodule SomeModule.Profile do
  use Ecto.Schema

  schema "profiles" do
    field :login_time, :utc_datetime
    # ...
    timestamps()
  end

end
© www.soinside.com 2019 - 2024. All rights reserved.