| 1 | package HTML::CalendarMonth::DateTool::DateManip; | ||
| 2 | { | ||
| 3 | $HTML::CalendarMonth::DateTool::DateManip::VERSION = '1.26'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # Interface to Date::Manip | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Carp; | ||
| 11 | |||
| 12 | use base qw( HTML::CalendarMonth::DateTool ); | ||
| 13 | |||
| 14 | use Date::Manip qw( | ||
| 15 | Date_DaysInMonth | ||
| 16 | Date_DayOfWeek | ||
| 17 | DateCalc | ||
| 18 | UnixDate | ||
| 19 | Date_SecsSince1970 | ||
| 20 | ParseDateDelta | ||
| 21 | ); | ||
| 22 | |||
| 23 | sub dow1st_and_lastday { | ||
| 24 | my($self, $month, $year) = @_; | ||
| 25 | $month ||= $self->month; | ||
| 26 | $year ||= $self->year; | ||
| 27 | ($self->dow(1), Date_DaysInMonth($month, $year)); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub day_epoch { | ||
| 31 | my($self, $day, $month, $year) = @_; | ||
| 32 | $day || croak "day required.\n"; | ||
| 33 | $month ||= $self->month; | ||
| 34 | $year ||= $self->year; | ||
| 35 | Date_SecsSince1970($month, $day, $year, 0, 0, 0); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub dow { | ||
| 39 | # Date::Manip uses 1..7 as indicies in the week, starting with Monday. | ||
| 40 | # Convert to 0..6 starting with Sunday. | ||
| 41 | my($self, $day, $month, $year) = @_; | ||
| 42 | $day || croak "day required.\n"; | ||
| 43 | $month ||= $self->month; | ||
| 44 | $year ||= $self->year; | ||
| 45 | Date_DayOfWeek($month, $day, $year) % 7; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub add_days { | ||
| 49 | my($self, $delta, $day, $month, $year) = @_; | ||
| 50 | defined $delta || croak "delta (in days) required.\n"; | ||
| 51 | $day || croak "day required.\n"; | ||
| 52 | $month ||= $self->month; | ||
| 53 | $year ||= $self->year; | ||
| 54 | my $date = DateCalc( | ||
| 55 | sprintf("%04d%02d%02d", $year, $month, $day), | ||
| 56 | "$delta days" | ||
| 57 | ); | ||
| 58 | my($y, $m, $d) = $date =~ /^(\d{4})(\d\d)(\d\d)/; | ||
| 59 | $_ += 0 foreach ($y, $m, $d); | ||
| 60 | ($d, $m, $y); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub week_of_year { | ||
| 64 | my($self, $day, $month, $year) = @_; | ||
| 65 | $day || croak "day required.\n"; | ||
| 66 | $month ||= $self->month; | ||
| 67 | $year ||= $self->year; | ||
| 68 | my $week = UnixDate(sprintf("%04d%02d%02d", $year, $month, $day), '%U'); | ||
| 69 | $week += 0; | ||
| 70 | ($year, $week); | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; |