ruby check a range of dates using date object? - ruby

I am new to ruby. I am practising booking a holiday on a fake airbnb but I want to check if the dates I want (start_of_holiday to end_of_holiday) is available in the dates of the room(start_date - end_date) but I can't seem to compare the ranges or convert the dates into ranges. Is there a better way of checking? thank you
require 'date'
start_of_holiday = Date.parse("2022-10-02").strftime('%Y-%m-%d')
end_of_holiday = Date.parse("2022-10-05").strftime('%Y-%m-%d')
start_date = '2022-10-01'
end_date = '2022-10-11'
print true if (start_of_holiday..end_of_holiday).include?(start_date..end_date)

The problem is that you're passing a range to include? which will return false in this case as the whole range (start_date..end_date) is not in (start_of_holiday..end_of_holiday).
From the docs;
include?(obj) → true or false
Returns true if obj is an element of the range, false otherwise.
You could simply adjust it to check for start_of_holiday and end_of_holiday
(start_date..end_date).cover?(start_of_holiday) && (start_date..end_date).cover?(end_of_holiday)
# true

Related

How to check if event's date is within a date range?

I have events (from an Event model) that have a starts_at: value in the form of a datetime. e.g.:
2016-02-18 11:00:00:00000
What I want to be able to do is check whether an event is starting this week.
I want to be able to make a list of events that are occuring this week (starting from the latest Monday).
#events = #calendar.events.where( ... )
I thought something along the lines of this:
start_week = Date.today.beginning_of_week(:monday).day()
end_week = Date.today.beginning_of_week(:monday).day()+6
range = start_week..end_week
#events = #calendar.events.where(starts_at: in range)
But it doesn't take into account the month or year. Also I'm not sure how to write the 'where' clause. How should I go about doing this? Thanks
Try this:
start_week = Date.today.beginning_of_week(:monday)
end_week = Date.today.beginning_of_week(:monday)+6
range = start_week..end_week
#events = #calendar.events.where(starts_at: range)
Assuming you want all the events from the current week, something like this should work:
#events = #calendar.events.where(starts_at: Time.zone.today.all_week)
all_week returns a Date range covering the current week.

Getting date value from an array hash

I have a hash that looks like
tagArray = { :"2014Date"=>["11/22/2014"], :"2015Date"=>["03/21/2015"] }
Since i already know for a given key, there is only one element in the array of 'values', how can you get just the value not as an array?
value = tagArray[:"2015Date"]
=>
value = ["04/12/2015"]
You can just index the array then and get the date like
value = value.fetch(0).to_s
=>
value = "04/12/2015"
However I am looking for a more elegant way of doing it.
Note: I am using Ruby 2.2, so need to 'strp' the date first to change it from mm/dd/yyyy format which is the end goal.
This is a bit simpler:
value = tagArray[:"2015Date"].last
=>
value = "03/21/2015"

How do I iterate through each date between two defined dates in ruby?

I have two dates, the begins_at (datetime) and the ends_at (datetime). I now simply want to iterate through every date between these two (including the ends_ and begins_at dates).
begins_at = Date.strptime("12/10/2014", "%m/%d/%Y")
ends_at = Date.strptime("12/20/2014", "%m/%d/%Y")
//iterate through all dates in this range kind of like this:
range = DateRange(begins-at,ends-at)
range.DateTime.each do |date|
....
end
Does anyone have an idea how I could achieve this?
You should use Range:
(begins_at..ends_at).each do |date|
# ...
end

Calculating True and False in PowerPivot DAX

I have a custom calculation using the number of days.
<= 5 and my DAX calc is =[Total Days Aged]<=5
=6 and <=10 and my DAX calc is =([Total Days Aged]<=10)&&([Total Days Aged]>=6)
My calculation are above which are returning 'True' or 'False expressions. How can I calculate the total number of 'true' values in the column?
I've tried the following and the IF function, but I can't get it to work.
=calculate(DISTINCTCOUNT(Query[0-5 Days]), Query[0-5 Days] = "True")
=CALCULATE(DISTINCT(Query[0-5 Days]), Query[0-5 Days] = "True")
My error message is: "DAX comparison operations do not support comparing values type of true/false of values with type text."
Thanks in advance,
It looks like you are trying to compare a logical true/false with a string. You should try replacing the string "True" in the query with logical value TRUE(). Try the query below.
=CALCULATE(DISTINCT(Query[0-5 Days]), Query[0-5 Days] = TRUE() )

Date comparison in Rails 3.2

Stage:
I have this model:
Promo(id: integer, start_date: datetime, end_date: datetime)
I want to know which current promotions.
May be our query should be like:
SELECT * FROM promos WHERE now BETWEEN start_date AND end_date;
Question:
How should I make it in Ruby?
Which is the correct way?
Thank you.
Rails is intelligent. If you retrieve a date/time, it will converted to DateTime object.
On contrary, passing DateTime object to the where clause (for Rails 3.x), it will correctly build the where clause.
String passed to where clause will be passed to the SQL where clause. If you pass an array,
second and later elements will be replaced with '?' character in the first element.
So, for your case:
Promo.where(["? between start_date and end_date", DateTime.now])
works. I verified on my Rails model(Position) which happen to have start_date and end_date, and works correctly:
[1] pry(main)> Position.where(["? between start_date and end_date", DateTime.now]).count
=> 2914
It's on Rails 3.2.8 with PostgreSQL.
Yes, you can use comparison operators to compare dates e.g.:
irb(main):018:0> yesterday = Date.new(2009,6,13)
=> #<Date: 4909991/2,0,2299161>
irb(main):019:0> Date.today > yesterday
=> true
But are you trying to compare a date to a datetime?
If that's the case, you'll want to convert the datetime to a date then do the comparison.
or
Date.parse('2010-11-01') < Date.today
will return true if '2010-11-01' has already passed
I hope this helps.
Haven't tested this, but try something like:
currentTime = Time.now()
Promo.find(
:all,
:conditions => ['start_date < ? AND end_date > ?', currentTime, currentTime]
)
You might need to use the Date class if the Time class doesn't work

Resources