DateTi...uration(3pm)
πΈ DateTime::Format::Human::Duration(3pm) β the "how long was that, bestie?" page π
ok so you're reading the manual for DateTime::Format::Human::Duration(3pm), and honestly? it's giving β¨words for time spansβ¨. Let's get into it π
β¨ NAME β¨
DateTime::Format::Human::Duration β get a locale specific string describing the span of a given duration. Like, it takes your boring numbers and turns them into "1 year, 2 months, 3 minutes, and 1 second" π¦ no cap.
π SYNOPSIS π
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
literally that's it. two lines of vibes and you're slaying π
π DESCRIPTION π
You feed it a duration, it hands you back a localized string that describes that duration. Periodt.
For example, you get stuff like this:
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
like, 345000028 nanoseconds?? that's so specific of it π but we love precision.
π«Ά INTERFACE π«Ά
π
new()
Makes you a span object. No args, no drama, no notes. Just vibes.
π
format_duration()
First argument is a DateTime::Duration object, bestie.
After that you can optionally pass some "standard args" as a hash, described down below π
π
format_duration_between()
First two args are DateTime objects.
After that you can optionally pass some "standard args" as a hash, also described down below π
π standard args π
ok this is the main character section, pay attention:
- 'locale'
The locale of the $dt object gets used if you don't specify this. It's giving "we already know, don't worry about it" π
Valid values are a string of the locale (E.g 'fr'), a DateTime object, or a DateTime object's 'locale' key.
- Since we're working with 2
DateTimeobjects of known points, we can have past and future tenses. So iconic of us honestly π°οΈ- past
String to use if the duration is past tense. Can have a sprintf '%s' or else it is prepended with a trailing space.
- future
String to use if the duration is future tense. Can have a sprintf '%s' or else it is prepended with a trailing space.
- no_time
Override the 'no_time' in the locale hash.
If the duration is baseless (IE ambiguous) then 'past' and 'future' get used based on whether $dur->in_units has negatives or not.
Also, by nature it's not split into type groups:
An example is
DateTime::Duration->new('seconds'=> 62)
Will result in '62 seconds' not '1 minute and 2 seconds' π so like, rude but okay.
For more sane results always be specific by using 2 datetime objects to get a duration object:
print $dt->format_duration_between(
$dta,
$dtb,
'past' => 'Your account expired %s ago.',
'future' => 'Your account expires in %s.',
'no_time'=> 'Your account just expired.',
);
This facilitates, for example, this Locale::Maketext vernacular which becomes:
'Your account [duration,_1,_2,expired %s ago,expires in,just expired].' => '[Votre compte [duration,_1,_2,a expire il ya,expire dans,vient d'expirer].'
- Time Resolution and Units
- units
Specify the units to format the duration with. Arguments will be passed to DateTime::Format's in_unit() method.
Example:
my $fmt = DateTime::Format::Human::Duration->new();
my $d = DateTime::Duration->new(...);
my $s = $fmt->format_duration($d, 'units' => [qw/years months days/] );
# $s == '1 year, 7 months, and 16 days'
Possible values include: years months weeks days hours minutes seconds nanoseconds
- precision
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.
Example:
my $fmt = DateTime::Format::Human::Duration->new();
my $d = DateTime::Duration->new(...);
print $fmt->format_duration($d);
# '1 year, 7 months, 2 weeks, 2 days, 13 hours, 43 minutes, and 15 seconds'
print $fmt->format_duration($d, 'precision' => 'days');
# '1 year, 7 months, 2 weeks, and 2 days'
- significant_units
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.
Example:
my $fmt = DateTime::Format::Human::Duration->new();
my $d = DateTime::Duration->new(...);
print $fmt->format_duration($d, 'significant_units' => 1);
# '3 days'
print $fmt->format_duration($d, 'significant_units' => 2);
# '3 days and 10 hours'
print $fmt->format_duration($d, 'significant_units' => 3);
# '3 days, 10 hours, and 27 minutes'
ngl significant_units is the "just give me the top 3, I don't need your whole life story" option and I respect it π«‘
π LOCALIZATION π
Localization is provided by the included DateTime::Format::Human::Duration::Locale modules. π
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
More will be included as time permits/folks volunteer/CLDR becomes an option π
They are setup this way:
DateTime::Format::Human::Duration::Locale::XYZ where 'XYZ' is the ISO code of DateTime::Locale
It can have one of 2 functions:
π get_human_span_hashref()
Takes no arguments, should return a hashref of this structure:
sub get_human_span_hashref {
return {
'no_oxford_comma' => 1,
'no_time' => 'pas le temps',
'and' => 'et',
'year' => 'an',
'years' => 'ans',
'month' => 'mois',
'months' => 'mois',
'week' => 'semaine',
'weeks' => 'semaines',
'day' => 'jour',
'days' => 'jours',
'hour' => 'heure',
'hours' => 'heures',
'minute' => 'minute',
'minutes' => 'minutes',
'second' => 'seconde',
'seconds' => 'seconds',
'nanosecond' => 'nanoseconde',
'nanoseconds' => 'nanosecondes',
};
}
π get_human_span_from_units()
Try to use "get_human_span_hashref()" 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.
This function receives a hashref of duration values, and a hashref of the "standard args". It should return the localized string.
sub get_human_span_from_units {
my ($duration_values_hr, $args_hr) = @_;
...;
return $string; # 1 year, 2days, 4 hours, and 17 minutes
}
Please see the example in "t/lib/DateTime/Format/Human/Duration/Locale/nb.pm".
π 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. π
With this module I hope to model a localization scheme that is inline with DateTime and is consistent and reuseable between <DateTime::Format> based classes.
The idea is to determine the locale to use based on a DateTime object.
XYZ::Locale should handle looking up (and caching if appropriate) the locale and loading the necessary locale module XYZ::Locale::fr
The specific locale module holds the data and possibly logic necessary to do what XYZ does in the vernacular of the given locale.
π¦ 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 POD about how to do it.
In the meantime if you're interested please contact me and I'd be happy to help and/or expediate this TODO.
Also, Dave Rolksy has mentioned to me that this sort of locale data might be appropriate for DateTime::Locale directly from CLDR. If that happens this module will be changed to use that if possible. β¨
β¨ FAQ β¨
π Why would I want to use this?
So you can localize your application's output of time periods without having to do a whole pile of logic every single time you wanna say it. π
Locale::Maketext::Utils has/will have a duration() bracket notation method which prompted this module's existence.
And duration() was prompted by its datetime() brother β all of which uses the most excellent DateTime project! π₯
π 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. π«
In other words do this (so that your duration can be specifically calculated):
$dtb = $dta->clone->add('seconds'=> 62);
my $duration = $dta - $dtb; # has a base, its not ambiguous
print $span->format_duration($duration); # 1 minutes and 2 seconds
not this:
my $duration = DateTime::Duration->new('seconds'=> 62); # no base, it is ambiguous
print $span->format_duration($duration); # 62 seconds
Note: format_duration_between() does not suffer from this, since we're using a specific DateTime object already. π
print $span->format_duration_between( $dt, $dt->clone()->add('seconds'=> 62) ); # 1 minute and 2 seconds
π 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. It's giving clarity, bestie. β¨
π My DateTime::Format::Human::Duration::Locale::XX still outputs in English!
That is because it defined neither the get_human_span_hashref() nor the get_human_span_from_units() functions.
It must define one of them or defaults are used. Periodt. π
π Why didn't you just use 'DateTime::Format::Duration'
Essencially DateTime::Format::Duration is an object representing a single strftime() type string to apply to any given duration. This is not flexible enough for the intent of this module. π¦
DateTime::Format::Duration is not a bad module, it's just for a different purpose than DateTime::Format::Human::Duration.
- It was not localizable π
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.
You could get around that by adding logic each time you wanted to call it but that is just messy.
- Had to keep an item even if it was zero π
If 'days' was in there you got '0 days', we only want items with a value to show.
That'd also require a lot of logic each time you wanted to call which is again messy.
- This module has no need for reparsing output back into an object π
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.
Plus since it all depends on the locale it is in it'd be difficult.
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. Main character module energy, fr fr. π
β DIAGNOSTICS β
Throws no warnings or errors of its own. It's chill like that. π
π CONFIGURATION AND ENVIRONMENT π
DateTime::Format::Human::Duration requires no configuration files or environment variables. Zero setup, just vibes. β¨
πΈ DEPENDENCIES πΈ
None.
π¦ INCOMPATIBILITIES π¦
None reported.
π BUGS AND LIMITATIONS π
No bugs have been reported.
Please report any bugs or feature requests to bug-datetime-format-span@rt.cpan.org, or through the web interface at <http://rt.cpan.org>.
π AUTHOR π
Daniel Muey "<http://drmuey.com/cpan_contact.pl>"
π LICENCE AND COPYRIGHT π
Copyright (c) 2008, Daniel Muey "<http://drmuey.com/cpan_contact.pl>". All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
π«Ά DISCLAIMER OF WARRANTY π«Ά
Okay so legally, here's the deal, and it's ALL CAPS SERIOUS:
BECAUSE 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 "AS IS" 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.
IN 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 (INCLUDING 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), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. π
And that's the whole page, bestie! π This one's running on perl v5.34.0, and the doc itself is dated 2022-06-13 β that's the DateTi...uration(3pm) footer energy, wrapped in a bow. πβ¨
π¦ where this page comes from
- debian bookworm: libdatetime-format-human-duration-perl 0.64-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libdatetime-format-human-duration-perl 0.64-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libdatetime-format-human-duration-perl 0.64-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdatetime-format-human-duration-perl 0.64-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdatetime-format-human-duration-perl 0.64-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms