Rounding Hours - Ruby

A year ago I found myself in need of having to round hours in a rails application. I asked the question on - Stack Overflow and it's been one of my most up-voted answers.

My dilemma was rounding a specified time to the hour for test data (and for other reasons I can't quite remember..). However, I thought I'd share the tip as it provided me with my first gold badge on SO.

The original answer suggested creating a custom function:

def random_time_to_nearest_hour
  time = rand(1.year).ago
  time - time.sec - 60 * time.min

But as luck would have it, RoR comes with a DateTime object that includes the function beginningofhour:

rand(10).years.ago.beginning_of_hour #Mon, 09 Nov 2009 23:00:00 UTC +00:00 

Also worth noting that the DateTime object comes with a few more useful methods that could save you some 'time'.

rand(10).years.ago.end_of_day #Sun, 09 Nov 2008 23:59:59 UTC +00:00 
rand(10).years.ago.midday #Thu, 09 Nov 2006 12:00:00 UTC +00:00 

For a full list of methods read the docs - docs

Useful Stuff

Let's start this post with a little confession. I'm not the fastest developer to have walked the Earth.. but I am keen on increasing my productivity as much as possible. To me, it's about saving 30 seconds here or a couple of minutes there that can make all the difference. I've create a list of tools I use to help increase my productivity. Independently they're very simple but you can achieve a lot when you put them together.

  • Sublime Text - I don't think you'll have to go far to find out why Sublime Text is one of the best Editors out there. With an extensive library of Plugins it's been my favorite editor for a couple of years now.
  • If you're going down the Sublime Text route then I would highly recommend installing SublimeLinter. Essentially "The big advantage of using SublimeLinter is that your code can be linted as you type (before saving your changes) and any errors are highlighted immediately". Which can save a huge amount of time. After installing the plugin you can usually find a linter for your language - a quick google will usually display the correct one. Just in case here are the links to Ruby and PHP to get you started.
  • Dropbox. I'm sure everyone knows what it does. As an example though, I created a symbolic link to point at ~/Dropbox/htdocs to keep all my WordPress projects stored in /Applications/MAMP/htdocs synced across machines.
  • Launching a live site can be a daunting task. It's especially important to consider that nothing strange is happening (like missing images or pages). A great tool that can automate this task, without having to go through each page and resource individually is a program called Screaming Frog. It usually takes a minute to produce a report. I'm sure this tool can do a lot more but so far that's all I've used it for.
  • There are so many devices out there it can be difficult to find out if the site you're developing even looks 'okay'. This tool can automate the task and potentially save you a couple of hours Broswer Stack. There's a paid version but I've found the free trial to meet my needs so far.
  • This last one may be a little bit odd but then again you may have a similar experience. Sometimes I get bored looking at the same screen for hours on end even if I'm really enjoying the project I'm working on. I find it hard to look at the screen - my eyes kind of 'tune out'. To get round this problem I find changing the color scheme beneficial. The Color Sublime plugin is great for this.