Rails Date Manipulation

Here's a simple date manipulation task from a real project. An install item is a "unit" that is to be scheduled for a production install. Generally, this would be a software install, but it could just as easily be a hardware install.

The default installation window for a production install is 4:00 AM to 6:00 AM, so as not to interfere with regular business operations. The task is to come up with a default date for an emergency install, i.e. - according to the customer, the window should be the upcoming window for the next day.

Here's the code in Rails to calculate the start and end dates for the next install window:

install_item.requested_install_date_start =
   Time.now.tomorrow.at_beginning_of_day + 4.hours

install_item.requested_install_date_end =
   Time.now.tomorrow.at_beginning_of_day + 6.hours

Is that cool or what? In Java, you'd be creating a Calendar object, a Format object, etc., just to perform the date manipulation. Here, it's all done with the simple statement: "Time.now.tomorrow.at_beginning_of_day + 4.hours".


No comments yet. Be the first.

Leave a Comment

Comments are moderated and will not appear on the site until reviewed.

(not displayed)