15ab95a7b7473519989cd0f92aca72e4029a6830
[kivitendo-erp.git] / SL / Helper / DateTime.pm
1 package DateTime;
2
3 use strict;
4
5 use DateTime::Format::Strptime;
6
7 use SL::Util qw(_hashify);
8
9 my ($ymd_parser, $ymdhms_parser);
10
11 sub new_local {
12   my ($class, %params) = @_;
13   return $class->new(hour => 0, minute => 0, second => 0, time_zone => $::locale->get_local_time_zone, %params);
14 }
15
16 sub now_local {
17   return shift->now(time_zone => $::locale->get_local_time_zone);
18 }
19
20 sub today_local {
21   return shift->now(time_zone => $::locale->get_local_time_zone)->truncate(to => 'day');
22 }
23
24 sub to_kivitendo_time {
25   my ($self, %params) = _hashify(1, @_);
26   return $::locale->format_date_object_to_time($self, %params);
27 }
28
29 sub to_kivitendo {
30   my ($self, %params) = _hashify(1, @_);
31   return $::locale->format_date_object($self, %params);
32 }
33
34 sub to_lxoffice {
35   # Legacy name.
36   goto &to_kivitendo;
37 }
38
39 sub from_kivitendo {
40   return $::locale->parse_date_to_object($_[1]);
41 }
42
43 sub from_lxoffice {
44   # Legacy name.
45   goto &from_kivitendo;
46 }
47
48 sub add_business_duration {
49   my ($self, %params) = @_;
50
51   my $abs_days = abs $params{days};
52   my $neg      = $params{days} < 0;
53   my $bweek    = $params{businessweek} || 5;
54   my $weeks    = int ($abs_days / $bweek);
55   my $days     = $abs_days % $bweek;
56
57   if ($neg) {
58     $self->subtract(weeks => $weeks);
59     $self->add(days => 8 - $self->day_of_week) if $self->day_of_week > $bweek;
60     $self->subtract(days => $self->day_of_week > $days ? $days : $days + (7 - $bweek));
61   } else {
62     $self->add(weeks => $weeks);
63     $self->subtract(days => $self->day_of_week - $bweek) if $self->day_of_week > $bweek;
64     $self->add(days => $self->day_of_week + $days <= $bweek ? $days : $days + (7 - $bweek));
65   }
66
67   $self;
68 }
69
70 sub add_businessdays {
71   my ($self, %params) = @_;
72
73   $self->add_business_duration(%params);
74 }
75
76 sub subtract_businessdays {
77   my ($self, %params) = @_;
78
79   $params{days} *= -1;
80
81   $self->add_business_duration(%params);
82 }
83
84 sub end_of_month {
85   my ($self) = @_;
86   return $self->truncate(to => 'month')->add(months => 1)->subtract(days => 1);
87 }
88
89 sub next_workday {
90   my ($self, %params) = @_;
91
92   my $extra_days = $params{extra_days} // 1;
93   $self->add(days => $extra_days);
94
95   my $day_of_week = $self->day_of_week;
96   $self->add(days => (8 - $day_of_week)) if $day_of_week >= 6;
97
98   return $self;
99 }
100
101 sub from_ymd {
102   my ($class, $ymd_string) = @_;
103
104   if (!$ymd_parser) {
105     $ymd_parser = DateTime::Format::Strptime->new(
106       pattern   => '%Y-%m-%d',
107       locale    => 'de_DE',
108       time_zone => 'local'
109     );
110   }
111
112   return $ymd_parser->parse_datetime($ymd_string // '');
113 }
114
115 sub from_ymdhms {
116   my ($class, $ymdhms_string) = @_;
117
118   if (!$ymdhms_parser) {
119     $ymdhms_parser = DateTime::Format::Strptime->new(
120       pattern   => '%Y-%m-%dT%H:%M:%S',
121       locale    => 'de_DE',
122       time_zone => 'local'
123     );
124   }
125
126   $ymdhms_string //= '';
127   $ymdhms_string   =~ s{ }{T};
128
129   return $ymdhms_parser->parse_datetime($ymdhms_string);
130 }
131
132 1;
133
134 __END__
135
136 =encoding utf8
137
138 =head1 NAME
139
140 SL::Helpers::DateTime - helper functions for L<DateTime>
141
142 =head1 FUNCTIONS
143
144 =over 4
145
146 =item C<new_local %params>
147
148 Returns the time given in C<%params> with the time zone set to the
149 local time zone.
150
151 =item C<now_local>
152
153 Returns the current time with the time zone set to the local time zone.
154
155 =item C<today_local>
156
157 Returns the current date with the time zone set to the local time zone.
158
159 =item C<to_kivitendo %param>
160
161 Formats the date and time according to the current kivitendo user's
162 date format with L<Locale::format_datetime_object>.
163
164 The legacy name C<to_lxoffice> is still supported.
165
166 =item C<from_kivitendo $string>
167
168 Parses a date string formatted in the current kivitendo user's date
169 format and returns an instance of L<DateTime>.
170
171 Note that only dates can be parsed at the moment, not the time
172 component (as opposed to L<to_kivitendo>).
173
174 The legacy name C<from_lxoffice> is still supported.
175
176 =item C<from_ymd $string>
177
178 Parses a date string in the ISO 8601 format C<YYYY-MM-DD> and returns
179 an instance of L<DateTime>. The time is set to midnight (00:00:00).
180
181 =item C<from_ymdhms $string>
182
183 Parses a date/time string in the ISO 8601 format
184 C<YYYY-MM-DDTHH:MM:SS> (a space instead of C<T> is also supported) and
185 returns an instance of L<DateTime>.
186
187 =item C<end_of_month>
188
189 Sets the object to the last day of object's month at midnight. Returns
190 the object itself.
191
192 =item C<next_workday %params>
193
194 Sets the object to the next workday. The recognized parameter is:
195
196 =over 2
197
198 =item * C<extra_days> - optional: If C<extra_days> is given, then
199 that amount of days is added to the objects date and if the resulting
200 date is not a workday, the object is set to the next workday.
201 Defaults to 1.
202
203 =back
204
205 Returns the object itself.
206
207 =back
208
209 =head1 AUTHOR
210
211 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
212
213 =cut