Generate random datetimes in rails with the minutes belongs to range(00, 30) - ruby-on-rails

Event model which has start and end datetime attributes in the database. I want to seed some random events but the event time should be proper.
For example:
6.times { date_range << DateTime.now + (rand * 21) }
generates
[Thu, 03 Aug 2017 21:22:48 +0530,
Tue, 08 Aug 2017 17:36:29 +0530,
Sat, 29 Jul 2017 06:19:51 +0530,
Sat, 29 Jul 2017 13:36:21 +0530,
Thu, 27 Jul 2017 15:08:55 +0530,
Fri, 04 Aug 2017 13:53:03 +0530]
which is the correct behaviour.
But how to generate random datetime like this:
[Thu, 03 Aug 2017 21:00:00 +0530,
Tue, 08 Aug 2017 17:30:00 +0530,
Sat, 29 Jul 2017 06:00:00 +0530,
Sat, 29 Jul 2017 13:00:00 +0530,
Thu, 27 Jul 2017 15:30:00 +0530,
Fri, 04 Aug 2017 13:00:00 +0530]
So in order to display these events properly on a calendar.

Could try separating out each segment and adding them onto the date individually
date_range = 6.times.collect do
DateTime.now.beginning_of_day + # starting from today
rand(21).days + # pick a random day, no further than 3 weeks out
rand(24).hours + # move forward to a random hour on that day
(rand(2) * 30).minutes # and then decide whether to add 30 minutes
end
or, could combine the hours + minutes
date_range = 6.times.collect do
DateTime.now.beginning_of_day + # starting from today
rand(21).days + # pick a random day, no further than 3 weeks out
(rand(48) * 30).minutes # pick a random interval of 30 minutes to add in
end

Found the working solution but not complete:
6.times { date_range << DateTime.parse((DateTime.now + (rand * 21)).beginning_of_hour.to_s) }
[Mon, 31 Jul 2017 06:00:00 +0530,
Thu, 03 Aug 2017 15:00:00 +0530,
Fri, 11 Aug 2017 14:00:00 +0530,
Mon, 31 Jul 2017 09:00:00 +0530,
Wed, 09 Aug 2017 16:00:00 +0530,
Sat, 12 Aug 2017 13:00:00 +0530]
This can work for now but need some datetime with 30 minutes as well.

Related

How to build a monthly range that takes in account the number of days of each month?

I have a range with a start_date, end_date and I want to get the same day of each month for the whole range, so here starting on the 30th of January I should get the 30th of each month:
start_date = Date.new(2019, 1, 30)
end_date = Date.new(2019, 12, 30)
range = (start_date...end_date)
dates = range.step(30).map(&:to_date)
dates
#=> [Wed, 30 Jan 2019,
# Fri, 01 Mar 2019,
# Sun, 31 Mar 2019,
# Tue, 30 Apr 2019,
# Thu, 30 May 2019,
# Sat, 29 Jun 2019,
# Mon, 29 Jul 2019,
# Wed, 28 Aug 2019,
# Fri, 27 Sep 2019,
# Sun, 27 Oct 2019,
# Tue, 26 Nov 2019,
# Thu, 26 Dec 2019]
I was using something like this for weeks but with months when you get to February for example it of course fails, so I would have to adjust to 28th.
I know I could loop and look at the month and do adjustments based on the start_date but it feels like a bad idea.
I think you can use either active support:
require 'active_support/time'
start_date = Date.parse('2019-10-31')
12.times.map { |i| start_date + i.month }
=> [
Thu, 31 Oct 2019,
Sat, 30 Nov 2019,
Tue, 31 Dec 2019,
Fri, 31 Jan 2020,
Sat, 29 Feb 2020,
Tue, 31 Mar 2020,
Thu, 30 Apr 2020,
Sun, 31 May 2020,
Tue, 30 Jun 2020,
Fri, 31 Jul 2020,
Mon, 31 Aug 2020,
Wed, 30 Sep 2020
]
or adjust: #next_month:
require 'date'
Date.parse('2019-10-31').next_month # => Sat, 30 Nov 2019

Subtracting two time ranges

I have two timeranges
Fri, 02 Aug 2019 10:09:58 UTC +00:00..Fri, 02 Aug 2019 23:59:59 UTC +00:00
Fri, 02 Aug 2019 11:09:58 UTC +00:00..Fri, 02 Aug 2019 12:09:58 UTC +00:00
What's the simplest way to subtract the second from the first so I get
[
Fri, 02 Aug 2019 10:09:58 UTC +00:00..Fri, 02 Aug 2019 11:09:58 UTC +00:00,
Fri, 02 Aug 2019 12:09:58 UTC +00:00..Fri, 02 Aug 2019 23:59:59 UTC +00:00
]
I would do something like this:
range_1 = (Time.parse('2019-08-02 10:09:58 UTC') .. Time.parse('2019-08-02 23:59:59 UTC'))
range_2 = (Time.parse('2019-08-02 11:09:58 UTC') .. Time.parse('2019-08-02 12:09:58 UTC'))
[(range_1.begin..range_2.begin), (range_2.end..range_1.end)]
#=> [2019-08-02 10:09:58 UTC..2019-08-02 11:09:58 UTC, 2019-08-02 12:09:58 UTC..2019-08-02 23:59:59 UTC]

How to get all the DATES between two input DATES In Rails

The user inputs the starting date and the end date. I want to get the dates between these two input dates.
I tried this:
(datestart..dateend).to_a
but it returns the whole month, and when I choose from the previous year, it gives an error ArgumentError (invalid date):.
This is the example return when I choose Jan. 1 2017 and Jan. 2 2017 - the result was whole month and an inaccurate dates.
[Tue, 12 Jan 2016, Wed, 13 Jan 2016, Thu, 14 Jan 2016, Fri, 15 Jan 2016, Sat, 16 Jan 2016, Sun, 17 Jan 2016, Mon, 18 Jan 2016, Tue, 19 Jan 2016, Wed, 20 Jan 2016, Thu, 21 Jan 2016, Fri, 22 Jan 2016, Sat, 23 Jan 2016, Sun, 24 Jan 2016, Mon, 25 Jan 2016, Tue, 26 Jan 2016, Wed, 27 Jan 2016, Thu, 28 Jan 2016, Fri, 29 Jan 2016, Sat, 30 Jan 2016, Sun, 31 Jan 2016, Mon, 01 Feb 2016, Tue, 02 Feb 2016, Wed, 03 Feb 2016, Thu, 04 Feb 2016, Fri, 05 Feb 2016, Sat, 06 Feb 2016, Sun, 07 Feb 2016, Mon, 08 Feb 2016, Tue, 09 Feb 2016, Wed, 10 Feb 2016, Thu, 11 Feb 2016, Fri, 12 Feb 2016]
Update! I Fix it by using
Date.strptime(params[:datestart_stat], '%m/%d/%Y')
start_date = Date.parse('date start')
end_date = Date.parse('date end')
(start..endd).to_a
Update! I was able to fix my mistake now. I just change the
Date.parse(params[:datestart_stat])
# to
Date.strptime(params[:datestart_stat], '%m/%d/%Y')
thank you guys.

ActiveRecord does not respect daylight saving time (DST)?

We're in the timezone Bern, which is +0100. But since we're now in summertime (we have daylight saving time), the current offset is +0200. In my rails app, I set the timezone using a wrapper in the application controller since I need to have user-based timezones:
around_filter :user_timezone
def user_timezone(&block)
Time.use_zone(current_timezone, &block)
end
Now the strange part:
Time.zone.now # 2013-04-10 10:32:56 +0200
# (correct offset)
SomeArModel.first.created_at # 2013-03-28 17:49:59 +0100
# (incorrect offset, no DST)
Is there any explanation for this?
Thats normal behavior, the DST change happened on Sun Mar 31 01:00:00 UTC 2013.
t = Time.mktime(2013, 03, 31, 1, 15, 0)
6.times do
t += 900
u = Time.at(t.to_i).utc
puts t.to_s + " " + u.to_s
end
output:
Sun Mar 31 01:30:00 +0100 2013 Sun Mar 31 00:30:00 UTC 2013
Sun Mar 31 01:45:00 +0100 2013 Sun Mar 31 00:45:00 UTC 2013
Sun Mar 31 03:00:00 +0200 2013 Sun Mar 31 01:00:00 UTC 2013
Sun Mar 31 03:15:00 +0200 2013 Sun Mar 31 01:15:00 UTC 2013
Sun Mar 31 03:30:00 +0200 2013 Sun Mar 31 01:30:00 UTC 2013
Sun Mar 31 03:45:00 +0200 2013 Sun Mar 31 01:45:00 UTC 2013

Iterating between two DateTimes, with a one hour step

I'm looking for an efficient way, in Ruby 1.9.x/Rails 3.2.x, to iterate between two DateTime objects, with a one-hour step.
('2013-01-01'.to_datetime .. '2013-02-01'.to_datetime).step(1.hour) do |date|
...
end
I understand that an issue with this is that 1.hour is just the number of seconds, but my attempts to convert that to a DateTime object and use that as the step doesn't work either.
I looked at "Beware of Ruby Sugar". It mentions, near the bottom, that DateTime has a direct step method. I confirmed this by running methods on a DateTime object, but I cannot find any documentation on step in DateTime, in either Ruby's or Rails' documents.
Similar to my answer in "How do I return an array of days and hours from a range?", the trick is to use to_i to work with seconds since the epoch:
('2013-01-01'.to_datetime.to_i .. '2013-02-01'.to_datetime.to_i).step(1.hour) do |date|
puts Time.at(date)
end
Note that Time.at() converts using your local time zone, so you may want to specify UTC by using Time.at(date).utc
Maybe late but, you can do it without Rails, for example to step with hours:
Ruby 2.1.0
require 'time'
hour_step = (1.to_f/24)
date_time = DateTime.new(2015,4,1,00,00)
date_time_limit = DateTime.new(2015,4,1,6,00)
date_time.step(date_time_limit,hour_step).each{|e| puts e}
2015-04-01T00:00:00+00:00
2015-04-01T01:00:00+00:00
2015-04-01T02:00:00+00:00
2015-04-01T03:00:00+00:00
2015-04-01T04:00:00+00:00
2015-04-01T05:00:00+00:00
2015-04-01T06:00:00+00:00
Or minutes:
#one_minute_step = (1.to_f/24/60)
fifteen_minutes_step = (1.to_f/24/4)
date_time = DateTime.new(2015,4,1,00,00)
date_time_limit = DateTime.new(2015,4,1,00,59)
date_time.step(date_time_limit,fifteen_minutes_step).each{|e| puts e}
2015-04-01T00:00:00+00:00
2015-04-01T00:15:00+00:00
2015-04-01T00:30:00+00:00
2015-04-01T00:45:00+00:00
I hope it helps.
Here's something I came up with recently:
require 'active_support/all'
def enumerate_hours(start, end_)
Enumerator.new { |y| loop { y.yield start; start += 1.hour } }.take_while { |d| d < end_ }
end
enumerate_hours(DateTime.now.utc, DateTime.now.utc + 1.day)
# returns [Wed, 20 Aug 2014 21:40:46 +0000, Wed, 20 Aug 2014 22:40:46 +0000, Wed, 20 Aug 2014 23:40:46 +0000, Thu, 21 Aug 2014 00:40:46 +0000, Thu, 21 Aug 2014 01:40:46 +0000, Thu, 21 Aug 2014 02:40:46 +0000, Thu, 21 Aug 2014 03:40:46 +0000, Thu, 21 Aug 2014 04:40:46 +0000, Thu, 21 Aug 2014 05:40:46 +0000, Thu, 21 Aug 2014 06:40:46 +0000, Thu, 21 Aug 2014 07:40:46 +0000, Thu, 21 Aug 2014 08:40:46 +0000, Thu, 21 Aug 2014 09:40:46 +0000, Thu, 21 Aug 2014 10:40:46 +0000, Thu, 21 Aug 2014 11:40:46 +0000, Thu, 21 Aug 2014 12:40:46 +0000, Thu, 21 Aug 2014 13:40:46 +0000, Thu, 21 Aug 2014 14:40:46 +0000, Thu, 21 Aug 2014 15:40:46 +0000, Thu, 21 Aug 2014 16:40:46 +0000, Thu, 21 Aug 2014 17:40:46 +0000, Thu, 21 Aug 2014 18:40:46 +0000, Thu, 21 Aug 2014 19:40:46 +0000, Thu, 21 Aug 2014 20:40:46 +0000, Thu, 21 Aug 2014 21:40:46 +0000]
I'm not entirely sure if this will help out but check out this stack overflow page, question seems similar.
calculate difference in time in days, hours and minutes

Resources