Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 81b492ac

Von Sven Schöling vor mehr als 10 Jahren hinzugefügt

  • ID 81b492ac44ad8fc5207a9072039b972fb954c4dd
  • Vorgänger a607a2d0
  • Nachfolger b6213d35

DateTime: (add|subtract)_businessdays Funktionen

Unterschiede anzeigen:

SL/Helper/DateTime.pm
36 36
  goto &from_kivitendo;
37 37
}
38 38

  
39
sub add_business_duration {
40
  my ($self, %params) = @_;
41

  
42
  my $abs_days = abs $params{days};
43
  my $neg      = $params{days} < 0;
44
  my $bweek    = $params{businessweek} || 5;
45
  my $weeks    = int ($abs_days / $bweek);
46
  my $days     = $abs_days % $bweek;
47

  
48
  if ($neg) {
49
    $self->subtract(weeks => $weeks);
50
    $self->add(days => 8 - $self->day_of_week) if $self->day_of_week > $bweek;
51
    $self->subtract(days => $self->day_of_week > $days ? $days : $days + (7 - $bweek));
52
  } else {
53
    $self->add(weeks => $weeks);
54
    $self->subtract(days => $self->day_of_week - $bweek) if $self->day_of_week > $bweek;
55
    $self->add(days => $self->day_of_week + $days <= $bweek ? $days : $days + (7 - $bweek));
56
  }
57

  
58
  $self;
59
}
60

  
61
sub add_businessdays {
62
  my ($self, %params) = @_;
63

  
64
  $self->add_business_duration(%params);
65
}
66

  
67
sub subtract_businessdays {
68
  my ($self, %params) = @_;
69

  
70
  $params{days} *= -1;
71

  
72
  $self->add_business_duration(%params);
73
}
74

  
39 75
1;
40 76

  
41 77
__END__
t/helper/datetime.t
1
use Test::More tests => 50;
2

  
3
use lib 't';
4

  
5
use Data::Dumper;
6

  
7
use DateTime;
8
use_ok 'SL::Helper::DateTime';
9

  
10
sub mon { DateTime->new(year => 2014, month => 6, day => 23) }
11
sub tue { DateTime->new(year => 2014, month => 6, day => 24) }
12
sub wed { DateTime->new(year => 2014, month => 6, day => 25) }
13
sub thu { DateTime->new(year => 2014, month => 6, day => 26) }
14
sub fri { DateTime->new(year => 2014, month => 6, day => 27) }
15
sub sat { DateTime->new(year => 2014, month => 6, day => 28) }
16
sub sun { DateTime->new(year => 2014, month => 6, day => 29) }
17

  
18

  
19
is mon->add_businessdays(days => 5)->day_of_week, 1, "mon + 5 => mon";
20
is mon->add_businessdays(days => 12)->day_of_week, 3, "mon + 12 => wed";
21
is fri->add_businessdays(days => 2)->day_of_week, 2, "fri + 2 => tue";
22
is tue->add_businessdays(days => 9)->day_of_week, 1, "tue + 9 => mon";
23
is tue->add_businessdays(days => 8)->day_of_week, 5, "tue + 8 => fri";
24

  
25
# same with 6day week
26
is mon->add_businessdays(businessweek => 6, days => 5)->day_of_week, 6, "mon + 5 => sat (6dw)";
27
is mon->add_businessdays(businessweek => 6, days => 12)->day_of_week, 1, "mon + 12 => mon (6dw)";
28
is fri->add_businessdays(businessweek => 6, days => 2)->day_of_week, 1, "fri + 2 => mon (6dw)";
29
is tue->add_businessdays(businessweek => 6, days => 9)->day_of_week, 5, "tue + 9 => fri (6dw)";
30
is tue->add_businessdays(businessweek => 6, days => 8)->day_of_week, 4, "tue + 8 => thu (6dw)";
31

  
32
# absolute dates
33

  
34
is mon->add_businessdays(days => 5), mon->add(days => 7), "mon + 5 => mon (date)";
35
is mon->add_businessdays(days => 12), mon->add(days => 16), "mon + 12 => wed (date)";
36
is fri->add_businessdays(days => 2), fri->add(days => 4), "fri + 2 => tue (date)";
37
is tue->add_businessdays(days => 9), tue->add(days => 13), "tue + 9 => mon (date)";
38
is tue->add_businessdays(days => 8), tue->add(days => 10), "tue + 8 => fri (date)";
39

  
40
# same with 6day week
41
is mon->add_businessdays(businessweek => 6, days => 5), mon->add(days => 5), "mon + 5 => sat (date) (6dw)";
42
is mon->add_businessdays(businessweek => 6, days => 12), mon->add(days => 14), "mon + 12 => mon (date) (6dw)";
43
is fri->add_businessdays(businessweek => 6, days => 2), fri->add(days => 3), "fri + 2 => mon (date) (6dw)";
44
is tue->add_businessdays(businessweek => 6, days => 9), tue->add(days => 10), "tue + 9 => fri (date) (6dw)";
45
is tue->add_businessdays(businessweek => 6, days => 8), tue->add(days => 9), "tue + 8 => thu (date) (6dw)";
46

  
47

  
48
# same with substract
49

  
50
is mon->subtract_businessdays(days => 5)->day_of_week, 1, "mon - 5 => mon";
51
is mon->subtract_businessdays(days => 12)->day_of_week, 4, "mon - 12 => thu";
52
is fri->subtract_businessdays(days => 2)->day_of_week, 3, "fri - 2 => wed";
53
is tue->subtract_businessdays(days => 9)->day_of_week, 3, "tue - 9 => wed";
54
is tue->subtract_businessdays(days => 8)->day_of_week, 4, "tue - 8 => thu";
55

  
56
# same with 6day week
57
is mon->subtract_businessdays(businessweek => 6, days => 5)->day_of_week, 2, "mon - 5 => tue (6dw)";
58
is mon->subtract_businessdays(businessweek => 6, days => 12)->day_of_week, 1, "mon - 12 => mon (6dw)";
59
is fri->subtract_businessdays(businessweek => 6, days => 4)->day_of_week, 1, "fri - 4 => mon (6dw)";
60
is tue->subtract_businessdays(businessweek => 6, days => 9)->day_of_week, 5, "tue - 9 => fri (6dw)";
61
is tue->subtract_businessdays(businessweek => 6, days => 8)->day_of_week, 6, "tue - 8 => sat (6dw)";
62

  
63
# absolute dates
64

  
65
is mon->subtract_businessdays(days => 5), mon->add(days => -7), "mon - 5 => mon (date)";
66
is mon->subtract_businessdays(days => 12), mon->add(days => -18), "mon - 12 => thu (date)";
67
is fri->subtract_businessdays(days => 2), fri->add(days => -2), "fri - 2 => wed (date)";
68
is tue->subtract_businessdays(days => 9), tue->add(days => -13), "tue - 9 => wed (date)";
69
is tue->subtract_businessdays(days => 8), tue->add(days => -12), "tue - 8 => thu (date)";
70

  
71
# same with 6day week
72
is mon->subtract_businessdays(businessweek => 6, days => 5), mon->add(days => -6), "mon - 5 => tue (date) (6dw)";
73
is mon->subtract_businessdays(businessweek => 6, days => 12), mon->add(days => -14), "mon - 12 => mon (date) (6dw)";
74
is fri->subtract_businessdays(businessweek => 6, days => 4), fri->add(days => -4), "fri - 4 => mon (date) (6dw)";
75
is tue->subtract_businessdays(businessweek => 6, days => 9), tue->add(days => -11), "tue - 9 => fri (date) (6dw)";
76
is tue->subtract_businessdays(businessweek => 6, days => 8), tue->add(days => -10), "tue - 8 => sat (date) (6dw)";
77

  
78
# add with negative days?
79
is mon->add_businessdays(businessweek => 6, days => -5), mon->add(days => -6), "mon - 5 => tue (date) (6dw)";
80
is mon->add_businessdays(businessweek => 6, days => -12), mon->add(days => -14), "mon - 12 => mon (date) (6dw)";
81
is fri->add_businessdays(businessweek => 6, days => -4), fri->add(days => -4), "fri - 4 => mon (date) (6dw)";
82
is tue->add_businessdays(businessweek => 6, days => -9), tue->add(days => -11), "tue - 9 => fri (date) (6dw)";
83
is tue->add_businessdays(businessweek => 6, days => -8), tue->add(days => -10), "tue - 8 => sat (date) (6dw)";
84

  
85
# what if staring date falls into eekend?
86
is sun->add_businessdays(days => 1), sun->add(days => 1), "1 day after sun is mon";
87
is sat->add_businessdays(days => 1), sat->add(days => 2), "1 day after sut is mon";
88
is sun->add_businessdays(days => -1), sun->add(days => -2), "1 day before sun is fri";
89
is sat->add_businessdays(days => -1), sat->add(days => -1), "1 day before sut is fri";

Auch abrufbar als: Unified diff