user3675188
user3675188

Reputation: 7409

How could I fitler array by time period(HHMM~HHMM) in Rails

I got the flights by mongoid.

How could I quick filter the flights by departure_at between time interval (HHMM)

For example,

When the time interval is a string 06:00~09:00

The result should be

[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

When the time interval is a string 10:00~18:00

The result should be

[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

whole datesets

[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

Upvotes: 0

Views: 64

Answers (2)

Kirill
Kirill

Reputation: 1538

Suppose datesets is an array of Jetstar objects:

require 'time'

def date_filter(datesets, min, max)
  datesets.select { |d| Time.parse(d.departure_at).hour.between?(min, max) }
end

# 06:00~09:00 inclusive
p(date_filter(datesets, 6, 9))

# 10:00~18:00 inclusive
p(date_filter(datesets, 10, 18))

Upvotes: 1

Dan Draper
Dan Draper

Reputation: 1121

In mongoid you can use between to do a search of records within a range. I don't recommend that you do the filtering directly in Ruby as this will be slow and memory intensive for larger data set.

This post goes through a solution:

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

However, if you want to do it in pure ruby you could do:

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

This will just query for hours. You can include minutes by doing:

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

Upvotes: 1

Related Questions