UTC time resets to 2000-01-01 (ruby). How do I prevent the time from resetting?

谁都会走 提交于 2019-12-02 04:15:22

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/7515398/utc-time-resets-to-2000-01-01-ruby-how-do-i-prevent-the-time-from-resetting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!