Perl : unable to frame code with Business:Hours module with example


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : unable to frame code with Business:Hours module with example
# 1  
Old 06-18-2013
Perl : unable to frame code with Business:Hours module with example

Hi all ,

There are two datetimes in the below format

$datetime1="2007-12-31 16:50:00";
$datetime2="2008-1-1 04:24:00";

I need to calculate time in minutes only in between the office hours.(08:00 to 18:00 ). in PERL.

For the above requirement I found a module Business::Hours that calculates the time in seconds after which I can calculate in minutes or hours.

But I am struggling to frame the code using the above module.

Could anyone please explain the module with relevant example?

Thanks in advance....

Code:
  use Business::Hours;
  $hours = Business::Hours->new();    
  # Get a Set::IntSpan of all the business hours in the next week.
  # use the default business hours of 9am to 6pm localtime.
  
  my %hours = (
        0 => { Name     => 'Sunday',
               Start    => '08:00',
               End      => '18:00' },

        1 => { Name     => 'Monday',
               Start    => '08:00',
               End      => '18:00' },
        
        2 => { Name     => 'Tuesday',
               Start    => '08:00',
               End      => '18:00' },

        3 => { Name     => 'Wednesday',
               Start    => '08:00',
               End      => '18:00' },

        4 => { Name     => 'Thursday',
               Start    => '08:00',
               End      => '18:00' },
        
        5 => { Name     => 'Friday',
               Start    => '08:00',
               End      => '18:00' },

        6 => { Name     => 'Saturday',
               Start    => '08:00',
               End      => '18:00' },
        ,
    );  


print $hours->for_timespan( Start => time(), End => time()+(86400*7) );
  
  
  print $hours->{$0};

print $hours->for_timespan(%hours);

link for the above module
Code:
http://search.cpan.org/dist/Business-Hours/lib/Business/Hours.pm#___top

Once again thanks ...
# 2  
Old 06-18-2013
Code:
#!/usr/bin/perl -w
    
use strict;
use Date::Parse;
my $datetime1="2007-12-31 16:50:00";
my $datetime2="2008-1-1 04:24:00";

my $epoch1 = str2time($datetime1);
my $epoch2 = str2time($datetime2);
#print "$epoch1, $epoch2\n";

use Business::Hours;
my $hours = Business::Hours->new();    

my %hours = (
      0 => { Name     => 'Sunday',
             Start    => '08:00',
             End      => '18:00' },

      1 => { Name     => 'Monday',
             Start    => '08:00',
             End      => '18:00' },
      
      2 => { Name     => 'Tuesday',
             Start    => '08:00',
             End      => '18:00' },

      3 => { Name     => 'Wednesday',
             Start    => '08:00',
             End      => '18:00' },

      4 => { Name     => 'Thursday',
             Start    => '08:00',
             End      => '18:00' },
      
      5 => { Name     => 'Friday',
             Start    => '08:00',
             End      => '18:00' },

      6 => { Name     => 'Saturday',
             Start    => '08:00',
             End      => '18:00' },
      ,
  );  

my $diffsec = $hours->between($epoch1,$epoch2);
my $diffmin = $diffsec/60;

printf("Diff is %2.2f min\n", $diffmin);

Code:
$ ./test.pl 
Diff is 70.00 min

This User Gave Thanks to rajamadhavan For This Post:
# 3  
Old 06-18-2013
Tons of thanks rajamadhavan...

It worked...

But Could you please explain me your code and about the module how the time is calculated ..

Once again thanks...
# 4  
Old 06-19-2013
Hi rajamadhavan,

I am not sure where I made the mistake but I am receiving different outputs(wrong output ) in two instances.

Code:
use Business::Hours;
my $datetime1="2013-05-15 12:00";
my $datetime2="2013-05-17 15:00";
my $epoch1 = str2time($datetime1);
my $epoch2 = str2time($datetime2);
my $hours = Business::Hours->new();    
my $diffsec = $hours->between($epoch1,$epoch2);
my $str= strftime("\%H:\%M:\%S", gmtime($diffsec));

print "Hours format : $str ";
print "\n";

prints the exact output : Hours format : 23:00:01


But 

use Business::Hours;
my $datetime1="2013-05-15 12:00";
my $datetime2="2013-05-17 16:00";
my $epoch1 = str2time($datetime1);
my $epoch2 = str2time($datetime2);

my $hours = Business::Hours->new();    
my $diffsec = $hours->between($epoch1,$epoch2);
my $str= strftime("\%H:\%M:\%S", gmtime($diffsec));

print "Hours format : $str ";
print "\n";

prints the wrong output  :   Hours format : 00:00:01 (wrong output, should print 25:00:00 )

# 5  
Old 06-19-2013
why cant you do something like this instead of gmtime. You can make this logic as a subroutine and reuse when needed

Code:
my $hours = int ($diffsec / 3600);
$diffsec = $diffsec % 3600;
my $minutes = int ($diffsec / 60);
$diffsec = $diffsec % 60;
my $seconds = $diffsec;
print "$hours:$minutes:$seconds\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CGI Perl : only frame is getting updated

I am working on CGI perl and trying to overcome the below problem.. I have divided the page into 3 frames( attached the screenshot) how to my frameset look like. In frame2, there will be a cgi script consisting on names(links), when clicked on link , the frame3 should get changed. But in... (1 Reply)
Discussion started by: scriptscript
1 Replies

2. Shell Programming and Scripting

awk : Search for text between two time frame (12 hours)

I have created the script to grep the errors from weblogic logs files and redirecting output to file.txt ...From file.txt I'm using awk command to collect the past 20 mins output...The script running from cron every 15 mins... The script working well... Now the challenges, I'm trying to use... (27 Replies)
Discussion started by: zenkarthi
27 Replies

3. Shell Programming and Scripting

Unable to install module using cpan

Hi, I am unable to install module using cpan. It says 407 Proxy Authentication Required I did setup proxy server and given username and password in .bashrc file and apt.conf file. export http_proxy=http://username:password@proxyserver:portno/ However, still unable to install... (1 Reply)
Discussion started by: Anjan1
1 Replies

4. Infrastructure Monitoring

Nagios - alert during business hours

Is there a way for certain nagios alerts to only alert during business hours? (1 Reply)
Discussion started by: streetfighter2
1 Replies

5. Shell Programming and Scripting

Unable to Install "Devel-Profile" perl module in windows.

Hi, I want to install 'Devel-Profile' in windows but i am not able to install. Here is the error. PPM> install Devel-Profile Install package 'Devel-Profile?' (y/N): y Installing package 'Devel-Profile'... Error installing package 'Devel-Profile': Could not locate a PPD file for... (3 Replies)
Discussion started by: vanitham
3 Replies

6. Shell Programming and Scripting

calling perl subroutine from perl expect module

All, Is it possible to call a subroutine from the perl expect module after logging to a system that is within the same program. My situation is I need to run a logic inside a machine that I'm logging in using the expect module, the logic is also available in the same expect program. Thanks,... (5 Replies)
Discussion started by: arun_maffy
5 Replies

7. OS X (Apple)

Unable to install Perl module via CPAN

Hi, I am trying to install Unicode::String from the cpan shell, and here is what I get: Checking if your kit is complete... Looks good Writing Makefile for Unicode::String cp String.pm blib/lib/Unicode/String.pm cp lib/Unicode/CharName.pm blib/lib/Unicode/CharName.pm... (3 Replies)
Discussion started by: psychomachine
3 Replies

8. Solaris

unable to detect new cpu/memory module

Hi all, I just add in new cpu/memory module into SFV480 and unable to see new module on slot B. I already try many way like only 1 new module and swap the both module and boot up but still unable to see it. If I only install the module into slot B, then the server is unbootable at all. I... (1 Reply)
Discussion started by: mailbox80
1 Replies

9. Solaris

Unable to load module

Hey guys i got this error in my messages: warning : mod_load: cannot load module sbd - I dont know anything about this. - How do i verify if there is a module sbd - What are the reasons modules cant be loaded (1 Reply)
Discussion started by: sbn
1 Replies

10. Shell Programming and Scripting

Replace Perl Module name in all Perl scripts

I want to replace a Perl module name in all my Perl Scripts in the cgi-bin directory. How is it possible? I have the following statement in my scripts use myUtil; I want to change it to use myUtil777; Regards, Rahul (2 Replies)
Discussion started by: rahulrathod
2 Replies
Login or Register to Ask a Question