Check if time is between two times ruby - ruby-on-rails

I'm looking to check if the DateTime.now is between 2 particular date and times on Ruby on Rails.
I can't seem to figure it out
def closed?
(DateTime.now > DateTime.new(2018, 6, 28, 13, 00, 00)) && (DateTime.now < DateTime.new(2018, 6, 28, 14, 00, 00))
end

I would use the between?(min, max) function from ActiveSupport:
def closed?
DateTime.now.between?(DateTime.new(2018, 6, 28, 13, 00, 00), DateTime.new(2018, 6, 28, 14, 00, 00))
end

You can use cover? method for that
now = DateTime.now
start = DateTime.new(2018, 6, 28, 13, 00, 00)
stop = DateTime.new(2018, 6, 28, 14, 00, 00)
p (start..stop).cover? now
Hope it will help you :)

If you want to only compare time then you can simply do
Time.zone.now.between?(Time.zone.parse("12:00"), Time.zone.parse("18:00"))
Time zone is a important factor here.

Related

Why isn't the offset of Samoa +13 or +14 when using pytz?

I've just read
BBC: Samoa and Tokelau skip a day for dateline change, 30.12.2011
I wanted to see this with pytz, but everything I tried only showed an offset of -11, but not of +13 or +14:
>>> import pytz
>>> tz = pytz.timezone('Pacific/Samoa')
>>> tz_us = pytz.timezone('US/Samoa')
>>> import datetime
>>> datetime.datetime(2011, 12, 30, 9, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-29T22:00:00-11:00'
>>> datetime.datetime(2011, 12, 30, 10,00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-29T23:00:00-11:00'
>>> datetime.datetime(2011, 12, 30, 11, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-30T00:00:00-11:00'
>>> datetime.datetime(2011, 12, 31, 15, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-31T04:00:00-11:00'
>>> datetime.datetime(2015, 12, 31, 15, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2015-12-31T04:00:00-11:00'
>>> datetime.datetime(2011, 12, 31, 15, 00, tzinfo=datetime.timezone.utc).astimezone(tz_us).isoformat()
'2011-12-31T04:00:00-11:00'
>>> datetime.datetime(2015, 12, 31, 15, 00, tzinfo=datetime.timezone.utc).astimezone(tz_us).isoformat()
'2015-12-31T04:00:00-11:00'
Why can't I see the offset +13 / +14?
Both Pacific/Samoa and US/Samoa are aliases of Pacific/Pago_Pago, representing American Samoa, which is UTC-11 and did not skip that day.
For American Samoa, use Pacific/Pago_Pago
For the Independent State of Samoa, use Pacific/Apia
For Tokelau, use Pacific/Fakaofo
Personally, I prefer to only use canonical zone names. See the list on Wikipedia for reference.
See the timezone change with pytz
UTC time with offset:
>>> import pytz
>>> tz = pytz.timezone('Pacific/Apia')
>>> import datetime
>>> datetime.datetime(2011, 12, 30, 9, 59, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-29T23:59:00-10:00'
>>> datetime.datetime(2011, 12, 30, 10, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-31T00:00:00+14:00'
Local time:
>>> '{:%Y-%m-%d %H:%M}'.format(datetime.datetime(2011, 12, 30, 9, 59, tzinfo=datetime.timezone.utc).astimezone(tz))
'2011-12-29 23:59'
>>> '{:%Y-%m-%d %H:%M}'.format(datetime.datetime(2011, 12, 30, 10, 00, tzinfo=datetime.timezone.utc).astimezone(tz))
'2011-12-31 00:00'

Remove multiple ranges of values from array

a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a2 = [2..4, 8..11, 16..17]
Removing one range of values from an array can be done like this:
[1, 2, 3, 4, 5, 6, 7, 8, 9].slice!(2..5)
Iterating over the ranges and apply the same as above (a2.each { |range| a1.slice!(range) }) isn't perfect though. The ranges overlap sometimes and thus destroy the referencing index for the other ranges.
So, any suggestions on how to remove the ranges in a2 from a1 in the most efficient way?
a1 is normally [*0..10080] long. a2 has about 30 ranges, each containing hundreds of values.
If the result of the first operation impacts the second you're either going to have to track the resulting offset implications, which can get crazy complicated, or simply go about doing the reverse operation and instead flag which you want or don't want using the ranges:
require 'set'
a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a2 = [2..4, 8..11, 16..17]
# Convert the ranges to a set of index values to remove
reject = Set.new(a2.flat_map(&:to_a))
# Using value/index pairs, accumulate those values which are
# not being excluded by their index.
a1.each_with_index.each_with_object([ ]) do |(v, i), a|
a << v unless (reject.include?(i))
end
# => [0, 1, 5, 6, 7, 12, 13, 14, 15, 18, 19, 20]
[-1, *a2.flat_map(&:minmax), a1.length].each_slice(2).flat_map{|i,j| a1[i+1...j]}
# => [0, 1, 5, 6, 7, 12, 13, 14, 15, 18, 19, 20]
I'm not sure this is the least naive solution, but it seems simple to convert your ranges into arrays so you're dealing with like-for-like:
a2.each{ |a| a1 = a1 - a.to_a }
a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a2 = [2..4, 8..11, 16..17]
a1 - a2.flat_map(&:to_a)

How to find the remaining time between two dates in a time zone in Ruby on Rails?

I am trying to do countdown for certain events using flipclock in Ruby on Rails.
I tried to find the time difference of an EST event.
time_diff = <%= (Time.utc(2016, 7, 2, 18, 14, 0).in_time_zone("Eastern Time (US & Canada)") - Time.now.in_time_zone("Eastern Time (US & Canada)")).to_i.abs %>;
I tried to check if it was correct by doing a one hour difference but I am always getting wrong number of hours left.
I think this is what you're after:
(ActiveSupport::TimeZone.new("EST").local(2016, 7, 2, 18, 14, 0) - Time.now).to_i.abs
The code in your question is equivalent to
(Time.utc(2016, 7, 2, 18, 14, 0) - Time.now).to_i.abs
You probably expect the first part of the expression to mean "18:14 EST" but it means "18:14 UTC… but show what time that was in EST".
It might clarify things if you note that
Time.utc(2016, 7, 2, 18, 14, 0).in_time_zone("EST") == Time.utc(2016, 7, 2, 18, 14, 0)
because
Time.utc(2016, 7, 2, 18, 14, 0).in_time_zone("EST").to_i == Time.utc(2016, 7, 2, 18, 14, 0).to_i
but
Time.utc(2016, 7, 2, 18, 14, 0).in_time_zone("EST").to_s != Time.utc(2016, 7, 2, 18, 14, 0).to_s

Rails array with weeks and year

I want to list all weeknumbers together with year.
This is what I have:
start # 2012-05-10
ende # 2013-06-20
while start < ende
weeks << start.cweek
start += 1.week
end
List all weeknumbers:
#kws.each do |w|
w
end
I need some inspiration how to assign the corresponding year to each weeknumber..
So that I get 22 / 2012 23 / 2012 etc..
Thanks for help..
When in your while loop, you can also store the year, and one easy way is just as an array of arrays.
Then in your each loop later you can get access to both:
start = Date.new( 2012, 5, 10 )
ende = Date.new( 2013, 6, 20 )
weeks = []
while start < ende
weeks << [start.cweek, start.year] # <-- enhanced
start += 1.week
end
weeks.each do |w,y| # <-- take two arguments in the block
puts "#{w} / #{y}" # and print them both out
end
Results:
=>
19 / 2012
20 / 2012
21 / 2012
22 / 2012
23 / 2012
24 / 2012
25 / 2012
...
22 / 2013
23 / 2013
24 / 2013
Create a hash instead with key as a year and value as an array of week numbers
start # 2012-05-10
ende # 2013-06-20
weeks ={}
while start < ende
weeks[start.year] = [] unless weeks[start.year]
weeks[start.year] << start.cweek
start += 1.week
end
p weeks
and you get o/p
=> {2012=>[19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52],
2013=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24]}
(10.weeks.ago.to_date..Date.today.to_date).map(&:beginning_of_week).uniq

How to identify if the given day, month, and year, combine a legal date?

Given 3 numbers: DD, MM, YYYY, what is the easiest way to know if they combine a legal date ?
Examples:
14, 05, 2011 => Legal
29, 02, 2011 => Illegal
29, 02, 2012 => Legal
35, 11, 1989 => Illegal
14, 18, 2011 => Illegal
14, 00, 2011 => Illegal
00, 11, 1979 => Illegal
31, 11, 1979 => Illegal
You can use valid_date? But it's YYYY, MM, DD:
irb(main):015:0> require 'date'
=> true
irb(main):021:0> Date::valid_date?(2011,05,14)
=> true
irb(main):022:0> Date::valid_date?(2011,02,29)
=> false
irb(main):023:0> Date::valid_date?(2012,02,29)
=> true
Date has a method valid_civil? .
require 'date'
dates = DATA.readlines.map{|line| line.split(', ').map(&:to_i)}
dates.each do |date|
d, m, y = date
puts Date.valid_civil?(y, m, d)
end
__END__
14, 05, 2011
29, 02, 2011
29, 02, 2012
35, 11, 1989
14, 18, 2011
14, 00, 2011
00, 11, 1979
31, 11, 1979
One option is to use something like:
require 'time'
def valid(year,month,day)
Time.parse "#{year}#{month}#{day}" rescue return false
return true
end

Resources