| 1 | package Template::Plugin::Date::FriendlyDelta; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | our $VERSION = '0.01'; | ||
| 6 | |||
| 7 | use Template::Plugin::Filter; | ||
| 8 | use base qw( Template::Plugin::Filter ); | ||
| 9 | |||
| 10 | use DateTime; | ||
| 11 | use Date::Parse; | ||
| 12 | |||
| 13 | sub init { | ||
| 14 | my $self = shift; | ||
| 15 | |||
| 16 | $self->{ _DYNAMIC } = 1; | ||
| 17 | $self->install_filter('friendlydelta'); | ||
| 18 | |||
| 19 | return $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub filter { | ||
| 23 | my ($self, $date2, $args) = @_; | ||
| 24 | |||
| 25 | my $date1 = DateTime->now(); | ||
| 26 | |||
| 27 | # Default strings | ||
| 28 | my $format_text = { | ||
| 29 | month => { | ||
| 30 | 1 => 'January', | ||
| 31 | 2 => 'February', | ||
| 32 | 3 => 'March', | ||
| 33 | 4 => 'April', | ||
| 34 | 5 => 'May', | ||
| 35 | 6 => 'June', | ||
| 36 | 7 => 'July', | ||
| 37 | 8 => 'August', | ||
| 38 | 9 => 'September', | ||
| 39 | 10 => 'October', | ||
| 40 | 11 => 'November', | ||
| 41 | 12 => 'December' | ||
| 42 | }, | ||
| 43 | unit => { | ||
| 44 | m => 'minute', | ||
| 45 | h => 'hour', | ||
| 46 | d => 'day', | ||
| 47 | }, | ||
| 48 | return_format => '$quantity $duration ago' | ||
| 49 | }; | ||
| 50 | |||
| 51 | $format_text = $args->[0] if ($args->[0]); | ||
| 52 | |||
| 53 | # Parse date from string, and get delta | ||
| 54 | my $delta = str2time($date1->datetime()) - str2time($date2); | ||
| 55 | |||
| 56 | my $unit = 'n'; | ||
| 57 | my $returnedTxt = ''; | ||
| 58 | |||
| 59 | if($delta < 60){ | ||
| 60 | $delta = 60; | ||
| 61 | } | ||
| 62 | if ( $delta <= 3600 ) { | ||
| 63 | $delta = $delta / 60; | ||
| 64 | $unit = 'm'; | ||
| 65 | } | ||
| 66 | elsif ( $delta <= 86400 ) { | ||
| 67 | $delta = ( $delta / 60 ) / 60; | ||
| 68 | $unit = 'h'; | ||
| 69 | } | ||
| 70 | elsif ( $delta <= 259200 ) { | ||
| 71 | $delta = ( ( $delta / 60 ) / 60 ) / 24; | ||
| 72 | $unit = 'd'; | ||
| 73 | } | ||
| 74 | |||
| 75 | if ( $unit ne 'n' ) { | ||
| 76 | $delta = int( $delta + 0.5 ); | ||
| 77 | } | ||
| 78 | |||
| 79 | # Regular date output | ||
| 80 | if ( $unit eq 'n' ) { | ||
| 81 | my ( $ss, $mm, $hh, $day, $month, $year, $zone ) = strptime($date1); | ||
| 82 | my ( $ss_2, $mm_2, $hh_2, $day_2, $month_2, $year_2, $zone_2 ) = strptime($date2); | ||
| 83 | |||
| 84 | my $monthName = $format_text->{'month'}->{$month_2 + 1}; | ||
| 85 | $returnedTxt = '$day $month $year'; | ||
| 86 | |||
| 87 | $day_2 = int($day_2); | ||
| 88 | my $yearOutput = ''; | ||
| 89 | if ( $year != $year_2 ) { | ||
| 90 | $yearOutput = $year_2 + 1900; | ||
| 91 | } | ||
| 92 | $returnedTxt =~ s/\$day/$day_2/g; | ||
| 93 | $returnedTxt =~ s/\$month/$monthName/g; | ||
| 94 | $returnedTxt =~ s/\$year/$yearOutput/g; | ||
| 95 | } | ||
| 96 | # Human readable date output | ||
| 97 | else { | ||
| 98 | |||
| 99 | $returnedTxt = $format_text->{'return_format'}; | ||
| 100 | |||
| 101 | my $duration = $format_text->{'unit'}->{$unit}; | ||
| 102 | |||
| 103 | # Custom plurals? | ||
| 104 | if ($args->[1] && $args->[1] == 1) { | ||
| 105 | $duration =~ s/\$delta/$delta/g; | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | $duration .= 's' if ($delta == 0 || $delta > 1); | ||
| 109 | } | ||
| 110 | |||
| 111 | $returnedTxt =~ s/\$duration/$duration/g; | ||
| 112 | $returnedTxt =~ s/\$quantity/$delta/g; | ||
| 113 | } | ||
| 114 | |||
| 115 | return ( $returnedTxt ); | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | __END__ | ||
| 120 | |||
| 121 | =head1 NAME | ||
| 122 | |||
| 123 | Template::Plugin::FriendlyDelta - A Template Toolkit plugin filter to show a human friendly time delta between a supplied date and now | ||
| 124 | |||
| 125 | =head1 SYNOPSIS | ||
| 126 | |||
| 127 | In your template: | ||
| 128 | [% USE FriendlyDelta %] | ||
| 129 | |||
| 130 | [% '2014-09-09T12:23:07' | friendlydelta %] | ||
| 131 | |||
| 132 | or | ||
| 133 | |||
| 134 | [% '2014-09-09T12:23:07' | friendlydelta($custom_format_hash) %] | ||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | =head1 DESCRIPTION | ||
| 140 | |||
| 141 | See above for use in your templates. You can also pass two arguments: a hash to change the display, and a modifier for | ||
| 142 | when you want to use custom plurals. | ||
| 143 | |||
| 144 | Structure of the format / display hash: | ||
| 145 | my $format_hash = { | ||
| 146 | month => { | ||
| 147 | 1 => 'January', | ||
| 148 | 2 => 'February', | ||
| 149 | 3 => 'March', | ||
| 150 | 4 => 'April', | ||
| 151 | 5 => 'May', | ||
| 152 | 6 => 'June', | ||
| 153 | 7 => 'July', | ||
| 154 | 8 => 'August', | ||
| 155 | 9 => 'September', | ||
| 156 | 10 => 'October', | ||
| 157 | 11 => 'November', | ||
| 158 | 12 => 'December' | ||
| 159 | }, | ||
| 160 | unit => { | ||
| 161 | m => 'minute', | ||
| 162 | h => 'hour', | ||
| 163 | d => 'day', | ||
| 164 | }, | ||
| 165 | return_format => '$quantity $duration ago' | ||
| 166 | } | ||
| 167 | |||
| 168 | If you do your own string replacement for plurals in $format_hash->{'return_format'}, pass 1 after: | ||
| 169 | [% '2014-09-09T12:23:07' | friendlydelta($format_hash, 1) %] | ||
| 170 | |||
| 171 | |||
| 172 | =head1 AUTHOR | ||
| 173 | |||
| 174 | Albert Cornelissen, E<lt>acorn@cpan.orgE<gt> | ||
| 175 | |||
| 176 | =head1 COPYRIGHT AND LICENSE | ||
| 177 | |||
| 178 | Copyright (C) 2014 by Albert Cornelissen | ||
| 179 | |||
| 180 | This library is free software; you can redistribute it and/or modify | ||
| 181 | it under the same terms as Perl itself, either Perl version 5.14.2 or, | ||
| 182 | at your option, any later version of Perl 5 you may have available. | ||
| 183 | |||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | 1; |