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 rounding 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.
Got something to add? Send me email.
More Articles by Anthony Lawrence © 2012-07-11 Anthony Lawrence