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);
93 SL::Helpers::DateTime - helper functions for L<DateTime>
99 =item C<new_local %params>
101 Returns the time given in C<%params> with the time zone set to the
106 Returns the current time with the time zone set to the local time zone.
110 Returns the current date with the time zone set to the local time zone.
112 =item C<to_kivitendo %param>
114 Formats the date and time according to the current kivitendo user's
115 date format with L<Locale::format_datetime_object>.
117 The legacy name C<to_lxoffice> is still supported.
119 =item C<from_kivitendo $string>
121 Parses a date string formatted in the current kivitendo user's date
122 format and returns an instance of L<DateTime>.
124 Note that only dates can be parsed at the moment, not the time
125 component (as opposed to L<to_kivitendo>).
127 The legacy name C<from_lxoffice> is still supported.
129 =item C<end_of_month>
131 Sets the object to the last day of object's month at midnight. Returns
138 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>