Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

Rounding Time

A.P. Lawrence
Expert Author
Published: 2006-01-03

WebProNews RSS Feed


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 "$timen";
$min=$min - $min % 15;
$time=sprintf("%s%0.4d",substr($time,0,4),$min * 100);
print "$timen";


As my tired brain thought, it wasn't very hard.

*Originally published at APLawrence.com

Receive Our Daily Email of Breaking eBusiness News


About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

WebProNews RSS Feed

More Expert Articles Articles

Contact WebProNews
Advertisement





TOP NEWS

Targeted Information for Business
WebProNews is part of the iEntry network

Internet Business: Marketing: Small Business:
WebProNews MarketingNewz SmallBusinessNewz
WebProWorld AdvertisingDay PromoteNews
EcommNewz SalesNewz EntrepreneurNewz

Software: Search Engines: Web Design:
WebMasterFree Jayde B2B DesignNewz
NetworkingFiles SearchZA FlashNewz
SecurityConfig SearchNewz WebSiteNotes

Developer: IT Management: Security:
DevWebPro ITManagement SecurityProNews
DevNewz SysAdminNews SecurityConfig
TheDevWeb NetworkingFiles NetworkNewz

The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.
eBUSINESS RESOURCES






 Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
 WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
About WebProNews
WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

WebProNews provides real-time coverage of internet business.

Free Email Newsletters:
WebProNews SearchNewz
WebProWorld DevWebPro
Marketing SecurityNews
Plus over 100 other newsletters!

Send me relevant info on products and services.


WebProWorld
Ten most recent posts.

NetworkingFiles
Featured Software

WebProNews in the News
View all recent mentions of WebProNews from around the world!

Recent Articles On ...
Google eBusiness
Yahoo Ask Jeeves
MSN Blogs
Search Engines Blogging
Affiliate Programs Marketing
eCommerce Advertising
eBay Sun Microsystems
AOL Adsense
Microsoft Adwords
Oracle IBM
Amazon Apple
SEM Mac
SEO iPod
Adsense XBox
PR Adobe



iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us