Berechnung des nächsten Arbeitstages aus oe.pl und OE.pm in DateTime-Helper …
[kivitendo-erp.git] / SL / Helper / DateTime.pm
1 package DateTime;
2
3 use strict;
4
5 use SL::Util qw(_hashify);
6
7 sub new_local {
8   my ($class, %params) = @_;
9   return $class->new(hour => 0, minute => 0, second => 0, time_zone => $::locale->get_local_time_zone, %params);
10 }
11
12 sub now_local {
13   return shift->now(time_zone => $::locale->get_local_time_zone);
14 }
15
16 sub today_local {
17   return shift->now(time_zone => $::locale->get_local_time_zone)->truncate(to => 'day');
18 }
19
20 sub to_kivitendo_time {
21   my ($self, %params) = _hashify(1, @_);
22   return $::locale->format_date_object_to_time($self, %params);
23 }
24
25 sub to_kivitendo {
26   my ($self, %params) = _hashify(1, @_);
27   return $::locale->format_date_object($self, %params);
28 }
29
30 sub to_lxoffice {
31   # Legacy name.
32   goto &to_kivitendo;
33 }
34
35 sub from_kivitendo {
36   return $::locale->parse_date_to_object($_[1]);
37 }
38
39 sub from_lxoffice {
40   # Legacy name.
41   goto &from_kivitendo;
42 }
43
44 sub add_business_duration {
45   my ($self, %params) = @_;
46
47   my $abs_days = abs $params{days};
48   my $neg      = $params{days} < 0;
49   my $bweek    = $params{businessweek} || 5;
50   my $weeks    = int ($abs_days / $bweek);
51   my $days     = $abs_days % $bweek;
52
53   if ($neg) {
54     $self->subtract(weeks => $weeks);
55     $self->add(days => 8 - $self->day_of_week) if $self->day_of_week > $bweek;
56     $self->subtract(days => $self->day_of_week > $days ? $days : $days + (7 - $bweek));
57   } else {
58     $self->add(weeks => $weeks);
59     $self->subtract(days => $self->day_of_week - $bweek) if $self->day_of_week > $bweek;
60     $self->add(days => $self->day_of_week + $days <= $bweek ? $days : $days + (7 - $bweek));
61   }
62
63   $self;
64 }
65
66 sub add_businessdays {
67   my ($self, %params) = @_;
68
69   $self->add_business_duration(%params);
70 }
71
72 sub subtract_businessdays {
73   my ($self, %params) = @_;
74
75   $params{days} *= -1;
76
77   $self->add_business_duration(%params);
78 }
79
80 sub end_of_month {
81   my ($self) = @_;
82   return $self->truncate(to => 'month')->add(months => 1)->subtract(days => 1);
83 }
84
85 sub next_workday {
86   my ($self, %params) = @_;
87
88   my $extra_days = $params{extra_days} // 1;
89   $self->add(days => $extra_days);
90
91   my $day_of_week = $self->day_of_week;
92   $self->add(days => (8 - $day_of_week)) if $day_of_week >= 6;
93
94   return $self;
95 }
96
97 1;
98
99 __END__
100
101 =encoding utf8
102
103 =head1 NAME
104
105 SL::Helpers::DateTime - helper functions for L<DateTime>
106
107 =head1 FUNCTIONS
108
109 =over 4
110
111 =item C<new_local %params>
112
113 Returns the time given in C<%params> with the time zone set to the
114 local time zone.
115
116 =item C<now_local>
117
118 Returns the current time with the time zone set to the local time zone.
119
120 =item C<today_local>
121
122 Returns the current date with the time zone set to the local time zone.
123
124 =item C<to_kivitendo %param>
125
126 Formats the date and time according to the current kivitendo user's
127 date format with L<Locale::format_datetime_object>.
128
129 The legacy name C<to_lxoffice> is still supported.
130
131 =item C<from_kivitendo $string>
132
133 Parses a date string formatted in the current kivitendo user's date
134 format and returns an instance of L<DateTime>.
135
136 Note that only dates can be parsed at the moment, not the time
137 component (as opposed to L<to_kivitendo>).
138
139 The legacy name C<from_lxoffice> is still supported.
140
141 =item C<end_of_month>
142
143 Sets the object to the last day of object's month at midnight. Returns
144 the object itself.
145
146 =item C<next_workday %params>
147
148 Sets the object to the next workday. The recognized parameter is:
149
150 =over 2
151
152 =item * C<extra_days> - optional: If C<extra_days> is given, then
153 that amount of days is added to the objects date and if the resulting
154 date is not a workday, the object is set to the next workday.
155 Defaults to 1.
156
157 =back
158
159 Returns the object itself.
160
161 =back
162
163 =head1 AUTHOR
164
165 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
166
167 =cut