From 81b492ac44ad8fc5207a9072039b972fb954c4dd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Wed, 25 Jun 2014 16:20:01 +0200 Subject: [PATCH] DateTime: (add|subtract)_businessdays Funktionen --- SL/Helper/DateTime.pm | 36 +++++++++++++++++ t/helper/datetime.t | 89 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 t/helper/datetime.t diff --git a/SL/Helper/DateTime.pm b/SL/Helper/DateTime.pm index 94c311f14..c79dad1b8 100644 --- a/SL/Helper/DateTime.pm +++ b/SL/Helper/DateTime.pm @@ -36,6 +36,42 @@ sub from_lxoffice { goto &from_kivitendo; } +sub add_business_duration { + my ($self, %params) = @_; + + my $abs_days = abs $params{days}; + my $neg = $params{days} < 0; + my $bweek = $params{businessweek} || 5; + my $weeks = int ($abs_days / $bweek); + my $days = $abs_days % $bweek; + + if ($neg) { + $self->subtract(weeks => $weeks); + $self->add(days => 8 - $self->day_of_week) if $self->day_of_week > $bweek; + $self->subtract(days => $self->day_of_week > $days ? $days : $days + (7 - $bweek)); + } else { + $self->add(weeks => $weeks); + $self->subtract(days => $self->day_of_week - $bweek) if $self->day_of_week > $bweek; + $self->add(days => $self->day_of_week + $days <= $bweek ? $days : $days + (7 - $bweek)); + } + + $self; +} + +sub add_businessdays { + my ($self, %params) = @_; + + $self->add_business_duration(%params); +} + +sub subtract_businessdays { + my ($self, %params) = @_; + + $params{days} *= -1; + + $self->add_business_duration(%params); +} + 1; __END__ diff --git a/t/helper/datetime.t b/t/helper/datetime.t new file mode 100644 index 000000000..afaa49e84 --- /dev/null +++ b/t/helper/datetime.t @@ -0,0 +1,89 @@ +use Test::More tests => 50; + +use lib 't'; + +use Data::Dumper; + +use DateTime; +use_ok 'SL::Helper::DateTime'; + +sub mon { DateTime->new(year => 2014, month => 6, day => 23) } +sub tue { DateTime->new(year => 2014, month => 6, day => 24) } +sub wed { DateTime->new(year => 2014, month => 6, day => 25) } +sub thu { DateTime->new(year => 2014, month => 6, day => 26) } +sub fri { DateTime->new(year => 2014, month => 6, day => 27) } +sub sat { DateTime->new(year => 2014, month => 6, day => 28) } +sub sun { DateTime->new(year => 2014, month => 6, day => 29) } + + +is mon->add_businessdays(days => 5)->day_of_week, 1, "mon + 5 => mon"; +is mon->add_businessdays(days => 12)->day_of_week, 3, "mon + 12 => wed"; +is fri->add_businessdays(days => 2)->day_of_week, 2, "fri + 2 => tue"; +is tue->add_businessdays(days => 9)->day_of_week, 1, "tue + 9 => mon"; +is tue->add_businessdays(days => 8)->day_of_week, 5, "tue + 8 => fri"; + +# same with 6day week +is mon->add_businessdays(businessweek => 6, days => 5)->day_of_week, 6, "mon + 5 => sat (6dw)"; +is mon->add_businessdays(businessweek => 6, days => 12)->day_of_week, 1, "mon + 12 => mon (6dw)"; +is fri->add_businessdays(businessweek => 6, days => 2)->day_of_week, 1, "fri + 2 => mon (6dw)"; +is tue->add_businessdays(businessweek => 6, days => 9)->day_of_week, 5, "tue + 9 => fri (6dw)"; +is tue->add_businessdays(businessweek => 6, days => 8)->day_of_week, 4, "tue + 8 => thu (6dw)"; + +# absolute dates + +is mon->add_businessdays(days => 5), mon->add(days => 7), "mon + 5 => mon (date)"; +is mon->add_businessdays(days => 12), mon->add(days => 16), "mon + 12 => wed (date)"; +is fri->add_businessdays(days => 2), fri->add(days => 4), "fri + 2 => tue (date)"; +is tue->add_businessdays(days => 9), tue->add(days => 13), "tue + 9 => mon (date)"; +is tue->add_businessdays(days => 8), tue->add(days => 10), "tue + 8 => fri (date)"; + +# same with 6day week +is mon->add_businessdays(businessweek => 6, days => 5), mon->add(days => 5), "mon + 5 => sat (date) (6dw)"; +is mon->add_businessdays(businessweek => 6, days => 12), mon->add(days => 14), "mon + 12 => mon (date) (6dw)"; +is fri->add_businessdays(businessweek => 6, days => 2), fri->add(days => 3), "fri + 2 => mon (date) (6dw)"; +is tue->add_businessdays(businessweek => 6, days => 9), tue->add(days => 10), "tue + 9 => fri (date) (6dw)"; +is tue->add_businessdays(businessweek => 6, days => 8), tue->add(days => 9), "tue + 8 => thu (date) (6dw)"; + + +# same with substract + +is mon->subtract_businessdays(days => 5)->day_of_week, 1, "mon - 5 => mon"; +is mon->subtract_businessdays(days => 12)->day_of_week, 4, "mon - 12 => thu"; +is fri->subtract_businessdays(days => 2)->day_of_week, 3, "fri - 2 => wed"; +is tue->subtract_businessdays(days => 9)->day_of_week, 3, "tue - 9 => wed"; +is tue->subtract_businessdays(days => 8)->day_of_week, 4, "tue - 8 => thu"; + +# same with 6day week +is mon->subtract_businessdays(businessweek => 6, days => 5)->day_of_week, 2, "mon - 5 => tue (6dw)"; +is mon->subtract_businessdays(businessweek => 6, days => 12)->day_of_week, 1, "mon - 12 => mon (6dw)"; +is fri->subtract_businessdays(businessweek => 6, days => 4)->day_of_week, 1, "fri - 4 => mon (6dw)"; +is tue->subtract_businessdays(businessweek => 6, days => 9)->day_of_week, 5, "tue - 9 => fri (6dw)"; +is tue->subtract_businessdays(businessweek => 6, days => 8)->day_of_week, 6, "tue - 8 => sat (6dw)"; + +# absolute dates + +is mon->subtract_businessdays(days => 5), mon->add(days => -7), "mon - 5 => mon (date)"; +is mon->subtract_businessdays(days => 12), mon->add(days => -18), "mon - 12 => thu (date)"; +is fri->subtract_businessdays(days => 2), fri->add(days => -2), "fri - 2 => wed (date)"; +is tue->subtract_businessdays(days => 9), tue->add(days => -13), "tue - 9 => wed (date)"; +is tue->subtract_businessdays(days => 8), tue->add(days => -12), "tue - 8 => thu (date)"; + +# same with 6day week +is mon->subtract_businessdays(businessweek => 6, days => 5), mon->add(days => -6), "mon - 5 => tue (date) (6dw)"; +is mon->subtract_businessdays(businessweek => 6, days => 12), mon->add(days => -14), "mon - 12 => mon (date) (6dw)"; +is fri->subtract_businessdays(businessweek => 6, days => 4), fri->add(days => -4), "fri - 4 => mon (date) (6dw)"; +is tue->subtract_businessdays(businessweek => 6, days => 9), tue->add(days => -11), "tue - 9 => fri (date) (6dw)"; +is tue->subtract_businessdays(businessweek => 6, days => 8), tue->add(days => -10), "tue - 8 => sat (date) (6dw)"; + +# add with negative days? +is mon->add_businessdays(businessweek => 6, days => -5), mon->add(days => -6), "mon - 5 => tue (date) (6dw)"; +is mon->add_businessdays(businessweek => 6, days => -12), mon->add(days => -14), "mon - 12 => mon (date) (6dw)"; +is fri->add_businessdays(businessweek => 6, days => -4), fri->add(days => -4), "fri - 4 => mon (date) (6dw)"; +is tue->add_businessdays(businessweek => 6, days => -9), tue->add(days => -11), "tue - 9 => fri (date) (6dw)"; +is tue->add_businessdays(businessweek => 6, days => -8), tue->add(days => -10), "tue - 8 => sat (date) (6dw)"; + +# what if staring date falls into eekend? +is sun->add_businessdays(days => 1), sun->add(days => 1), "1 day after sun is mon"; +is sat->add_businessdays(days => 1), sat->add(days => 2), "1 day after sut is mon"; +is sun->add_businessdays(days => -1), sun->add(days => -2), "1 day before sun is fri"; +is sat->add_businessdays(days => -1), sat->add(days => -1), "1 day before sut is fri"; -- 2.20.1