我有带有时区信息的日期变量。当我使用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.
如果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