# # Rounding time
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Rounding time

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© November 2005 Anthony Lawrence

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.


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Rounding time


Inexpensive and informative Apple related e-books:

Take Control of IOS 11

Photos for Mac: A Take Control Crash Course

Take Control of Automating Your Mac

Take Control of OS X Server

Take Control of Pages





More Articles by © Anthony Lawrence





Printer Friendly Version

Have you tried Searching this site?

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.

Contact us


Printer Friendly Version





A Netscape engineer who shan't be named once passed a pointer to JavaScript, stored it as a string and later passed it back to C, killing 30. (Blake Ross)




Linux posts

Troubleshooting posts


This post tagged:

Perl

Programming



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode