| 1 | package Template::Plugin::Date::FriendlyDelta; | ||
| 2 | |||
| 3 | use 5.008001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = '0.02'; | ||
| 8 | |||
| 9 | use base qw( Template::Plugin::Filter ); | ||
| 10 | |||
| 11 | use DateTime; | ||
| 12 | use Date::Parse; | ||
| 13 | |||
| 14 | sub init { | ||
| 15 | my $self = shift; | ||
| 16 | |||
| 17 | $self->{ _DYNAMIC } = 1; | ||
| 18 | $self->install_filter('friendlydelta'); | ||
| 19 | |||
| 20 | return $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub filter { | ||
| 24 | my ($self, $date2, $args) = @_; | ||
| 25 | |||
| 26 | my $date1 = DateTime->now(); | ||
| 27 | |||
| 28 | # Default strings | ||
| 29 | my $format_text = { | ||
| 30 | month => { | ||
| 31 | 1 => 'January', | ||
| 32 | 2 => 'February', | ||
| 33 | 3 => 'March', | ||
| 34 | 4 => 'April', | ||
| 35 | 5 => 'May', | ||
| 36 | 6 => 'June', | ||
| 37 | 7 => 'July', | ||
| 38 | 8 => 'August', | ||
| 39 | 9 => 'September', | ||
| 40 | 10 => 'October', | ||
| 41 | 11 => 'November', | ||
| 42 | 12 => 'December' | ||
| 43 | }, | ||
| 44 | unit => { | ||
| 45 | m => 'minute', | ||
| 46 | h => 'hour', | ||
| 47 | d => 'day', | ||
| 48 | }, | ||
| 49 | return_format => '$quantity $duration ago' | ||
| 50 | }; | ||
| 51 | |||
| 52 | $format_text = $args->[0] if ($args->[0]); | ||
| 53 | |||
| 54 | # Parse date from string, and get delta | ||
| 55 | my $delta = str2time($date1->datetime()) - str2time($date2); | ||
| 56 | |||
| 57 | my $unit = 'n'; | ||
| 58 | my $returnedTxt = ''; | ||
| 59 | |||
| 60 | if($delta < 60){ | ||
| 61 | $delta = 60; | ||
| 62 | } | ||
| 63 | if ( $delta <= 3600 ) { | ||
| 64 | $delta = $delta / 60; | ||
| 65 | $unit = 'm'; | ||
| 66 | } | ||
| 67 | elsif ( $delta <= 86400 ) { | ||
| 68 | $delta = ( $delta / 60 ) / 60; | ||
| 69 | $unit = 'h'; | ||
| 70 | } | ||
| 71 | elsif ( $delta <= 259200 ) { | ||
| 72 | $delta = ( ( $delta / 60 ) / 60 ) / 24; | ||
| 73 | $unit = 'd'; | ||
| 74 | } | ||
| 75 | |||
| 76 | if ( $unit ne 'n' ) { | ||
| 77 | $delta = int( $delta + 0.5 ); | ||
| 78 | } | ||
| 79 | |||
| 80 | # Regular date output | ||
| 81 | if ( $unit eq 'n' ) { | ||
| 82 | my ( $ss, $mm, $hh, $day, $month, $year, $zone ) = strptime($date1); | ||
| 83 | my ( $ss_2, $mm_2, $hh_2, $day_2, $month_2, $year_2, $zone_2 ) = strptime($date2); | ||
| 84 | |||
| 85 | my $monthName = $format_text->{'month'}->{$month_2 + 1}; | ||
| 86 | $returnedTxt = '$day $month $year'; | ||
| 87 | |||
| 88 | $day_2 = int($day_2); | ||
| 89 | my $yearOutput = ''; | ||
| 90 | if ( $year != $year_2 ) { | ||
| 91 | $yearOutput = $year_2 + 1900; | ||
| 92 | } | ||
| 93 | $returnedTxt =~ s/\$day/$day_2/g; | ||
| 94 | $returnedTxt =~ s/\$month/$monthName/g; | ||
| 95 | $returnedTxt =~ s/\$year/$yearOutput/g; | ||
| 96 | } | ||
| 97 | # Human readable date output | ||
| 98 | else { | ||
| 99 | |||
| 100 | $returnedTxt = $format_text->{'return_format'}; | ||
| 101 | |||
| 102 | my $duration = $format_text->{'unit'}->{$unit}; | ||
| 103 | |||
| 104 | # Custom plurals? | ||
| 105 | if ($args->[1] && $args->[1] == 1) { | ||
| 106 | $duration =~ s/\$delta/$delta/g; | ||
| 107 | } | ||
| 108 | else { | ||
| 109 | $duration .= 's' if ($delta == 0 || $delta > 1); | ||
| 110 | } | ||
| 111 | |||
| 112 | $returnedTxt =~ s/\$duration/$duration/g; | ||
| 113 | $returnedTxt =~ s/\$quantity/$delta/g; | ||
| 114 | } | ||
| 115 | |||
| 116 | return ( $returnedTxt ); | ||
| 117 | |||
| 118 | } | ||
| 119 | |||
| 120 | __END__ |