.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "DateTime::Format::Human::Duration 3pm" .TH DateTime::Format::Human::Duration 3pm "2016-07-24" "perl v5.22.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" DateTime::Format::Human::Duration \- Get a locale specific string describing the span of a given duration .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use DateTime; \& use DateTime::Format::Human::Duration \& \& my $span = DateTime::Format::Human::Duration\->new(); \& my $dur = $dta \- $dtb; \& print $span\->format_duration($dur); # 1 year, 2 months, 3 minutes, and 1 second \& \& print $span\->format_duration_between($dta, $dtb); # 1 year, 2 months, 3 minutes, and 1 second .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Get a localized string representing the duration. .PP For example: .PP .Vb 4 \& 1 second \& 2 minutes and 3 seconds \& 3 weeks, 1 day, and 5 seconds \& 4 years, 1 month, 2 days, 6 minutes, 1 second, and 345000028 nanoseconds .Ve .SH "INTERFACE" .IX Header "INTERFACE" .SS "\fInew()\fP" .IX Subsection "new()" Create span object, no args .SS "\fIformat_duration()\fP" .IX Subsection "format_duration()" First argument is a DateTime::Duration object .PP After that you can optionally pass some \*(L"standard args\*(R" as a hash as described below .SS "\fIformat_duration_between()\fP" .IX Subsection "format_duration_between()" First two args are DateTime objects .PP After that you can optionally pass some \*(L"standard args\*(R" as a hash as described below .SS "standard args" .IX Subsection "standard args" .IP "1. 'locale'" 4 .IX Item "1. 'locale'" locale of the \f(CW$dt\fR object will be used if you do not specify this .Sp Valid values are a string of the locale (E.g 'fr'), a DateTime object, or a DateTime object's 'locale' key. .IP "2. Since we're working with 2 DateTime objects of known points we can have past and future tenses." 4 .IX Item "2. Since we're working with 2 DateTime objects of known points we can have past and future tenses." .RS 4 .PD 0 .IP "\(bu" 4 .PD past .Sp String to use if duration is past tense. Can have a sprintf '%s' or else is prepended with a trailing space. .IP "\(bu" 4 future .Sp String to use if duration is future tense. Can have a sprintf '%s' or else is prepended with a trailing space. .IP "\(bu" 4 no_time .Sp Override the 'no_time' in the locale hash. .RE .RS 4 .Sp If duration is baseless (\s-1IE\s0 ambiguous) then 'past' and 'future' is used based on if \f(CW$dur\fR\->in_units has negatives or not. .Sp Also by nature it's not split into type groups: .Sp An example is .Sp .Vb 1 \& DateTime::Duration\->new(\*(Aqseconds\*(Aq=> 62) .Ve .Sp Will result in '62 seconds' not '1 minute and 2 seconds' .Sp For more sane results always be specific by using 2 datetime object to get a duration object .Sp .Vb 7 \& print $dt\->format_duration_between( \& $dta, \& $dtb, \& \*(Aqpast\*(Aq => \*(AqYour account expired %s ago.\*(Aq, \& \*(Aqfuture\*(Aq => \*(AqYour account expires in %s.\*(Aq, \& \*(Aqno_time\*(Aq=> \*(AqYour account just expired.\*(Aq, \& ); .Ve .Sp This facilitates, for example, this Locale::Maketext vernacular which becomes: .Sp .Vb 1 \& \*(AqYour account [duration,_1,_2,expired %s ago,expires in,just expired].\*(Aq => \*(Aq[Votre compte [duration,_1,_2,a expire\*' il ya,expire dans,vient d\*(Aqexpirer].\*(Aq .Ve .RE .IP "3. Time Resolution and Units" 4 .IX Item "3. Time Resolution and Units" .RS 4 .PD 0 .IP "\(bu" 4 .PD units .Sp Specify units to format duration with. Arguments will be passed to DateTime::Format's \fIin_unit()\fR method. .Sp Example: .Sp .Vb 2 \& my $fmt = DateTime::Format::Human::Duration\->new(); \& my $d = DateTime::Duration\->new(...); \& \& my $s = $fmt\->format_duration($d, \*(Aqunits\*(Aq => [qw/years months days/] ); \& # $s == \*(Aq1 year, 7 months, and 16 days\*(Aq .Ve .Sp Possible values include: years months weeks days hours minutes seconds nanoseconds .IP "\(bu" 4 precision .Sp By default, the duration will be formatted using nanosecond resolution. Resolution can be reduced by passing \&'years', 'months', 'weeks', 'days', 'hours', 'minutes', or 'seconds' to the 'precision' argument. .Sp Example: .Sp .Vb 2 \& my $fmt = DateTime::Format::Human::Duration\->new(); \& my $d = DateTime::Duration\->new(...); \& \& print $fmt\->format_duration($d); \& # \*(Aq1 year, 7 months, 2 weeks, 2 days, 13 hours, 43 minutes, and 15 seconds\*(Aq \& \& print $fmt\->format_duration($d, \*(Aqprecision\*(Aq => \*(Aqdays\*(Aq); \& # \*(Aq1 year, 7 months, 2 weeks, and 2 days\*(Aq .Ve .IP "\(bu" 4 significant_units .Sp By default, the duration will be formatted using all specified units. To restrict the number of units output, set this to a value of one or more. .Sp Example: .Sp .Vb 2 \& my $fmt = DateTime::Format::Human::Duration\->new(); \& my $d = DateTime::Duration\->new(...); \& \& print $fmt\->format_duration($d, \*(Aqsignificant_units\*(Aq => 1); \& # \*(Aq3 days\*(Aq \& print $fmt\->format_duration($d, \*(Aqsignificant_units\*(Aq => 2); \& # \*(Aq3 days and 10 hours\*(Aq \& print $fmt\->format_duration($d, \*(Aqsignificant_units\*(Aq => 3); \& # \*(Aq3 days, 10 hours, and 27 minutes\*(Aq .Ve .RE .RS 4 .RE .SH "LOCALIZATION" .IX Header "LOCALIZATION" Localization is provided by the included DateTime::Format::Human::Duration::Locale modules. .PP Included are DateTime::Format::Human::Duration::Locale::es, DateTime::Format::Human::Duration::Locale::fr, DateTime::Format::Human::Duration::Locale::pt, DateTime::Format::Human::Duration::Locale::de, DateTime::Format::Human::Duration::Locale::it .PP More will be included as time permits/folks volunteer/CLDR becomes an option .PP They are setup this way: .PP DateTime::Format::Human::Duration::Locale::XYZ where '\s-1XYZ\s0' is the \s-1ISO\s0 code of DateTime::Locale .PP It can have one of 2 functions: .IP "\fIget_human_span_hashref()\fR" 4 .IX Item "get_human_span_hashref()" Takes no arguments, should return a hashref of this structure: .Sp .Vb 10 \& sub get_human_span_hashref { \& return { \& \*(Aqno_oxford_comma\*(Aq => 1, \& \*(Aqno_time\*(Aq => \*(Aqpas le temps\*(Aq, \& \*(Aqand\*(Aq => \*(Aqet\*(Aq, \& \*(Aqyear\*(Aq => \*(Aqan\*(Aq, \& \*(Aqyears\*(Aq => \*(Aqans\*(Aq, \& \*(Aqmonth\*(Aq => \*(Aqmois\*(Aq, \& \*(Aqmonths\*(Aq => \*(Aqmois\*(Aq, \& \*(Aqweek\*(Aq => \*(Aqsemaine\*(Aq, \& \*(Aqweeks\*(Aq => \*(Aqsemaines\*(Aq, \& \*(Aqday\*(Aq => \*(Aqjour\*(Aq, \& \*(Aqdays\*(Aq => \*(Aqjours\*(Aq, \& \*(Aqhour\*(Aq => \*(Aqheure\*(Aq, \& \*(Aqhours\*(Aq => \*(Aqheures\*(Aq, \& \*(Aqminute\*(Aq => \*(Aqminute\*(Aq, \& \*(Aqminutes\*(Aq => \*(Aqminutes\*(Aq, \& \*(Aqsecond\*(Aq => \*(Aqseconde\*(Aq, \& \*(Aqseconds\*(Aq => \*(Aqseconds\*(Aq, \& \*(Aqnanosecond\*(Aq => \*(Aqnanoseconde\*(Aq, \& \*(Aqnanoseconds\*(Aq => \*(Aqnanosecondes\*(Aq, \& }; \& } .Ve .IP "\fIget_human_span_from_units()\fR" 4 .IX Item "get_human_span_from_units()" Try to use \*(L"\fIget_human_span_hashref()\fR\*(R" if the locale allows for it since it's much easier. If you cannot, however, then this will give you the maximum level of configurability. .Sp This function receives a hashref of duration values, and a hashref of the \*(L"standard args\*(R". It should return the localized string. .Sp .Vb 5 \& sub get_human_span_from_units { \& my ($duration_values_hr, $args_hr) = @_; \& ...; \& return $string; # 1 year, 2days, 4 hours, and 17 minutes \& } .Ve .Sp Please see the example in \f(CW\*(C`t/lib/DateTime/Format/Human/Duration/Locale/nb.pm\*(C'\fR. .SH "LOCALIZATION of DateTime::Format modules" .IX Header "LOCALIZATION of DateTime::Format modules" DateTime does an excellent job at implementing localization. Often DateTime::Format based class's either don't support localization or they implement it haphazardly and inconsistently. .PP With this module I hope to model a localization scheme that is inline with DateTime and is consistent and reuseable between based classes. .PP The idea is to determine the locale to use based on a DateTime object. .PP XYZ::Locale should handle looking up (and caching if appropriate) the locale and loading the necessary locale module XYZ::Locale::fr .PP The specific locale module holds the data and possibly logic necessary to do what \s-1XYZ\s0 does in the vernacular of the given locale. .SS "\s-1TODO \s0" .IX Subsection "TODO " Eventually the generic logic will be re-broken out into its own module for re-use by your class and I'll have more detailed \s-1POD\s0 about how to do it. .PP In the meantime if you're interested please contact me and I'd be happy to help and/or expediate this \s-1TODO.\s0 .PP Also, Dave Rolksy has mentioned to me that this sort of locale data might be appropriate for DateTime::Locale directly from \s-1CLDR.\s0 If that happens this module will be changed to use that if possible. .SH "FAQ" .IX Header "FAQ" .SS "Why would I want to use this?" .IX Subsection "Why would I want to use this?" So you can localize your application's output of time periods without having to do a lot of logic each time you wanted to say it. .PP Locale::Maketext::Utils has/will have a \fIduration()\fR bracket notation method which prompted this module's existence .PP \&\fIduration()\fR was prompted by its \fIdatetime()\fR brother, all of which uses the most excellent DateTime project! .SS "Why did my duration say '62 seconds' instead of '1 minute and 2 seconds'" .IX Subsection "Why did my duration say '62 seconds' instead of '1 minute and 2 seconds'" Because you used an ambiguous duration (one without a base) so there is no way to apply date math and accurately represent the number of each given item in that duration since it may or may not span leap\-[second, days, years, etc..] .PP In other words do this (so that your duration can be specifically calculated): .PP .Vb 3 \& $dtb = $dta\->clone\->add(\*(Aqseconds\*(Aq=> 62); \& my $duration = $dta \- $dtb; # has a base, its not ambiguous \& print $span\->format_duration($duration); # 1 minutes and 2 seconds .Ve .PP not this: .PP .Vb 2 \& my $duration = DateTime::Duration\->new(\*(Aqseconds\*(Aq=> 62); # no base, it is ambiguous \& print $span\->format_duration($duration); # 62 seconds .Ve .PP Note \*(L"format_duration_between\*(R"(), does not suffer from this since we're using a specific DateTime object already. .PP .Vb 1 \& print $span\->format_duration_between( $dt, $dt\->clone()\->add(\*(Aqseconds\*(Aq=> 62) ); # 1 minute and 2 seconds .Ve .SS "Why do you put a comma before the 'and' in a group of more than 2 items?" .IX Subsection "Why do you put a comma before the 'and' in a group of more than 2 items?" We want to use the so-called Oxford comma to avoid ambiguity. .SS "My DateTime::Format::Human::Duration::Locale::XX still outputs in English!" .IX Subsection "My DateTime::Format::Human::Duration::Locale::XX still outputs in English!" That is because it defined neither the \*(L"\fIget_human_span_hashref()\fR\*(R" or the \*(L"\fIget_human_span_from_units()\fR\*(R" functions .PP It must define one of them or defaults are used. .SS "Why didn't you just use 'DateTime::Format::Duration'" .IX Subsection "Why didn't you just use 'DateTime::Format::Duration'" Essencially DateTime::Format::Duration is an object representing a single \fIstrftime()\fR type string to apply to any given duration. This is not flexible enough for the intent of this module. .PP DateTime::Format::Duration is not a bad module its just for a different purpose than DateTime::Format::Human::Duration .IP "\(bu" 4 It was not localizable .Sp You either got '2 days' or '1 days' which a) forces it to be in English and b) doesn't even make sense in English. .Sp You could get around that by adding logic each time you wanted to call it but that is just messy. .IP "\(bu" 4 Had to keep an item even if it was zero .Sp If 'days' was in there you got '0 days', we only want items with a value to show. .Sp That'd also require a lot of logic each time you wanted to call which is again messy. .IP "\(bu" 4 This module has no need for reparsing output back into an object .Sp Since the datetime info for 2 points in time are generally in a form easily rendered into a DateTime object it'd be silly to even attempt to store and parse the output of this module back into an object. .Sp Plus since it all depends on the locale it is in it'd be difficult. .PP The purpose of DateTime::Format::Human::Duration was to generate a localized human language description of a duration without the caller needing to supply any logic. .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" Throws no warnings or errors of its own .SH "CONFIGURATION AND ENVIRONMENT" .IX Header "CONFIGURATION AND ENVIRONMENT" DateTime::Format::Human::Duration requires no configuration files or environment variables. .SH "DEPENDENCIES" .IX Header "DEPENDENCIES" None. .SH "INCOMPATIBILITIES" .IX Header "INCOMPATIBILITIES" None reported. .SH "BUGS AND LIMITATIONS" .IX Header "BUGS AND LIMITATIONS" No bugs have been reported. .PP Please report any bugs or feature requests to \&\f(CW\*(C`bug\-datetime\-format\-span@rt.cpan.org\*(C'\fR, or through the web interface at . .SH "AUTHOR" .IX Header "AUTHOR" Daniel Muey \f(CW\*(C`\*(C'\fR .SH "LICENCE AND COPYRIGHT" .IX Header "LICENCE AND COPYRIGHT" Copyright (c) 2008, Daniel Muey \f(CW\*(C`\*(C'\fR. All rights reserved. .PP This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. .SH "DISCLAIMER OF WARRANTY" .IX Header "DISCLAIMER OF WARRANTY" \&\s-1BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE \*(L"AS IS\*(R" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.\s0 .PP \&\s-1IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE \s0(\s-1INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE\s0), \s-1EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\s0