1 2 3 4 5 6 7
use strict; use warnings; no warnings 'once'; use Date::Calc; use DateTime; my $onlyDate1= DateTime ->now( time_zone => 'Europe/Paris' )->add( days => 7);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
use strict; use POSIX; print "Tag: ".get_time( get_add_day(time, 10) ); sub get_add_day{ my $start_time = shift; my $adddays = shift; foreach(1..$adddays){ $start_time += 24 * 60 * 60; # addiert einen Tag while(!is_week_day($start_time)){ # wenn der Tag kein Werktag ist -> addiere einen Tag $start_time += 24 * 60 * 60; } } return $start_time; } sub is_week_day{ my $Wochentag = (localtime(shift))[6]; if($Wochentag == 0 || $Wochentag == 6){ return 0; } return 1; } sub get_time{ return strftime "%d.%m.%Y %H:%M:%S", localtime(shift); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use strict; use DateTime; use POSIX; my $SAMSTAG = 6; my $SONNTAG = 7; my $add_Geschaeftstage = 7; my $dt = DateTime->now( time_zone => 'Europe/Paris' ); while ($add_Geschaeftstage) { $dt->add( days => 1 ); $add_Geschaeftstage-- unless ( $dt->wday() == $SAMSTAG || $dt->wday() == $SONNTAG ); } print $dt->strftime( "%d.%m.%Y" );
$add_Geschaeftstage-- if ( $dt->wday() != $SAMSTAG and $dt->wday() != $SONNTAG );
$add_Geschaeftstage-- if $dt->wday() ~~ [$MONTAG..$FREITAG];
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#! /usr/bin/perl use strict; use warnings; use DateTime; # Quick-Hacked: extend DateTime to set/check for business days { # default setting: 1-5 (Mo-Fr) are business days my %business_days; @business_days{1..5} = (1)x5; # Method to define business days sub DateTime::set_business_days { my $self = shift; %business_days = (); $business_days{$_} = 1 for @_; } # Method if given wday is a business day sub DateTime::is_business_day { my $self = shift; my $wday = $self->wday; return 1 if $business_days{$wday}; return 0; } } my $day1 = DateTime->now( time_zone => 'Europe/Berlin' ); $day1->set_business_days( 1 .. 5 ); my $add = 7; while ( $add ) { $day1->add( days => 1 ); my $wday = $day1->wday; $add-- if $day1->is_business_day; } print $day1->ymd, $/;
1 2 3 4 5
sub DateTime::set_business_days { my $self = shift; %business_days = (); $business_days{$_} = 1 for @_; }
1 2 3 4 5 6 7 8 9 10
# definiere neue Routine/Methode im Namensraum DateTime sub DateTime::set_business_days { # uebernehme das Objekt, ueber das wir aufgerufen wurden (sogar ueberfluessig hier) my $self = shift; # setze den Hash der Geschaeftstage zurueck, damit keine Altlasten uebrig sind %business_days = (); # setze den Hash neu; fuer jedes gegebene Argument (die stehen in @_) wird im # Hash %business_days ein neues Schlüssel-Wert-Paar erzeugt; Nummer_Wochentag => 1 $business_days{$_} = 1 for @_; }
my $self = shift;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#! /usr/bin/perl use strict; use warnings; use DateTime; # functions to set/check for business days { # set defaults; 1-5 (Mo-Fr) are business days # see: http://search.cpan.org/perldoc?DateTime#$dt-%3Eday_of_week() # for information which number is which day of week my %business_day; @business_day{1..5} = (1)x5; # function to (re)set business days sub set_business_days { %business_day = (); $business_day($_} = 1 for @_; } # check if given day of week is a business day sub is_business_day { my $wday = shift; return 1 if $business_day{$wday}; return 0; } } # or 1..3 for a shorter week; using 1..5 here is redundant, as they are already predefined set_business_days( 1..5 ); my $day1 = DateTime->now( time_zone => 'Europe/Berlin' ); my $add = 7; while ( $add ) { $day1->add( days => 1 ); $add-- if is_business_day( $day1->wday ); } print $day1->ymd, $/;
1 2 3 4
type TExtendedDateTime = class(TDateTime) public function bIsBusinessDay(): Boolean; end;
1 2 3 4 5 6
my $add_Geschaeftstage = 7; while ($add_Geschaeftstage) { $date += 1D; $add_Geschaeftstage-- if $date->is_geschaeftstag; }