| 1 |
NAME |
| 2 |
DateTime::Format::Human::Duration - Get a locale specific string |
| 3 |
describing the span of a given duration |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
use DateTime; |
| 7 |
use DateTime::Format::Human::Duration |
| 8 |
|
| 9 |
my $span = DateTime::Format::Human::Duration->new(); |
| 10 |
my $dur = $dta - $dtb; |
| 11 |
print $span->format_duration($dur); # 1 year, 2 months, 3 minutes, and 1 second |
| 12 |
|
| 13 |
print $span->format_duration_between($dta, $dtb); # 1 year, 2 months, 3 minutes, and 1 second |
| 14 |
|
| 15 |
DESCRIPTION |
| 16 |
Get a localized string representing the duration. |
| 17 |
|
| 18 |
For example: |
| 19 |
|
| 20 |
1 second |
| 21 |
2 minutes and 3 seconds |
| 22 |
3 weeks, 1 day, and 5 seconds |
| 23 |
4 years, 1 month, 2 days, 6 minutes, 1 second, and 345000028 nanoseconds |
| 24 |
|
| 25 |
INTERFACE |
| 26 |
new() |
| 27 |
Create span object, no args |
| 28 |
|
| 29 |
format_duration() |
| 30 |
First argument is a DateTime::Duration object |
| 31 |
|
| 32 |
After that you can optionally pass some standard args as a hash as |
| 33 |
described below |
| 34 |
|
| 35 |
format_duration_between() |
| 36 |
First two args are DateTime objects |
| 37 |
|
| 38 |
After that you can optionally pass some standard args as a hash as |
| 39 |
described below |
| 40 |
|
| 41 |
standard args |
| 42 |
1. 'locale' |
| 43 |
locale of the $dt object will be used if you do not specify this |
| 44 |
|
| 45 |
Valid values are a string of the locale (E.g 'fr'), a DateTime |
| 46 |
object, or a DateTime object's 'locale' key. |
| 47 |
|
| 48 |
2. Since we're working with 2 DateTime objects of known points we can |
| 49 |
have past and future tenses. |
| 50 |
* past |
| 51 |
String to use if duration is past tense. Can have a sprintf '%s' |
| 52 |
or else is prepended with a trailing space. |
| 53 |
|
| 54 |
* future |
| 55 |
String to use if duration is future tense. Can have a sprintf |
| 56 |
'%s' or else is prepended with a trailing space. |
| 57 |
|
| 58 |
* no_time |
| 59 |
Override the 'no_time' in the locale hash. |
| 60 |
|
| 61 |
If duration is baseless (IE ambiguous) then 'past' and 'future' is |
| 62 |
used based on if $dur->in_units has negatives or not. |
| 63 |
|
| 64 |
Also by nature it's not split into type groups: |
| 65 |
|
| 66 |
An example is |
| 67 |
|
| 68 |
DateTime::Duration->new('seconds'=> 62) |
| 69 |
|
| 70 |
Will result in '62 seconds' not '1 minute and 2 seconds' |
| 71 |
|
| 72 |
For more sane results always be specific by using 2 datetime object |
| 73 |
to get a duration object |
| 74 |
|
| 75 |
print $dt->format_duration_between( |
| 76 |
$dta, |
| 77 |
$dtb, |
| 78 |
'past' => 'Your account expired %s ago.', |
| 79 |
'future' => 'Your account expires in %s.', |
| 80 |
'no_time'=> 'Your account just expired.', |
| 81 |
); |
| 82 |
|
| 83 |
This facilitates, for example, this Locale::Maketext vernacular |
| 84 |
which becomes: |
| 85 |
|
| 86 |
'Your account [duration,_1,_2,expired %s ago,expires in,just expired].' => '[Votre compte [duration,_1,_2,a expiré il ya,expire dans,vient d'expirer].' |
| 87 |
|
| 88 |
3. Time Resolution and Units |
| 89 |
* units |
| 90 |
Specify units to format duration with. Arguments will be passed |
| 91 |
to DateTime::Format's in_unit() method. |
| 92 |
|
| 93 |
Example: |
| 94 |
|
| 95 |
my $fmt = DateTime::Format::Human::Duration->new(); |
| 96 |
my $d = DateTime::Duration->new(...); |
| 97 |
|
| 98 |
my $s = $fmt->format_duration($d, 'units' => [qw/years months days/] ); |
| 99 |
# $s == '1 year, 7 months, and 16 days' |
| 100 |
|
| 101 |
Possible values include: years months weeks days hours minutes |
| 102 |
seconds nanoseconds |
| 103 |
|
| 104 |
* precision |
| 105 |
By default, the duration will be formatted using nanosecond |
| 106 |
resolution. Resolution can be reduced by passing 'years', |
| 107 |
'months', 'weeks', 'days', 'hours', 'minutes', or 'seconds' to |
| 108 |
the 'precision' argument. |
| 109 |
|
| 110 |
Example: |
| 111 |
|
| 112 |
my $fmt = DateTime::Format::Human::Duration->new(); |
| 113 |
my $d = DateTime::Duration->new(...); |
| 114 |
|
| 115 |
print $fmt->format_duration($d); |
| 116 |
# '1 year, 7 months, 2 weeks, 2 days, 13 hours, 43 minutes, and 15 seconds' |
| 117 |
|
| 118 |
print $fmt->format_duration($d, 'precision' => 'days'); |
| 119 |
# '1 year, 7 months, 2 weeks, and 2 days' |
| 120 |
|
| 121 |
* significant_units |
| 122 |
By default, the duration will be formatted using all specified |
| 123 |
units. To restrict the number of units output, set this to a |
| 124 |
value of one or more. |
| 125 |
|
| 126 |
Example: |
| 127 |
|
| 128 |
my $fmt = DateTime::Format::Human::Duration->new(); |
| 129 |
my $d = DateTime::Duration->new(...); |
| 130 |
|
| 131 |
print $fmt->format_duration($d, 'significant_units' => 1); |
| 132 |
# '3 days' |
| 133 |
print $fmt->format_duration($d, 'significant_units' => 2); |
| 134 |
# '3 days and 10 hours' |
| 135 |
print $fmt->format_duration($d, 'significant_units' => 3); |
| 136 |
# '3 days, 10 hours, and 27 minutes' |
| 137 |
|
| 138 |
LOCALIZATION |
| 139 |
Localization is provided by the included |
| 140 |
DateTime::Format::Human::Duration::Locale modules. |
| 141 |
|
| 142 |
Included are DateTime::Format::Human::Duration::Locale::es, |
| 143 |
DateTime::Format::Human::Duration::Locale::fr, |
| 144 |
DateTime::Format::Human::Duration::Locale::pt, |
| 145 |
DateTime::Format::Human::Duration::Locale::de, |
| 146 |
DateTime::Format::Human::Duration::Locale::it |
| 147 |
|
| 148 |
More will be included as time permits/folks volunteer/CLDR becomes an |
| 149 |
option |
| 150 |
|
| 151 |
They are setup this way: |
| 152 |
|
| 153 |
DateTime::Format::Human::Duration::Locale::XYZ where 'XYZ' is the ISO |
| 154 |
code of DateTime::Locale |
| 155 |
|
| 156 |
It can have one of 2 functions: |
| 157 |
|
| 158 |
get_human_span_hashref() |
| 159 |
Takes no arguments, should return a hashref of this structure: |
| 160 |
|
| 161 |
sub get_human_span_hashref { |
| 162 |
return { |
| 163 |
'no_oxford_comma' => 1, |
| 164 |
'no_time' => 'pas le temps', |
| 165 |
'and' => 'et', |
| 166 |
'year' => 'an', |
| 167 |
'years' => 'ans', |
| 168 |
'month' => 'mois', |
| 169 |
'months' => 'mois', |
| 170 |
'week' => 'semaine', |
| 171 |
'weeks' => 'semaines', |
| 172 |
'day' => 'jour', |
| 173 |
'days' => 'jours', |
| 174 |
'hour' => 'heure', |
| 175 |
'hours' => 'heures', |
| 176 |
'minute' => 'minute', |
| 177 |
'minutes' => 'minutes', |
| 178 |
'second' => 'seconde', |
| 179 |
'seconds' => 'seconds', |
| 180 |
'nanosecond' => 'nanoseconde', |
| 181 |
'nanoseconds' => 'nanosecondes', |
| 182 |
}; |
| 183 |
} |
| 184 |
|
| 185 |
get_human_span_from_units() |
| 186 |
Try to use get_human_span_hashref() if the locale allows for it |
| 187 |
since it's much easier. If you cannot, however, then this will give |
| 188 |
you the maximum level of configurability. |
| 189 |
|
| 190 |
This function receives a hashref of duration values, and a hashref |
| 191 |
of the standard args. It should return the localized string. |
| 192 |
|
| 193 |
sub get_human_span_from_units { |
| 194 |
my ($duration_values_hr, $args_hr) = @_; |
| 195 |
...; |
| 196 |
return $string; # 1 year, 2days, 4 hours, and 17 minutes |
| 197 |
} |
| 198 |
|
| 199 |
Please see the example in |
| 200 |
`t/lib/DateTime/Format/Human/Duration/Locale/nb.pm'. |
| 201 |
|
| 202 |
LOCALIZATION of DateTime::Format modules |
| 203 |
DateTime does an excellent job at implementing localization. Often |
| 204 |
DateTime::Format based class's either don't support localization or they |
| 205 |
implement it haphazardly and inconsistently. |
| 206 |
|
| 207 |
With this module I hope to model a localization scheme that is inline |
| 208 |
with DateTime and is consistent and reuseable between <DateTime::Format> |
| 209 |
based classes. |
| 210 |
|
| 211 |
The idea is to determine the locale to use based on a DateTime object. |
| 212 |
|
| 213 |
XYZ::Locale should handle looking up (and caching if appropriate) the |
| 214 |
locale and loading the necessary locale module XYZ::Locale::fr |
| 215 |
|
| 216 |
The specific locale module holds the data and possibly logic necessary |
| 217 |
to do what XYZ does in the vernacular of the given locale. |
| 218 |
|
| 219 |
TODO |
| 220 |
Eventually the generic logic will be re-broken out into its own module |
| 221 |
for re-use by your class and I'll have more detailed POD about how to do |
| 222 |
it. |
| 223 |
|
| 224 |
In the meantime if you're interested please contact me and I'd be happy |
| 225 |
to help and/or expediate this TODO. |
| 226 |
|
| 227 |
Also, Dave Rolksy has mentioned to me that this sort of locale data |
| 228 |
might be appropriate for DateTime::Locale directly from CLDR. If that |
| 229 |
happens this module will be changed to use that if possible. |
| 230 |
|
| 231 |
FAQ |
| 232 |
Why would I want to use this? |
| 233 |
So you can localize your application's output of time periods without |
| 234 |
having to do a lot of logic each time you wanted to say it. |
| 235 |
|
| 236 |
Locale::Maketext::Utils has/will have a duration() bracket notation |
| 237 |
method which prompted this module's existence |
| 238 |
|
| 239 |
duration() was prompted by its datetime() brother, all of which uses the |
| 240 |
most excellent DateTime project! |
| 241 |
|
| 242 |
Why did my duration say '62 seconds' instead of '1 minute and 2 seconds' |
| 243 |
Because you used an ambiguous duration (one without a base) so there is |
| 244 |
no way to apply date math and accurately represent the number of each |
| 245 |
given item in that duration since it may or may not span leap-[second, |
| 246 |
days, years, etc..] |
| 247 |
|
| 248 |
In other words do this (so that your duration can be specifically |
| 249 |
calculated): |
| 250 |
|
| 251 |
$dtb = $dta->clone->add('seconds'=> 62); |
| 252 |
my $duration = $dta - $dtb; # has a base, its not ambiguous |
| 253 |
print $span->format_duration($duration); # 1 minutes and 2 seconds |
| 254 |
|
| 255 |
not this: |
| 256 |
|
| 257 |
my $duration = DateTime::Duration->new('seconds'=> 62); # no base, it is ambiguous |
| 258 |
print $span->format_duration($duration); # 62 seconds |
| 259 |
|
| 260 |
Note format_duration_between(), does not suffer from this since we're |
| 261 |
using a specific DateTime object already. |
| 262 |
|
| 263 |
print $span->format_duration_between( $dt, $dt->clone()->add('seconds'=> 62) ); # 1 minute and 2 seconds |
| 264 |
|
| 265 |
Why do you put a comma before the 'and' in a group of more than 2 items? |
| 266 |
We want to use the so-called Oxford comma to avoid ambiguity. |
| 267 |
|
| 268 |
My DateTime::Format::Human::Duration::Locale::XX still outputs in English! |
| 269 |
That is because it defined neither the get_human_span_hashref() or the |
| 270 |
get_human_span_from_units() functions |
| 271 |
|
| 272 |
It must define one of them or defaults are used. |
| 273 |
|
| 274 |
Why didn't you just use 'DateTime::Format::Duration' |
| 275 |
Essencially DateTime::Format::Duration is an object representing a |
| 276 |
single strftime() type string to apply to any given duration. This is |
| 277 |
not flexible enough for the intent of this module. |
| 278 |
|
| 279 |
DateTime::Format::Duration is not a bad module its just for a different |
| 280 |
purpose than DateTime::Format::Human::Duration |
| 281 |
|
| 282 |
* It was not localizable |
| 283 |
You either got '2 days' or '1 days' which a) forces it to be in |
| 284 |
English and b) doesn't even make sense in English. |
| 285 |
|
| 286 |
You could get around that by adding logic each time you wanted to |
| 287 |
call it but that is just messy. |
| 288 |
|
| 289 |
* Had to keep an item even if it was zero |
| 290 |
If 'days' was in there you got '0 days', we only want items with a |
| 291 |
value to show. |
| 292 |
|
| 293 |
That'd also require a lot of logic each time you wanted to call |
| 294 |
which is again messy. |
| 295 |
|
| 296 |
* This module has no need for reparsing output back into an object |
| 297 |
Since the datetime info for 2 points in time are generally in a form |
| 298 |
easily rendered into a DateTime object it'd be silly to even attempt |
| 299 |
to store and parse the output of this module back into an object. |
| 300 |
|
| 301 |
Plus since it all depends on the locale it is in it'd be difficult. |
| 302 |
|
| 303 |
The purpose of DateTime::Format::Human::Duration was to generate a |
| 304 |
localized human language description of a duration without the caller |
| 305 |
needing to supply any logic. |
| 306 |
|
| 307 |
DIAGNOSTICS |
| 308 |
Throws no warnings or errors of its own |
| 309 |
|
| 310 |
CONFIGURATION AND ENVIRONMENT |
| 311 |
DateTime::Format::Human::Duration requires no configuration files or |
| 312 |
environment variables. |
| 313 |
|
| 314 |
DEPENDENCIES |
| 315 |
None. |
| 316 |
|
| 317 |
INCOMPATIBILITIES |
| 318 |
None reported. |
| 319 |
|
| 320 |
BUGS AND LIMITATIONS |
| 321 |
No bugs have been reported. |
| 322 |
|
| 323 |
Please report any bugs or feature requests to |
| 324 |
`[email protected]', or through the web interface at |
| 325 |
http://rt.cpan.org. |
| 326 |
|
| 327 |
AUTHOR |
| 328 |
Daniel Muey `<http://drmuey.com/cpan_contact.pl>' |
| 329 |
|
| 330 |
LICENCE AND COPYRIGHT |
| 331 |
Copyright (c) 2008, Daniel Muey `<http://drmuey.com/cpan_contact.pl>'. |
| 332 |
All rights reserved. |
| 333 |
|
| 334 |
This module is free software; you can redistribute it and/or modify it |
| 335 |
under the same terms as Perl itself. See perlartistic. |
| 336 |
|
| 337 |
DISCLAIMER OF WARRANTY |
| 338 |
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
| 339 |
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
| 340 |
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
| 341 |
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
| 342 |
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 343 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
| 344 |
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
| 345 |
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
| 346 |
NECESSARY SERVICING, REPAIR, OR CORRECTION. |
| 347 |
|
| 348 |
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
| 349 |
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
| 350 |
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE |
| 351 |
TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR |
| 352 |
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE |
| 353 |
SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
| 354 |
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
| 355 |
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
| 356 |
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 357 |
DAMAGES. |
| 358 |
|