我怎么能在Rails中按时间段(HHMM~HHMM)拟合数组

时间:2015-07-19 00:15:05

标签: ruby-on-rails ruby ruby-on-rails-4 mongoid

我通过mongoid获得了这些航班。

如何在departure_at

之间按time interval (HHMM)快速过滤航班

例如,

当时间间隔是字符串06:00~09:00

结果应为

[2] #<Jetstar _id: 20150720_0655_1040_TPE_KIX|OSA, departure_at: 2015-07-20 06:55:00 UTC
[5] #<Jetstar _id: 20150722_0655_1040_TPE_KIX|OSA, departure_at: 2015-07-22 06:55:00 UTC
[7] #<Jetstar _id: 20150723_0655_1040_TPE_KIX|OSA, departure_at: 2015-07-23 06:55:00 UTC

当时间间隔是字符串10:00~18:00

结果应为

[1] #<Jetstar _id: 20150719_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-19 12:45:00 UTC
[3] #<Jetstar _id: 20150720_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-20 12:45:00 UTC
[4] #<Jetstar _id: 20150721_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-21 12:45:00 UTC
[6] #<Jetstar _id: 20150722_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-22 12:45:00 UTC
[8] #<Jetstar _id: 20150723_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-23 12:45:00 UTC
[9] #<Jetstar _id: 20150724_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-24 12:45:00 UTC

整个日期集

[1] #<Jetstar _id: 20150719_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-19 12:45:00 UTC
[2] #<Jetstar _id: 20150720_0655_1040_TPE_KIX|OSA, departure_at: 2015-07-20 06:55:00 UTC
[3] #<Jetstar _id: 20150720_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-20 12:45:00 UTC
[4] #<Jetstar _id: 20150721_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-21 12:45:00 UTC
[5] #<Jetstar _id: 20150722_0655_1040_TPE_KIX|OSA, departure_at: 2015-07-22 06:55:00 UTC
[6] #<Jetstar _id: 20150722_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-22 12:45:00 UTC
[7] #<Jetstar _id: 20150723_0655_1040_TPE_KIX|OSA, departure_at: 2015-07-23 06:55:00 UTC
[8] #<Jetstar _id: 20150723_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-23 12:45:00 UTC
[9] #<Jetstar _id: 20150724_1245_1630_TPE_KIX|OSA, departure_at: 2015-07-24 12:45:00 UTC

2 个答案:

答案 0 :(得分:1)

在mongoid中,您可以使用它来搜索范围内的记录。我不建议您直接在Ruby中进行过滤,因为对于较大的数据集,这将是缓慢且占用内存的。

这篇文章通过一个解决方案:

Query Mongodb on month, day, year... of a datetime

但是,如果你想用纯红宝石做,你可以这样做:

start, finish = "06:00~09:00".split('~')
start_hour = start.split(':')[0].to_i
end_hour = finish.split(':')[0].to_i

flights.select do |flight|
  flight.departure_time.hour >= start_hour && flight.departure_time.hour <= end_hour
end

这只会查询几个小时。您可以通过以下方式包括会议记录:

start, finish = "06:00~09:00".split('~')
start = start.split(':').map(&:to_i)
finish = finish.split(':').map(&:to_i)

# Work in minutes
start_minutes = start[0] * 60 + start[1]
finish_minutes = finish[0] * 60 + finish[1]

flights.select do |flight|
  dep_mins = flight.departure_time.hour * 60 + flight.departure_time.min
  dep_mins >= start_minutes && dep_mins <= finish_minutes
end

答案 1 :(得分:1)

假设12 AA 356djs 225gsd 1245gr BB 123asd 123asd 123asd CC TToptg TToptg gngngn DD sIG123 gjn123 uot123 EE 166131 100021 205011 LL lplplp 122121 aggagg KK hbnkio ohgimp 125125 TT KGNskg fgnjdg 125154 PP ginini 216361 sgdaga ZZ gonhon q215ag hagqgq RR TKEMMM 125sdg 125961 II tninks 150121 192u9u 12 AA 356djs 225gsd 1245gr BB 123asd 123asd 123asd CC TToptg TToptg gngngn DD sIG123 gjn123 uot123 EE 166131 100021 205011 LL lplplp 122121 aggagg KK hbnkio ohgimp 125125 TT KGNskg fgnjdg 125154 PP ginini 216361 sgdaga ZZ gonhon q215ag hagqgq RR TKEMMM 125sdg 125961 II tninks 150121 192u9u .... (repeating until 100th data block) datesets个对象的数组:

Jetstar