547c4b94b7d279054cd11d06632b764fe0412f5a
[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 1;
86
87 __END__
88
89 =encoding utf8
90
91 =head1 NAME
92
93 SL::Helpers::DateTime - helper functions for L<DateTime>
94
95 =head1 FUNCTIONS
96
97 =over 4
98
99 =item C<new_local %params>
100
101 Returns the time given in C<%params> with the time zone set to the
102 local time zone.
103
104 =item C<now_local>
105
106 Returns the current time with the time zone set to the local time zone.
107
108 =item C<today_local>
109
110 Returns the current date with the time zone set to the local time zone.
111
112 =item C<to_kivitendo %param>
113
114 Formats the date and time according to the current kivitendo user's
115 date format with L<Locale::format_datetime_object>.
116
117 The legacy name C<to_lxoffice> is still supported.
118
119 =item C<from_kivitendo $string>
120
121 Parses a date string formatted in the current kivitendo user's date
122 format and returns an instance of L<DateTime>.
123
124 Note that only dates can be parsed at the moment, not the time
125 component (as opposed to L<to_kivitendo>).
126
127 The legacy name C<from_lxoffice> is still supported.
128
129 =item C<end_of_month>
130
131 Sets the object to the last day of object's month at midnight. Returns
132 the object itself.
133
134 =back
135
136 =head1 AUTHOR
137
138 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
139
140 =cut