X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/646cb2aa3a79d7b35a1673fc67cdf802e9cba515..refs/heads/b-3.3.0:/SL/Helper/DateTime.pm diff --git a/SL/Helper/DateTime.pm b/SL/Helper/DateTime.pm index 94c311f14..138a8f3f4 100644 --- a/SL/Helper/DateTime.pm +++ b/SL/Helper/DateTime.pm @@ -4,6 +4,11 @@ use strict; use SL::Util qw(_hashify); +sub new_local { + my ($class, %params) = @_; + return $class->new(hour => 0, minute => 0, second => 0, time_zone => $::locale->get_local_time_zone, %params); +} + sub now_local { return shift->now(time_zone => $::locale->get_local_time_zone); } @@ -36,6 +41,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__ @@ -50,6 +91,11 @@ SL::Helpers::DateTime - helper functions for L =over 4 +=item C + +Returns the time given in C<%params> with the time zone set to the +local time zone. + =item C Returns the current time with the time zone set to the local time zone.