blib/lib/AmberDB/Date.pm untested

filter: Statementstmt 0.0% 0 / 389 Branchbran 0.0% 0 / 264 Conditioncond 0.0% 0 / 254 MC/DCmcdc 0.0% 0 / 144 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 1114 CC 133 SCAR 42.3
CC
133
Cov
0%
day_range
156.0
dateid2week
156.0
offset2date
156.0
CRAP 68.6
1 package AmberDB::Date;
2
3 use 5.016;
4 use warnings;
5 use Carp qw(croak cluck);
6 use Encode; # Encoding management if needed
7 use Time::Local; # Core module for time operations
8
9 our $VERSION = '5.26.6';
10 my $CREATED = '2008-02-07';
11
12 # Default English Month Names
13 my $DEFAULT_MONTHS = [
14 "January", "February", "March", "April", "May", "June",
15 "July", "August", "September", "October", "November", "December"
16 ];
17
18 # Default English Day Names
19 my $DEFAULT_DAYS = [
20 "Sunday", "Monday", "Tuesday", "Wednesday",
21 "Thursday", "Friday", "Saturday"
22 ];
23
24 my $MAX_CACHE_ENTRIES = 1024;
25
26 # ------------------------------------------------
27 # Constructor
28 # ------------------------------------------------
29 sub new {
30 my $class = shift;
31 my %args = ( ref $_[0] eq 'HASH' ) ? %{ $_[0] } : @_;
32 my $self = bless {}, $class;
33 $self->{_date} ||= {};
34
35 if ( $args{locale} ) {
36 $self->{locale} = $args{locale};
37 }
38 else {
39 require AmberDB::Locale;
40 $self->{locale} = AmberDB::Locale->new( language => $args{language} );
41 }
42
43 $self->{months} //= ( $self->{locale} && $self->{locale}->can('months') ? $self->{locale}->months() : undef )
44 // ( $self->can('months') ? $self->months() : $DEFAULT_MONTHS );
45 $self->{days} //= ( $self->{locale} && $self->{locale}->can('days') ? $self->{locale}->days() : undef )
46 // ( $self->can('days') ? $self->days() : $DEFAULT_DAYS );
47
48 if ( defined $args{time} ) {
49 $self->{time} = $args{time};
50 }
51
52 # Populate current timestamp data into self so $date->day_id, $date->year etc. work immediately
53 my $d = $self->get_date( $args{time} );
54 %$self = ( %$self, %$d );
55
56 return $self;
57 }
58
59 # ------------------------------------------------
60 # Returns detailed current or specified time as a blessed AmberDB::Date object
61 # ------------------------------------------------
62 sub get_date {
63 my ( $self, $time ) = @_;
64
65 my $t;
66 if ( defined $time ) {
67 $t = $time;
68 }
69 elsif ( ref $self ) {
70 $t = $self->{time} //= ( ( $self->{date} && defined $self->{date}->{time} ) ? $self->{date}->{time} : time() );
71 }
72 else {
73 $t = time();
74 }
75
76 if ( ref $self && exists $self->{_date}{get_date}{$t} ) {
77 return $self->{_date}{get_date}{$t};
78 }
79
80 my $date_hash = {};
81
82 # Set timestamp value
83 $date_hash->{time} = $t;
84
85 (
86 $date_hash->{second}, $date_hash->{minute},
87 $date_hash->{hour}, $date_hash->{day},
88 $date_hash->{month}, $date_hash->{year},
89 $date_hash->{daynumber}
90 ) = ( localtime( $date_hash->{time} ) )[ 0, 1, 2, 3, 4, 5, 6 ];
91
92 my $months = ( ref $self && $self->{months} ) // ( $self->can('months') ? $self->months() : $DEFAULT_MONTHS );
93 my $days = ( ref $self && $self->{days} ) // ( $self->can('days') ? $self->days() : $DEFAULT_DAYS );
94
95 $date_hash->{monthname} = $months->[ $date_hash->{month} ];
96 $date_hash->{dayname} = $days->[ $date_hash->{daynumber} ];
97 $date_hash->{daynames} = join( " ", @$days );
98 $date_hash->{monthnames} = join( " ", @$months );
99
100 $date_hash->{year} += 1900;
101 $date_hash->{month} += 1;
102
103 # Zero-padding formatting
104 $date_hash->{month} = sprintf "%02d", $date_hash->{month};
105 $date_hash->{day} = sprintf "%02d", $date_hash->{day};
106 $date_hash->{hour} = sprintf "%02d", $date_hash->{hour};
107 $date_hash->{minute} = sprintf "%02d", $date_hash->{minute};
108 $date_hash->{second} = sprintf "%02d", $date_hash->{second};
109
110 $date_hash->{month_id} = $date_hash->{year} . $date_hash->{month};
111 $date_hash->{day_id} = $date_hash->{month_id} . $date_hash->{day};
112 $date_hash->{hour_id} = $date_hash->{day_id} . $date_hash->{hour};
113 $date_hash->{minute_id} = $date_hash->{hour_id} . $date_hash->{minute};
114 $date_hash->{second_id} = $date_hash->{minute_id} . $date_hash->{second};
115
116 $date_hash->{short} =
117 "$date_hash->{day}/$date_hash->{month}/$date_hash->{year}";
118 $date_hash->{only_time} =
119 "$date_hash->{hour}:$date_hash->{minute}:$date_hash->{second}";
120 $date_hash->{str} = "$date_hash->{short} - $date_hash->{only_time}";
121 $date_hash->{date_short} = $date_hash->{short};
122 $date_hash->{date_str} = $date_hash->{str};
123
124 # Side-effect: Save year directory to object path
125 $date_hash->{year_dir} = $date_hash->{year};
126
127 $date_hash->{months} = $months;
128 $date_hash->{days} = $days;
129
130 my $obj = bless $date_hash, 'AmberDB::Date';
131
132 if ( ref $self ) {
133 $self->{_date}{get_date} = {} if keys %{ $self->{_date}{get_date} || {} } > $MAX_CACHE_ENTRIES;
134 $self->{_date}{get_date}{$t} = $obj;
135 }
136
137 return $obj;
138 }
139
140 # ------------------------------------------------
141 # OOP Accessor / Getter Methods
142 # Support both $date->day_id and $date->day_id($custom_epoch)
143 # ------------------------------------------------
144 sub year {
145 my ( $self, $t ) = @_;
146 return $self->get_date($t)->{year} if defined $t;
147 return $self->{year} if defined $self->{year};
148 return $self->{year} = $self->{date}->{year} if ref $self && $self->{date} && defined $self->{date}->{year};
149 return $self->{year} = $self->get_date()->{year};
150 }
151
152 sub month {
153 my ( $self, $t ) = @_;
154 return $self->get_date($t)->{month} if defined $t;
155 return $self->{month} if defined $self->{month};
156 return $self->{month} = $self->{date}->{month} if ref $self && $self->{date} && defined $self->{date}->{month};
157 return $self->{month} = $self->get_date()->{month};
158 }
159
160 sub day {
161 my ( $self, $t ) = @_;
162 return $self->get_date($t)->{day} if defined $t;
163 return $self->{day} if defined $self->{day};
164 return $self->{day} = $self->{date}->{day} if ref $self && $self->{date} && defined $self->{date}->{day};
165 return $self->{day} = $self->get_date()->{day};
166 }
167
168 sub hour {
169 my ( $self, $t ) = @_;
170 return $self->get_date($t)->{hour} if defined $t;
171 return $self->{hour} if defined $self->{hour};
172 return $self->{hour} = $self->{date}->{hour} if ref $self && $self->{date} && defined $self->{date}->{hour};
173 return $self->{hour} = $self->get_date()->{hour};
174 }
175
176 sub minute {
177 my ( $self, $t ) = @_;
178 return $self->get_date($t)->{minute} if defined $t;
179 return $self->{minute} if defined $self->{minute};
180 return $self->{minute} = $self->{date}->{minute} if ref $self && $self->{date} && defined $self->{date}->{minute};
181 return $self->{minute} = $self->get_date()->{minute};
182 }
183
184 sub second {
185 my ( $self, $t ) = @_;
186 return $self->get_date($t)->{second} if defined $t;
187 return $self->{second} if defined $self->{second};
188 return $self->{second} = $self->{date}->{second} if ref $self && $self->{date} && defined $self->{date}->{second};
189 return $self->{second} = $self->get_date()->{second};
190 }
191
192 sub month_id {
193 my ( $self, $t ) = @_;
194 return $self->get_date($t)->{month_id} if defined $t;
195 return $self->{month_id} if defined $self->{month_id};
196 return $self->{month_id} = $self->{date}->{month_id} if ref $self && $self->{date} && defined $self->{date}->{month_id};
197 return $self->{month_id} = $self->year . $self->month;
198 }
199
200 sub day_id {
201 my ( $self, $t ) = @_;
202 return $self->get_date($t)->{day_id} if defined $t;
203 return $self->{day_id} if defined $self->{day_id};
204 return $self->{day_id} = $self->{date}->{day_id} if ref $self && $self->{date} && defined $self->{date}->{day_id};
205 return $self->{day_id} = $self->month_id . $self->day;
206 }
207
208 sub hour_id {
209 my ( $self, $t ) = @_;
210 return $self->get_date($t)->{hour_id} if defined $t;
211 return $self->{hour_id} if defined $self->{hour_id};
212 return $self->{hour_id} = $self->{date}->{hour_id} if ref $self && $self->{date} && defined $self->{date}->{hour_id};
213 return $self->{hour_id} = $self->day_id . $self->hour;
214 }
215
216 sub minute_id {
217 my ( $self, $t ) = @_;
218 return $self->get_date($t)->{minute_id} if defined $t;
219 return $self->{minute_id} if defined $self->{minute_id};
220 return $self->{minute_id} = $self->{date}->{minute_id} if ref $self && $self->{date} && defined $self->{date}->{minute_id};
221 return $self->{minute_id} = $self->hour_id . $self->minute;
222 }
223
224 sub second_id {
225 my ( $self, $t ) = @_;
226 return $self->get_date($t)->{second_id} if defined $t;
227 return $self->{second_id} if defined $self->{second_id};
228 return $self->{second_id} = $self->{date}->{second_id} if ref $self && $self->{date} && defined $self->{date}->{second_id};
229 return $self->{second_id} = $self->minute_id . $self->second;
230 }
231
232 sub date_str {
233 my ( $self, $t ) = @_;
234 return $self->get_date($t)->{str} if defined $t;
235 return $self->{date_str} if defined $self->{date_str};
236 return $self->{date_str} = $self->{date}->{str} if ref $self && $self->{date} && defined $self->{date}->{str};
237 return $self->{date_str} = ( $self->date_short . " - " . $self->only_time );
238 }
239
240 sub date_short {
241 my ( $self, $t ) = @_;
242 return $self->get_date($t)->{short} if defined $t;
243 return $self->{date_short} if defined $self->{date_short};
244 return $self->{date_short} = $self->{date}->{short} if ref $self && $self->{date} && defined $self->{date}->{short};
245 return $self->{date_short} = ( $self->day . "/" . $self->month . "/" . $self->year );
246 }
247
248 sub only_time {
249 my ( $self, $t ) = @_;
250 return $self->get_date($t)->{only_time} if defined $t;
251 return $self->{only_time} if defined $self->{only_time};
252 return $self->{only_time} = $self->{date}->{only_time} if ref $self && $self->{date} && defined $self->{date}->{only_time};
253 return $self->{only_time} = ( $self->hour . ":" . $self->minute . ":" . $self->second );
254 }
255
256 sub epoch {
257 my ($self) = @_;
258 return $self->{time} //= ( ( ref $self && $self->{date} && defined $self->{date}->{time} ) ? $self->{date}->{time} : time() );
259 }
260
261 sub monthname {
262 my ( $self, $t ) = @_;
263 return $self->get_date($t)->{monthname} if defined $t;
264 return $self->{monthname} if defined $self->{monthname};
265 return $self->{monthname} = $self->{date}->{monthname} if ref $self && $self->{date} && defined $self->{date}->{monthname};
266 return $self->get_date()->{monthname};
267 }
268
269 sub dayname {
270 my ( $self, $t ) = @_;
271 return $self->get_date($t)->{dayname} if defined $t;
272 return $self->{dayname} if defined $self->{dayname};
273 return $self->{dayname} = $self->{date}->{dayname} if ref $self && $self->{date} && defined $self->{date}->{dayname};
274 return $self->get_date()->{dayname};
275 }
276
277 sub year_dir {
278 my ( $self, $t ) = @_;
279 return $self->get_date($t)->{year_dir} if defined $t;
280 return $self->{year_dir} if defined $self->{year_dir};
281 return $self->{year_dir} = $self->{date}->{year_dir} if ref $self && $self->{date} && defined $self->{date}->{year_dir};
282 return $self->year;
283 }
284
285 # ------------------------------------------------
286 # Converts date string to numeric date ID (YYYYMMDDHHMMSS)
287 # ------------------------------------------------
288 sub str2dateid {
289 my ( $self, $datestr ) = @_;
290 return unless defined $datestr && length($datestr);
291
292 if ( ref $self && exists $self->{_date}{str2dateid}{$datestr} ) {
293 return $self->{_date}{str2dateid}{$datestr};
294 }
295
296 my ( $day_id, $hour, $dateid );
297 my ( $day, $month, $year );
298
299 # DD/MM/YYYY or DD.MM.YYYY format
300 if ( $datestr =~ /([0-9]{1,2})[\.\/]([0-9]{1,2})[\.\/]([0-9]{4})/ ) {
301 ( $day, $month, $year ) = ( $1, $2, $3 );
302 }
303
304 # YYYY-MM-DD format
305 elsif ( $datestr =~ /([0-9]{4})\-([0-9]{1,2})\-([0-9]{1,2})/ ) {
306 ( $day, $month, $year ) = ( $3, $2, $1 );
307 }
308
309 # Already numeric date ID format
310 elsif ( $datestr =~ /^(?:[0-9]{14}|[0-9]{12}|[0-9]{8})$/ ) {
311 $day_id = $datestr;
312 }
313
314 if ( $day && $month && $year ) {
315 $day = sprintf "%02d", $day;
316 $month = sprintf "%02d", $month;
317 $day_id = $year . $month . $day;
318 }
319
320 # Parse time component if present
321 if ( $datestr =~ /([0-9]{1,2}):([0-9]{1,2})(:([0-9]{2}))?/ ) {
322 my ( $hor, $min, $sec ) = ( $1, $2, $4 );
323 $hor = sprintf "%02d", $hor;
324 $min = sprintf "%02d", $min;
325 $sec = sprintf "%02d", ( $sec // 0 );
326 $hour = $hor . $min . $sec;
327 }
328
329 $dateid = ( $day_id // '' ) . ( $hour // '' );
330
331 if ( ref $self ) {
332 $self->{_date}{str2dateid} = {} if keys %{ $self->{_date}{str2dateid} || {} } > $MAX_CACHE_ENTRIES;
333 $self->{_date}{str2dateid}{$datestr} = $dateid;
334 }
335
336 return $dateid;
337 }
338
339 # ------------------------------------------------
340 # Converts numeric date ID into human-readable string
341 # ------------------------------------------------
342 sub dateid2str {
343 my ( $self, $dateid ) = @_;
344 return unless defined $dateid && length($dateid);
345
346 if ( ref $self && exists $self->{_date}{dateid2str}{$dateid} ) {
347 return $self->{_date}{dateid2str}{$dateid};
348 }
349
350 my $formatted;
351
352 # secondid (14 digits)
353 if ( $dateid =~
354 /^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/ )
355 {
356 $formatted = "$3/$2/$1 - $4:$5:$6";
357 }
358
359 # minuteid (12 digits)
360 elsif ( $dateid =~ /^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/ ) {
361 $formatted = "$3/$2/$1 - $4:$5";
362 }
363
364 # dayid (8 digits)
365 elsif ( $dateid =~ /^([0-9]{4})([0-9]{2})([0-9]{2})/ ) {
366 $formatted = "$3/$2/$1";
367 }
368
369 # monthid (6 digits)
370 elsif ( $dateid =~ /^([0-9]{4})([0-9]{2})/ ) {
371 my $year = $1;
372 my $month = $2 - 1; # 0-indexed
373
374 # Fallback to default months if array missing
375 my $months_ref = ( ref $self && $self->{months} ) // ( $self->can('months') ? $self->months() : $DEFAULT_MONTHS );
376 $formatted = "$months_ref->[$month] $year";
377 }
378 else {
379 $formatted = $dateid;
380 }
381
382 if ( ref $self ) {
383 $self->{_date}{dateid2str} = {} if keys %{ $self->{_date}{dateid2str} || {} } > $MAX_CACHE_ENTRIES;
384 $self->{_date}{dateid2str}{$dateid} = $formatted;
385 }
386
387 return $formatted;
388 }
389
390 # ------------------------------------------------
391 # Format timestamp string for emails/HTTP headers
392 # ------------------------------------------------
393 sub time2str {
394 my ( $self, $time ) = @_;
395 $time //= ( ref $self ? ( $self->{time} //= time() ) : time() );
396
397 if ( ref $self && exists $self->{_date}{time2str}{$time} ) {
398 return $self->{_date}{time2str}{$time};
399 }
400
401 my $str;
402 eval { require HTTP::Date; };
403 if ($@) {
404 # Basic fallback string format if HTTP::Date is missing
405 my $d = $self->get_date($time);
406 $str = $d->{str};
407 }
408 else {
409 $str = HTTP::Date::time2str($time);
410 }
411
412 if ( ref $self ) {
413 $self->{_date}{time2str} = {} if keys %{ $self->{_date}{time2str} || {} } > $MAX_CACHE_ENTRIES;
414 $self->{_date}{time2str}{$time} = $str;
415 }
416
417 return $str;
418 }
419
420 # ------------------------------------------------
421 # Lists array of day IDs between two date boundaries
422 # ------------------------------------------------
423 sub day_range {
424 my ( $self, $start, $end ) = @_;
425 return unless $start && $end;
426
427 my $range_key = "$start:$end";
428 if ( ref $self && exists $self->{_date}{day_range}{$range_key} ) {
429 my $cached = $self->{_date}{day_range}{$range_key};
430 return wantarray ? @$cached : scalar(@$cached);
431 }
432
433 my ( $start_year, $start_month, $start_day ) =
434 ( $start =~ /([0-9]{4})([0-9]{2})([0-9]{2})/ );
435 my ( $end_year, $end_month, $end_day ) =
436 ( $end =~ /([0-9]{4})([0-9]{2})([0-9]{2})/ );
437
438 return unless $start_year && $end_year;
439
440 my @days;
441
442 # If years differ, divide range into yearly sub-calculations
443 if ( $start_year != $end_year ) {
444 my @calcs = ( [ $start, $start_year . "1231" ] );
445 my $mid_year = $start_year + 1;
446 while ( $mid_year < $end_year ) {
447 push @calcs, [ $mid_year . "0101", $mid_year . "1231" ];
448 $mid_year++;
449 }
450 push @calcs, [ $end_year . "0101", $end ];
451
452 foreach my $calc (@calcs) {
453 push @days, $self->day_range(@$calc);
454 }
455 }
456 else {
457 # Days count table per month
458 my @months = (
459 [ ( 1 .. 31 ) ],
460 [ ( 1 .. 28 ) ],
461 [ ( 1 .. 31 ) ],
462 [ ( 1 .. 30 ) ],
463 [ ( 1 .. 31 ) ],
464 [ ( 1 .. 30 ) ],
465 [ ( 1 .. 31 ) ],
466 [ ( 1 .. 31 ) ],
467 [ ( 1 .. 30 ) ],
468 [ ( 1 .. 31 ) ],
469 [ ( 1 .. 30 ) ],
470 [ ( 1 .. 31 ) ]
471 );
472
473 # Gregorian calendar leap year check
474 my $is_leap = ( ( $start_year % 4 == 0 && $start_year % 100 != 0 )
475 || ( $start_year % 400 == 0 ) );
476 if ($is_leap) {
477 push @{ $months[1] }, 29;
478 }
479
480 for ( my $i = 0 ; $i < @months ; $i++ ) {
481 my $month = $i + 1;
482 $month = sprintf "%02d", $month;
483
484 foreach my $day ( @{ $months[$i] } ) {
485 $day = sprintf "%02d", $day;
486 my $dayid = $start_year . $month . $day;
487
488 next if $dayid < $start;
489 last if $dayid > $end; # Stop iteration once end boundary exceeded
490 push @days, $dayid;
491 }
492 }
493 }
494
495 if ( ref $self ) {
496 $self->{_date}{day_range} = {} if keys %{ $self->{_date}{day_range} || {} } > $MAX_CACHE_ENTRIES;
497 $self->{_date}{day_range}{$range_key} = [@days];
498 }
499
500 return wantarray ? @days : scalar(@days);
501 }
502
503 # ------------------------------------------------
504 # Calculates ISO week number for given date ID
505 # ------------------------------------------------
506 sub dateid2week {
507 my ( $self, $dateid ) = @_;
508 return unless defined $dateid && length($dateid);
509
510 if ( ref $self && exists $self->{_date}{dateid2week}{$dateid} ) {
511 my $cached = $self->{_date}{dateid2week}{$dateid};
512 return wantarray ? @$cached : $cached->[0];
513 }
514
515 my $daytime;
516 my ( $y, $m, $d, $h, $min, $s ) = ( 0, 0, 0, 0, 0, 0 );
517
518 # secondid
519 if ( $dateid =~
520 /^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/ )
521 {
522 ( $y, $m, $d, $h, $min, $s ) = ( $1, $2, $3, $4, $5, $6 );
523 }
524
525 # minuteid
526 elsif ( $dateid =~ /^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/ ) {
527 ( $y, $m, $d, $h, $min ) = ( $1, $2, $3, $4, $5 );
528 }
529
530 # dayid
531 elsif ( $dateid =~ /^([0-9]{4})([0-9]{2})([0-9]{2})/ ) {
532 ( $y, $m, $d ) = ( $1, $2, $3 );
533 }
534 else {
535 return;
536 }
537
538 # Convert to epoch time using Time::Local
539 eval { $daytime = timelocal( $s, $min, $h, $d, ( $m - 1 ), $y ); };
540 return if $@;
541
542 my ( $wday, $yday ) = ( localtime($daytime) )[ 6, 7 ];
543 my $days = ( $yday - $wday ) / 7;
544 my $yweek = ( $days =~ /([0-9]+)\./ ) ? ( $1 + 2 ) : ( $days + 1 );
545
546 if ( ref $self ) {
547 $self->{_date}{dateid2week} = {} if keys %{ $self->{_date}{dateid2week} || {} } > $MAX_CACHE_ENTRIES;
548 $self->{_date}{dateid2week}{$dateid} = [ $yweek, $wday, $yday ];
549 }
550
551 return wantarray ? ( $yweek, $wday, $yday ) : $yweek;
552 }
553
554 # ------------------------------------------------
555 # Calculates new date string based on offset string (e.g. 2D, 1M, 1Y)
556 # ------------------------------------------------
557 sub offset2date {
558 my ( $self, $string ) = @_;
559 return unless defined $string && length($string);
560
561 my $today = ref $self ? $self->day_id : undef;
562 my $cache_key = defined $today ? "$today:$string" : undef;
563
564 if ( $cache_key && exists $self->{_date}{offset2date}{$cache_key} ) {
565 return $self->{_date}{offset2date}{$cache_key};
566 }
567
568 my $ls = ( $string =~ /^\-/ ) ? -1 : 1;
569 my $dd = ( $string =~ /([0-9]+)D/ ) ? $1 : 0;
570 my $mm = ( $string =~ /([0-9]+)M/ ) ? $1 : 0;
571 my $yy = ( $string =~ /([0-9]+)Y/ ) ? $1 : 0;
572
573 # Unit durations in seconds
574 my $unit = {};
575 $unit->{hour} = 60 * 60;
576 $unit->{day} = 60 * 60 * 24;
577 $unit->{week} = 60 * 60 * 24 * 7;
578 $unit->{month} = 60 * 60 * 24 * 30; # Approximate
579 $unit->{year} = 60 * 60 * 24 * 365;
580
581 my $all_diff =
582 $ls *
583 ( ( $dd * $unit->{day} ) +
584 ( $mm * $unit->{month} ) +
585 ( $yy * $unit->{year} ) );
586
587 my $base_time = ref $self ? ( $self->{time} //= time() ) : time();
588 my ( $second, $minute, $hour, $day, $month, $year, $dnumber ) =
589 ( localtime( $base_time + $all_diff ) )[ 0, 1, 2, 3, 4, 5, 6 ];
590
591 $year += 1900;
592 $month += 1;
593
594 $month = sprintf "%02d", $month;
595 $day = sprintf "%02d", $day;
596
597 my $res = $year . $month . $day;
598
599 if ( $cache_key && ref $self ) {
600 $self->{_date}{offset2date} = {} if keys %{ $self->{_date}{offset2date} || {} } > $MAX_CACHE_ENTRIES;
601 $self->{_date}{offset2date}{$cache_key} = $res;
602 }
603
604 return $res;
605 }
606
607 # ------------------------------------------------
608 # Returns number of days per month for specified year offset
609 # ------------------------------------------------
610 sub MonthDaysInYear {
611 my ( $self, $year_offset ) = @_;
612 $year_offset //= 0;
613
614 my $cur_year = ref $self ? $self->year : ( (localtime)[5] + 1900 );
615 my $cache_key = "$cur_year:$year_offset";
616
617 if ( ref $self && exists $self->{_date}{monthdays}{$cache_key} ) {
618 my $cached = $self->{_date}{monthdays}{$cache_key};
619 return wantarray ? @$cached : scalar(@$cached);
620 }
621
622 my $hour = 60 * 60;
623 my $day = 24 * $hour;
624
625 # Approximate year offset calculation
626 my $yy_time = ( ( $day * 365 ) + ( $hour * 6 ) ) * $year_offset;
627
628 my $base_time = ref $self ? ( $self->{time} //= time() ) : time();
629 my ( $yy_month, $yy_year ) = ( localtime( $base_time + $yy_time ) )[ 4, 5 ];
630
631 my @MonthDays = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
632
633 # Gregorian leap year check
634 my $full_year = $yy_year + 1900;
635 if ( ( $full_year % 4 == 0 && $full_year % 100 != 0 )
636 || ( $full_year % 400 == 0 ) )
637 {
638 $MonthDays[1] = 29;
639 }
640
641 if ( ref $self ) {
642 $self->{_date}{monthdays} = {} if keys %{ $self->{_date}{monthdays} || {} } > $MAX_CACHE_ENTRIES;
643 $self->{_date}{monthdays}{$cache_key} = [ $yy_month, @MonthDays ];
644 }
645
646 return ( $yy_month, @MonthDays );
647 }
648
649 # ------------------------------------------------
650 # Flushes date cache and memoized accessor values
651 # ------------------------------------------------
652 sub reset_date {
653 my ($self) = @_;
654 return unless ref $self;
655 %{ $self->{_date} } = () if $self->{_date};
656 undef $self->{time};
657 for my $k (qw(day_id second_id month_id hour_id minute_id year month day hour minute second date_str date_short only_time monthname dayname year_dir)) {
658 eval { delete $self->{$k}; };
659 }
660 return $self;
661 }
662
663 1;
664
665 __END__