Ruby on rails 如何格式化时间戳范围postgresql rails
我有一个使用Postgresql的Rails应用程序。我希望用户能够为特定的一天选择一个时间范围。例如,如果我是用户,我希望在星期一选择(上午9:00-下午2:00)作为我的时间范围 然后我将如何格式化以保存在Postgresql数据库中 迁移:Ruby on rails 如何格式化时间戳范围postgresql rails,ruby-on-rails,ruby,postgresql,Ruby On Rails,Ruby,Postgresql,我有一个使用Postgresql的Rails应用程序。我希望用户能够为特定的一天选择一个时间范围。例如,如果我是用户,我希望在星期一选择(上午9:00-下午2:00)作为我的时间范围 然后我将如何格式化以保存在Postgresql数据库中 迁移: class CreateMondayTimeFrames < ActiveRecord::Migration def change create_table :monday_time_frames do |t| t.bel
class CreateMondayTimeFrames < ActiveRecord::Migration
def change
create_table :monday_time_frames do |t|
t.belongs_to :user, index: true
t.tstzrange :time_range
t.timestamps null: false
end
end
end
def create
# The params I am passing in:
# params[:time_start] => "9:00am", params[:time_stop] => "2:00pm"
# Sudo: formatted_timestamp_range = (params[:time_start]..params[:time_stop]).convert_to_range
end
$ start = Time.zone.parse("2017-03-26 9:00am").to_i
$ stop = Time.zone.parse("2017-03-26 2:00pm").to_i
$ now = Time.zone.parse("2017-03-26 12:00pm").to_i
$ (start..stop).include?(now)
$ => true
通过将数据类型更改为
int8range
解决了此问题。如果有人能给出tstzrange
的答案,我会将你的答案标记为正确答案
class CreateMondayTimeFrames < ActiveRecord::Migration
def change
create_table :monday_time_frames do |t|
t.belongs_to :user, index: true
t.int8range :time_range
t.timestamps null: false
end
end
end
在控制台中测试:
class CreateMondayTimeFrames < ActiveRecord::Migration
def change
create_table :monday_time_frames do |t|
t.belongs_to :user, index: true
t.tstzrange :time_range
t.timestamps null: false
end
end
end
def create
# The params I am passing in:
# params[:time_start] => "9:00am", params[:time_stop] => "2:00pm"
# Sudo: formatted_timestamp_range = (params[:time_start]..params[:time_stop]).convert_to_range
end
$ start = Time.zone.parse("2017-03-26 9:00am").to_i
$ stop = Time.zone.parse("2017-03-26 2:00pm").to_i
$ now = Time.zone.parse("2017-03-26 12:00pm").to_i
$ (start..stop).include?(now)
$ => true
不要添加答案,然后请求其他问题的解决方案。改为单独问一个问题。