UTC时间重置为2000-01-01(ruby)。如何避免重置时间? [英] UTC time resets to 2000-01-01 (ruby). How do I prevent the time from resetting?

查看:144
本文介绍了UTC时间重置为2000-01-01(ruby)。如何避免重置时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用任务和电子表格gem将excel电子表格读入我的数据库。我正在阅读的列之一是start_time。为了做到这一点,我正在形成一个值数组,然后逐个传递这些数组值。

  cnum_array = []#开始时间
sheet1.each 3 do | row |
除了row [9] .blank?
time = Time.parse(row [9])
cnum_array<<< time.utc
end
end

count = 0
当然在Course.all
course.update_attribute:start_time,cnum_array [count]
count + = 1
end

这似乎工作正常。如果我在最后一个循环中插入了一个puts course.start_time语句,那么它会在正确的时间打印出来。像这样:

  count = 0 
当然在Course.all
course.update_attribute:start_time, cnum_array [count]
puts course.start_time
count + = 1
end


$ b $这给我正确的时间,例如2012-01-23 15:30:00



但是当我稍后查看课程时间(例如通过我的控制台的Course.find(1).start_time ),它给了我2000-01-01 15:20:00。所以一天中的时间是正确的,但这一天本身可以追溯到2000-01-01。有没有人知道为什么会发生这种情况,我该如何解决?谢谢!

解决方案

您正在使用 Time 类。这个课程处理时间,而不是日期。我的猜测是,您的数据库列的类型为 time



我建议您使用 datetime (或可能 timestamp )列类型。


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!

解决方案

You are using the Time class. This class deals with times, not dates. My guess is that your database column is of type time as well.

I recommend you use a datetime (or possibly timestamp) column type.

这篇关于UTC时间重置为2000-01-01(ruby)。如何避免重置时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆