UTC 时间重置为 2000-01-01 (ruby)。如何防止时间重置?
我正在使用任务和电子表格 gem 将 Excel 电子表格读入我的数据库。我正在阅读的专栏之一是“start_time”。为此,我将形成一个值数组,然后逐一传入每个数组值。
cnum_array = [] # for start times
sheet1.each 3 do |row|
unless row[9].blank?
time = Time.parse(row[9])
cnum_array << time.utc
end
end
count = 0
for course in Course.all
course.update_attribute :start_time, cnum_array[count]
count += 1
end
这似乎工作正常。如果我在最后一个循环中插入“puts course.start_time”语句,它将打印出正确的时间。就像这样:
count = 0
for course in Course.all
course.update_attribute :start_time, cnum_array[count]
puts course.start_time
count += 1
end
这给了我正确的时间,例如“2012-01-23 15:30:00”。
但是当我稍后查找课程时间时(例如通过控制台的 Course.find(1).start_time),它给了我“2000-01-01 15:20:00”。因此,一天中的时间是正确的,但这一天本身可以追溯到 2000 年 1 月 1 日。
有谁知道为什么会发生这种情况,以及我该如何解决它?谢谢!
I'm using a task and the spreadsheet gem to read in an excel spreadsheet into my database. One of the columns I'm reading in is "start_time." To do this, I'm forming an array of values, then passing in each of these array values, one by one.
cnum_array = [] # for start times
sheet1.each 3 do |row|
unless row[9].blank?
time = Time.parse(row[9])
cnum_array << time.utc
end
end
count = 0
for course in Course.all
course.update_attribute :start_time, cnum_array[count]
count += 1
end
This seems to work fine. If I insert a "puts course.start_time" statement within this last loop, it prints off the right time. Like so:
count = 0
for course in Course.all
course.update_attribute :start_time, cnum_array[count]
puts course.start_time
count += 1
end
This gives me the right time, e.g. "2012-01-23 15:30:00."
But when I look up the course time later (e.g. via my console's Course.find(1).start_time), it gives me "2000-01-01 15:20:00." So the time of day is right, but the day itself goes back to 2000-01-01.
Does anyone know why this is happening, and how I can fix it? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
Time
类。本课程涉及时间,而不是日期。我的猜测是您的数据库列也是time
类型。我建议您使用
datetime
(或可能timestamp
)列类型。You are using the
Time
class. This class deals with times, not dates. My guess is that your database column is of typetime
as well.I recommend you use a
datetime
(or possiblytimestamp
) column type.