It was a long, hard day. Up early to clean a room of furniture so that new carpet can be put in later this week, off to a real estate refinancing closing after that, back to rake leaves and then the usual computer type work. After dinner some refinishing of hardwood floors and a little sealing of cracks noticed when I pulled up some of the old carpet. Before I went to bed, I checked email and noticed something from a customer needing some help with rounding off time.
Specifically, he had a string that might look like "DH154932" which represents 3:49 PM and some seconds. He needed that rounded off to the nearest previous 15 minutes. I looked at it, thought "that's not too hard", and then realized that my head was about to fall onto the keyboard, so I went to bed. As I drifted off I thought "floor() can do that", but I think my brain was confusing refinishing the living room floor with rouding time, because it can't. But it is easy enough, and when I woke up, I dashed off an email explaining how to do it.
The easiest way to handle this is to extract the minutes portion from the string and round that down. But how do you round down to the previous fifteen minutes? Well, you could do it the long way:
$min=45 if $min > 45; $min=30 if $min > 30; $min=15 if $min > 15; $min=0 if $min > 0;
And unless you have to process thousands of these each second, there's nothing wrong with that. It's brute force, but so what? Far too often programmers employ "clever" solutions when there is really no reason to. A straightforward method like this is easy to understand. It may not be highly efficient, but if that doesn't matter, why not use it? You'll never be confused by that code, and if you have to modify it you'll have little danger of screwing it up.
But I don't know how much processing is going on here, so I gave him a different method. It's probably faster, though I didn't actually test it and that's another important point to remember: just because it looks faster doesn't mean it is. If you really need efficiency, you have to test. The four "if" statements above might actually run faster than a the single statement I used. Probably not in this case; what I used was
$min=$min - $min % 15;
which is quite direct and should compile into something pretty fast. But if I was really worried, I'd set up tests.
So, the whole thing looks something like this:
#!/usr/bin/perl
# this.pl DH154951
$time=shift @ARGV;
$min=substr($time,4,2);
print "$time\n";
$min=$min - $min % 15;
$time=sprintf("%s%0.4d",substr($time,0,4),$min * 100);
print "$time\n";
As my tired brain thought, it wasn't very hard.
More Articles by Anthony Lawrence - Find me on Google+
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar