rounding time in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rounding time in perl
# 1  
Old 02-10-2010
rounding time in perl

Is there a way I can round time in perl to the nearest five minutes?

For example if I have log giving the following time stamps

23,52,30 it would rounded up to 23,55,00

and

23,50,01 would be rounded to 23,50,00
# 2  
Old 02-10-2010
implement manually

I think this is the algorithm problem rather than the lagnauge.
What about implementing it manually.

Code:
#!/usr/bin/perl
use 5.010;

use warnings;
use strict;

sub round_by_five {
    my ($hour, $min, $sec) = @_;

    my $tot = $min * 60 + $sec;
    my $rem = $tot % 300;
    return (
        $hour,
        $rem >= 150 ? ($tot+300-$rem)/60 : ($tot-$rem)/60,
        0
    );
}

say join ",", round_by_five(23, 52, 30);
say join ",", round_by_five(23, 50, 01);

5 minutes are 300 seconds.
so, if minute + second is equal to or larger than 150 seconds, it should be ceil to 5min, otherwise should be floor to 5min.


ps) there are ceil() and floor() functions in POSIX module.
# 3  
Old 02-11-2010
hi hojung-yoon,

I really like your solution. I have tested it though and there is a problem that for example 07, 59, 59 gets rounded to 07, 60 ,00. Is there a way I can handle this so that it rounds to 08,00,00?

Many thanks

Last edited by borderblaster; 02-11-2010 at 01:03 PM..
# 4  
Old 02-11-2010
Code:
use 5.010;

use warnings;
use strict;

sub round_by_five {
    my ($hour, $min, $sec) = @_;

    my $tot = $min * 60 + $sec;
    my $rem = $tot % 300;

    my $new_min = $rem >= 150 ? ($tot+300-$rem)/60 : ($tot-$rem)/60 ;

    return $new_min == 60
        ? ($hour+1, 0, 0)
        : ($hour, $new_min, 0)
        ;
}

say join ",", round_by_five(23, 52, 30);
say join ",", round_by_five(23, 50, 01);
say join ",", round_by_five( 7, 59, 59);
say join ",", round_by_five( 7,  1, 30);

Also note that 0-prefixed numbers are octal(8-based).
So 08 is wrong number that it will emit an error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Rounding off a decimal

How to round off a decimal number to higher whole number using ceil command in unix? Eg. 4.41 or 4.11 or 4.51 should be rounded off to 5. (11 Replies)
Discussion started by: SanjayKumar28
11 Replies

2. Shell Programming and Scripting

printf (awk,perl,shell) float rounding issue

Hi guys, could someone throw some light on the following behaviour of printf (I'll start with info about the system and the tool/shell/interpreter versions)?: $ uname -a Linux linux-86if.site 3.1.0-1.2-desktop #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) x86_64 x86_64 x86_64... (9 Replies)
Discussion started by: elixir_sinari
9 Replies

3. Shell Programming and Scripting

Help required between "wall time" and Elapsed time in perl.

Hi, I have having a great confusion in under standing the below terms. a) "wall time" (time according the the clock on the wall vs. cpu time spent executing it) what exactly it means? For example: wall time cpu time line:text 4.22404 24.186 if(some junk condition) ... (1 Reply)
Discussion started by: vanitham
1 Replies

4. Shell Programming and Scripting

Rounding number, but....

Dear Experts, I'm trying to find a way to round a number but in this way: 14367.577 ---> 14000 I used the following to round the number to the closer integer: echo $var|awk '{print int($1+0.5)}' and also: xargs printf "%1.0f" However, they don't work for my above... (9 Replies)
Discussion started by: Gery
9 Replies

5. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

6. Shell Programming and Scripting

PERL - rounding fractional number

It seems that perl sprintf uses the round-to-even method: foreach my $i ( 0.5, 1.5, 2.5, 3.5 ) { printf "$i -> %.0f\n", $i; } __END__ 0.5 -> 0 1.5 -> 2 2.5 -> 2 3.5 -> 4 4.5 -> 4 Where we probably wants to use round-half-up, i.e. output should be as below: 0.5 -> 1 1.5 -> 2... (8 Replies)
Discussion started by: ganapati
8 Replies

7. Linux

Rounding Script Help

I need some help with my rouding script. I have started pretty much from scratch and have no idea if its correct or even close but I have been trying and have gotten to this point. i keep getting syntax errors and im not sure what is wrong. Here is what I got let value=$1; while do let... (0 Replies)
Discussion started by: kingrj46
0 Replies

8. Shell Programming and Scripting

Rounding off decimals to the nearest number in PERL

Hi Guys, I am generating a statistical report , below is the snippet of the code : Now, $nSlices stands for the time duration,meaning,the statistics will be displayed for that particular time duration. Trouble is, for certain values of $totalTime (which is the end time - start time ), i... (9 Replies)
Discussion started by: rdlover
9 Replies

9. UNIX for Dummies Questions & Answers

Rounding problem

Hi, Can any one help me in finding a solution for rounding off to 2 decimal places. I am using the following code: VAR1=.01292105263157894736 VAR2=`echo "scale=2; $VAR1 * 100" | bc -l` The result I 'm getting is 1.29210526315789473600 But I need the output as 1.29 Thanks Shash (2 Replies)
Discussion started by: shash
2 Replies

10. Shell Programming and Scripting

Rounding off using BC.

Hello again. I'm trying to use BC to calculate some numbers in a shell script. I want to have the numbers rounded off to 1 decimal place. for example: initsize=1566720 zipsize=4733 I'm trying to get the ratio between them. the equation is: (($initsize-$zipsize)/$initsize)*100 so... (3 Replies)
Discussion started by: noodlesoup
3 Replies
Login or Register to Ask a Question