如何使用 Elixir 和 Ecto 比较日期
How to compare dates with Elixir and Ecto
我有一个从数据库列中获取的 Ecto.DateTime
实例,我需要检查该日期是否超过 5 分钟。
最好的方法是什么?
为了更清楚,这是我在 ruby ->
中需要的
updated_at < (Time.now - 5.minutes)
您可以使用 datetime_add/3 将负值用作 count
参数:
from u in User,
where: u.reset_password_sent_at > datetime_add(^Ecto.DateTime.utc, -5, "minute")
如果您需要在不使用查询的情况下执行此操作,您可以使用 :calendar erlang 模块中的函数:
ecto_5_mins_ago =
Ecto.DateTime.utc
|> Ecto.DateTime.to_erl
|> :calendar.datetime_to_gregorian_seconds
|> Kernel.-(60 * 5)
|> :calendar.gregorian_seconds_to_datetime
|> Ecto.DateTime.from_erl
Ecto.DateTime.compare(u.reset_password_token, ecto_5_mins_ago)
我有一个从数据库列中获取的 Ecto.DateTime
实例,我需要检查该日期是否超过 5 分钟。
最好的方法是什么?
为了更清楚,这是我在 ruby ->
中需要的updated_at < (Time.now - 5.minutes)
您可以使用 datetime_add/3 将负值用作 count
参数:
from u in User,
where: u.reset_password_sent_at > datetime_add(^Ecto.DateTime.utc, -5, "minute")
如果您需要在不使用查询的情况下执行此操作,您可以使用 :calendar erlang 模块中的函数:
ecto_5_mins_ago =
Ecto.DateTime.utc
|> Ecto.DateTime.to_erl
|> :calendar.datetime_to_gregorian_seconds
|> Kernel.-(60 * 5)
|> :calendar.gregorian_seconds_to_datetime
|> Ecto.DateTime.from_erl
Ecto.DateTime.compare(u.reset_password_token, ecto_5_mins_ago)