【发布时间】:2018-09-26 14:13:50
【问题描述】:
我想从用户那里得到一个日期输入并比较(如果它是未来至少 30 分钟)它是否是未来并将其保存到数据库中。 用户输入的日期是这样的 2018-09-26 用户输入的时间是这样的 16:00 所以我将它转换为天真的日期时间
def build_naive_datetime(date, time) do
{:ok, datetime} = NaiveDateTime.from_iso8601("#{date} #{time}:00")
datetime
end
所以它返回~N[2018-09-26 16:00:00]。
我想比较一下。
def schedule_datetime_is_future?(schedule_datetime) do
# 30 mins future from now on
future_datetime = DateTime.to_naive(Timex.add(Timex.now, Timex.Duration.from_minutes(30)))
case Timex.compare(schedule_datetime, future_datetime) do
1 ->
# schedule datetime is future
true
-1 ->
# schedule datetime is not future
false
_ ->
false
end
end
但 Timex.now 在我的开发计算机时区时间返回(可能返回 utc)。 所以比较不能正常工作。(用户输入的日期时间是当地时间。) 我怎样才能以更好的方式做到这一点? 我是否必须将用户日期时间更改为 UTC 时间? 我该怎么做?
谢谢
【问题讨论】: