5 use SL::Util qw(_hashify);
8 my ($class, %params) = @_;
9 return $class->new(hour => 0, minute => 0, second => 0, time_zone => $::locale->get_local_time_zone, %params);
13 return shift->now(time_zone => $::locale->get_local_time_zone);
17 return shift->now(time_zone => $::locale->get_local_time_zone)->truncate(to => 'day');
20 sub to_kivitendo_time {
21 my ($self, %params) = _hashify(1, @_);
22 return $::locale->format_date_object_to_time($self, %params);
26 my ($self, %params) = _hashify(1, @_);
27 return $::locale->format_date_object($self, %params);
36 return $::locale->parse_date_to_object($_[1]);
44 sub add_business_duration {
45 my ($self, %params) = @_;
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;
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));
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));
66 sub add_businessdays {
67 my ($self, %params) = @_;
69 $self->add_business_duration(%params);
72 sub subtract_businessdays {
73 my ($self, %params) = @_;
77 $self->add_business_duration(%params);
82 return $self->truncate(to => 'month')->add(months => 1)->subtract(days => 1);
86 my ($self, %params) = @_;
88 my $extra_days = $params{extra_days} // 1;
89 $self->add(days => $extra_days);
91 my $day_of_week = $self->day_of_week;
92 $self->add(days => (8 - $day_of_week)) if $day_of_week >= 6;
105 SL::Helpers::DateTime - helper functions for L<DateTime>
111 =item C<new_local %params>
113 Returns the time given in C<%params> with the time zone set to the
118 Returns the current time with the time zone set to the local time zone.
122 Returns the current date with the time zone set to the local time zone.
124 =item C<to_kivitendo %param>
126 Formats the date and time according to the current kivitendo user's
127 date format with L<Locale::format_datetime_object>.
129 The legacy name C<to_lxoffice> is still supported.
131 =item C<from_kivitendo $string>
133 Parses a date string formatted in the current kivitendo user's date
134 format and returns an instance of L<DateTime>.
136 Note that only dates can be parsed at the moment, not the time
137 component (as opposed to L<to_kivitendo>).
139 The legacy name C<from_lxoffice> is still supported.
141 =item C<end_of_month>
143 Sets the object to the last day of object's month at midnight. Returns
146 =item C<next_workday %params>
148 Sets the object to the next workday. The recognized parameter is:
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.
159 Returns the object itself.
165 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>