samedi 22 juillet 2017

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

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.




Aucun commentaire:

Enregistrer un commentaire