[Bps-public-commit] Business-Hours branch, master, updated. 0.09-2-ge911f0d

jesse jesse at bestpractical.com
Mon Sep 28 15:23:57 EDT 2009


The branch, master has been updated
       via  e911f0df022a275d4248c45f8949d600d6b57de6 (commit)
       via  e97221067eff735c7f82195b487ba073821c43e9 (commit)
      from  6102684b48dd770fbca1cffdc8b6b73b8d510423 (commit)

Summary of changes:
 .shipit               |   10 +++++
 Changes               |    3 ++
 MANIFEST              |    1 +
 MANIFEST.SKIP         |   11 ++++++
 lib/Business/Hours.pm |   90 ++++++++++++++++++++++++++++++++++++++++++++++---
 t/1-business-hours.t  |   81 +++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 190 insertions(+), 6 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 .shipit
 create mode 100644 MANIFEST.SKIP

- Log -----------------------------------------------------------------
commit e97221067eff735c7f82195b487ba073821c43e9
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Tue Sep 29 04:21:33 2009 +0900

    Support for multiple business-hour periods per day
    
    Date: Sat, 26 Sep 2009 10:15:40 +0100
    From: Jo?o Miguel Neves <joao.neves at intraneia.com>
    To: Jesse Vincent <jesse at cpan.org>, Jesse Vincent <jesse at fsck.com>
    Subject: [Fwd: Re: [Business::Hours] Patch for multiple business hour
    periods per day]
    
    [-- Attachment #1 --]

diff --git a/Changes b/Changes
index e66da67..1e0cb3c 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 Revision history for Perl module Business::Hours
 
+  * Add support for "breaks" during business hours -- 
+		Joao Miguel Neves <joao.neves at intraneia.com>
+
 0.09 Fri Nov 28 01:46:33 MSK 2008
   * add support for holidays
 
diff --git a/lib/Business/Hours.pm b/lib/Business/Hours.pm
index 4f70200..df8feae 100644
--- a/lib/Business/Hours.pm
+++ b/lib/Business/Hours.pm
@@ -7,7 +7,7 @@ require 5.006;
 use Set::IntSpan;
 use Time::Local qw/timelocal_nocheck/;
 
-our $VERSION = 0.09;
+our $VERSION = '0.09_01';
 
 =head1 NAME
 
@@ -112,6 +112,29 @@ Start => '9:00', End => '18:00'.  A given day MUST have a start
 and end time OR may declare both Start and End to be undef, if
 there are no valid hours on that day.
 
+You can use the array Breaks to mark interruptions between Start/End (for instance lunch hour). It's an array of periods, each with a Start and End time:
+
+    my %hours = (
+        0 => { Name     => 'Sunday',
+               Start    => 'HH:MM',
+               End      => 'HH:MM',
+               Breaks  => [ 
+                             { Start    => 'HH:MM',
+                             End      => 'HH:MM' },
+                             { Start    => 'HH:MM',
+                             End      => 'HH:MM' },
+                           ],
+
+        1 => { Name     => 'Monday',
+               Start    => 'HH:MM',
+               End      => 'HH:MM' },
+        ....
+
+        6 => { Name     => 'Saturday',
+               Start    => 'HH:MM',
+               End      => 'HH:MM' },
+    );
+
 Note that the ending time is really "what is the first minute we're closed.
 If you specifiy an "End" of 18:00, that means that at 6pm, you are closed.
 The last business second was 17:59:59.
@@ -202,6 +225,7 @@ sub for_timespan {
 
     # Split the Start and End times into hour/minute specifications
     foreach my $day ( keys %$bizdays ) {
+	# Kept for (retro)compatibility
         my $day_href = $bizdays->{$day};
         foreach my $which qw(Start End) {
             if (   $day_href->{$which}
@@ -211,6 +235,23 @@ sub for_timespan {
                 $day_href->{ $which . 'Minute' } = $2;
             }
         }
+	# Processing each period
+	if ($bizdays->{$day}->{'Breaks'}) {
+	    my $i = 0;
+	    for ($i = 0; $i < @{$bizdays->{$day}->{'Breaks'}}; $i++) {
+		if ($bizdays->{$day}->{'Breaks'}[$i]) {
+		    my $day_href = $bizdays->{$day}->{'Breaks'}[$i];
+		    foreach my $which qw(Start End) {
+			if (   $day_href->{$which}
+			       && $day_href->{$which} =~ /^(\d+)\D(\d+)$/ )
+			{
+			    $day_href->{ $which . 'Hour' }   = $1;
+			    $day_href->{ $which . 'Minute' } = $2;
+			}
+		    }
+		}
+	    }
+	}
     }
 
     # now that we know what the business hours are for each day in a week,
@@ -241,6 +282,12 @@ sub for_timespan {
     # (This is fed into Set::IntSpan to use to compute our actual run.
     my @run_list;
 
+    # @break_list is a run list of the period's breaks between business hours
+    # its form is (<int>-<int2>,<int3>-<int4>)
+    # For documentation about its format, have a look at Set::IntSpan.
+    # (This is fed into Set::IntSpan to use to compute our actual run.
+    my @break_list;
+
     while ( $week_start <= $args{'End'} ) {
 
         my @this_week_start = localtime($week_start);
@@ -274,11 +321,44 @@ sub for_timespan {
                 # We subtract 1 from the ending time, because the ending time
                 # really specifies what hour we end up closed at
                 $day_bizhours_end--;
-
+		
                 push( @run_list, "$day_bizhours_start-$day_bizhours_end" );
-
+		
             }
-
+	    
+	    if ($bizdays->{$dow}->{'Breaks'}) {
+		my $i = 0;
+		for ($i = 0; $i < @{$bizdays->{$dow}->{'Breaks'}}; $i++) {
+		    my $day_hours = $bizdays->{$dow}->{'Breaks'}[$i];
+		    if ( $day_hours->{'Start'} && $day_hours->{'End'} ) {
+			
+			# add the business seconds in that week to the runlist we'll use to
+			# figure out business hours
+			# (Be careful to use timelocal to convert times in the week into actual
+			# seconds, so we don't lose at DST transition)
+			my $day_bizhours_start = timelocal_nocheck(
+			    0,
+			    $day_hours->{'StartMinute'},
+                            $day_hours->{'StartHour'},
+                            ( $this_week_start[3] + $dow ),
+                            $this_week_start[4],
+                            $this_week_start[5]
+                        );
+
+                        my $day_bizhours_end = timelocal_nocheck(
+                            0, $day_hours->{'EndMinute'},
+                            $day_hours->{'EndHour'}, ( $this_week_start[3] + $dow ),
+                            $this_week_start[4], $this_week_start[5]
+                        );
+
+                        # We subtract 1 from the ending time, because the ending time
+                        # really specifies what hour we end up closed at
+                        $day_bizhours_end--;
+
+			push( @break_list, "$day_bizhours_start-$day_bizhours_end" );
+		    }
+                }
+	    }
         }
 
     # now that we're done with this week, calculate the start of the next week
@@ -289,7 +369,7 @@ sub for_timespan {
 
     }
 
-    my $business_hours = Set::IntSpan->new( join( ',', @run_list ) );
+    my $business_hours = Set::IntSpan->new( join( ',', @run_list ) ) - Set::IntSpan->new( join( ',', @break_list ) );
     my $business_hours_in_period
         = $business_hours->intersect($business_period);
 
diff --git a/t/1-business-hours.t b/t/1-business-hours.t
index c8f100e..6062d57 100644
--- a/t/1-business-hours.t
+++ b/t/1-business-hours.t
@@ -1,6 +1,6 @@
 use strict;
 use warnings;
-use Test::More tests => 14;
+use Test::More tests => 17;
 
 BEGIN { use_ok 'Business::Hours' }
 
@@ -87,3 +87,82 @@ BEGIN { use_ok 'Business::Hours' }
     is(cardinality $span, (30 * 60)+1);
 }
 
+{
+    my %BUSINESS_HOURS = (
+			  0 => {
+			      Name  => 'Sunday',
+			      Start => undef,
+			      End   => undef,
+			  },
+			  1 => {
+			      Name  => 'Monday',	
+			      Start => '9:00',
+			      End   => '18:00',
+			      Breaks => [
+					 {
+					     Start => '13:00',
+					     End   => '14:00',
+					 },
+					 ],
+			  },
+			  2 => {
+			      Name  => 'Tuesday', 
+			      Start => '9:00',
+			      End   => '18:00',
+			      Breaks => [
+					 {
+					     Start => '13:00',
+					     End   => '14:00',
+					 },
+					 ],
+			  },
+			  3 => {
+			      Name  => 'Wednesday',
+			      Start => '9:00',
+			      End   => '18:00',
+			      Breaks => [
+					 {
+					     Start => '13:00',
+					     End   => '14:00',
+					 },
+					 ],
+			  },
+			  4 => {
+			      Name  => 'Thursday',
+			      Start => '9:00',
+			      End   => '18:00',
+			      Breaks => [
+					 {
+					     Start => '13:00',
+					     End   => '14:00',
+					 },
+					 ],
+			  },
+			  5 => {
+			      Name  => 'Friday',
+			      Start => '9:00',
+			      End   => '18:00',
+			      Breaks => [
+					 {
+					     Start => '13:00',
+					     End   => '14:00',
+					 },
+					 ],
+			  },
+			  6 => {
+			      Name  => 'Saturday',
+			      Start => undef,
+			      End   => undef,
+			  });
+    my $hours = Business::Hours->new();
+    $hours->business_hours(%BUSINESS_HOURS);
+    is(ref($hours), 'Business::Hours');
+    # how many business hours were there in the first week.
+    my $hours_span = $hours->for_timespan(Start => '0', End => ( (86400 * 7) - 1));
+    is(ref($hours_span), 'Set::IntSpan');
+
+    # Are there 40 working hours
+
+    is(cardinality $hours_span, (40 * 60 * 60));
+}
+

commit e911f0df022a275d4248c45f8949d600d6b57de6
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Tue Sep 29 04:23:52 2009 +0900

    releng work

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e69de29
diff --git a/.shipit b/.shipit
new file mode 100644
index 0000000..44c02c1
--- /dev/null
+++ b/.shipit
@@ -0,0 +1,10 @@
+# auto-generated shipit config file.
+steps = FindVersion, ChangeVersion, CheckChangeLog, DistTest, Commit, Tag, MakeDist, UploadCPAN, Twitter
+
+git.tagpattern = %v
+twitter.config = ~/.twitterrc
+
+# svn.tagpattern = MyProj-%v
+# svn.tagpattern = http://code.example.com/svn/tags/MyProj-%v
+
+# CheckChangeLog.files = ChangeLog, MyProj.CHANGES
diff --git a/MANIFEST b/MANIFEST
index 3479432..2a29547 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -3,6 +3,7 @@ lib/Business/Hours.pm
 LICENSE
 Makefile.PL
 MANIFEST
+MANIFEST.SKIP
 META.yml
 README
 SIGNATURE
diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP
new file mode 100644
index 0000000..7fca686
--- /dev/null
+++ b/MANIFEST.SKIP
@@ -0,0 +1,11 @@
+TODO
+^Makefile$
+blib
+pm_to_blib
+.swp$
+~$
+.tmp$
+.bak$
+.git/
+.gitignore$
+.shipit$

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list