【发布时间】:2013-12-21 11:21:08
【问题描述】:
我不知道怎么做,但我的控制台和服务器对于 DateTime.now 有两个不同的时区。如果我在控制台中运行 DateTime.now,则会返回以下内容:
Wed, 04 Dec 2013 14:27:23 -0500
但是,我的任务模型中有以下内容:
def self.already_expired
where("time_frame < ?", DateTime.now)
end
上面代码的目标是在“time_Frame”是过去日期的任务上触发一些任务方法。这是我调用该方法的地方:
task :change_it => :environment do
puts "yo yo you yo you"
@tasks = Task.already_expired
@tasks.each do |task|
puts "Kalabar" + task.inspect + "now time is:" + DateTime.now.to_s
end
end
服务器日志显示:
Task Load (1.2ms) SELECT "tasks".* FROM "tasks" WHERE (time_frame < '2013-12-04 17:25:37')
Kalabar#<Task id: 3, title: "task 1 - past", content: "Should show this one",
created_at: "2013-12-04 17:05:53", updated_at: "2013-12-04 17:05:53",
schedule_id: 2, amount: nil, time_frame: "2013-12-02 08:15:00">
now time is:2013-12-04T12:25:37-05:00
Kalabar#<Task id: 5, title: "another task - past",
content: "should show this one", created_at: "2013-12-04 17:11:23",
updated_at: "2013-12-04 17:11:23", schedule_id: 3, amount: nil,
time_frame: "2013-12-03 02:07:00">now time is:2013-12-04T12:25:37-05:00
Kalabar#<Task id: 6, title: "another task - future",
content: "should not show at first", created_at: "2013-12-04 17:11:23",
updated_at: "2013-12-04 17:25:09", schedule_id: 3, amount: nil,
time_frame: "2013-12-04 12:27:00">now time is:2013-12-04T12:25:37-05:00
因此,它将 time_frame 与 UTC 时间的 '2013-12-04 12:25:37 进行比较。但是,当我让它打印 DateTime.now.to_s 时,它会打印“2012-12-04 08:15:00”。
我想在所有内容进入数据库之前将其转换为 UTC。我也不明白为什么它有两个不同的时区 DateTime.now 这是我的问题
- 为什么 DateTime.now 有两个不同的时区
- 如何在保存到数据库之前将时区转换为 UTC?
更新:
我将以下内容放入我的 application.rb 文件中:
config.time_zone = 'Eastern Time (US & Canada)'
现在用户输入东部时间的日期时间,并将其存储为 UTC 时间。所以现在 time_frame 正在与 DateTime.now 进行正确比较。问题是,如果我放置 time_frame,它会将其放置在东部时间。我不太确定发生了什么事。它将它存储在UTC,但在东部时间打印。这是危险的/正确的吗?我在任务代码块中添加了一个“puts time_Frame”,如下所示:
task :change_it => :environment do
@tasks = Task.already_expired
@tasks.each do |task|
puts "Kalabar" + task.inspect + "now time is:" + DateTime.now.to_s
puts "time_frame is:" + task.time_frame.to_s
end
end
它会输出这个:
Kalabar#<Task id: 3, title: "task 1 - past", content: "Should show this one", created_at: "2013-12-04 17:05:53", updated_at: "2013-12-04 17:05:53", schedule_id: 2, amount: nil, time_frame: "2013-12-02 08:15:00">now time is:2013-12-04T15:27:22-05:00
time_frame is:2013-12-02 03:15:00 -0500
Kalabar#<Task id: 7, title: "Task 1 - past by days", content: "Should always show", created_at: "2013-12-04 20:20:40", updated_at: "2013-12-04 20:20:40", schedule_id: 4, amount: nil, time_frame: "2013-12-02 09:02:00">now time is:2013-12-04T15:27:22-05:00
time_frame is:2013-12-02 04:02:00 -0500
Kalabar#<Task id: 8, title: "Task 2 - past by minutes", content: "should always show", created_at: "2013-12-04 20:20:40", updated_at: "2013-12-04 20:20:40", schedule_id: 4, amount: nil, time_frame: "2013-12-04 20:15:00">now time is:2013-12-04T15:27:22-05:00
time_frame is:2013-12-04 15:15:00 -0500
所以,如您所见,在数据库中它是 UTC,但当它输入时它是东部时间。为什么要这样做/危险吗?
【问题讨论】:
标签: ruby-on-rails ruby datetime time datetime-format