Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails时间对象_Ruby On Rails_Date_Time_Controller - Fatal编程技术网

Ruby on rails Rails时间对象

Ruby on rails Rails时间对象,ruby-on-rails,date,time,controller,Ruby On Rails,Date,Time,Controller,我的events controller中有一个快速添加操作,因为客户端实际上只在给定的一天中的三个不同时段安排事件。日期和时间与默认表单配合得很好,但是试图手动设置值会给我带来一些麻烦 def quick_add #params are date like 2012-04-29, timeslot is a string timeslot = params[:timeslot].to_sym date = params[:date].to_date @workout = Workout.ne

我的events controller中有一个快速添加操作,因为客户端实际上只在给定的一天中的三个不同时段安排事件。日期和时间与默认表单配合得很好,但是试图手动设置值会给我带来一些麻烦

def quick_add #params are date like 2012-04-29, timeslot is a string

timeslot = params[:timeslot].to_sym
date = params[:date].to_date

@workout = Workout.new do |w|
  w.name = 'Workout!'
  w.date = date
  case timeslot
  when :morning
    w.time = Time.local(w.date.year, w.date.month, w.date.day, 6)
  when :noon
    w.time = Time.local(w.date.year, w.date.month, w.date.day, 12)
  when :evening
    w.time = Time.local(w.date.year, w.date.month, w.date.day, 18, 15)
  else
    w.time = Time.now
  end
end
正在创建事件,日期是正确的,但时间是:

Morning: 2000-01-01 10:00:00 UTC 
  Expected: 2012-05-02 06:00:00 UTC -400
Noon: 2000-01-01 16:00:00 UTC 
  Expected: 2012-05-02 12:00:00 UTC -400
Evening: 2000-01-01 22:15:00 UTC 
  Expected: 2012-05-02 18:15:00 UTC -400

值得注意的是,在rails控制台中运行命令似乎可以得到我期望的结果。

时间值以整数形式存储在UTC/GMT(+0)时区中,因此不必存储任何时区数据。Rails总是将时间作为UTC时间存储在数据库中。当您读出它们时,您将希望再次将它们转换回您的本地时区


您可以使用
time.getlocal
将其转换为您的本地时区。

您的句子“事件正在创建,日期正确,但时间是:”,是否有错误?日期不正确,时间正确。可能
params[:date]。to_data
没有给您预期的结果???检查请求参数以确保
params[:date]
正确。我将日期存储在一个单独的字段中,该字段工作正常。时间对象是有问题的对象