jacobydave,
@jacobydave@mastodon.xyz avatar

Finding an issue with (either my current situation or) Perl's DateTime::Format::Duration module.

I create 2 DT objects, display via DT::F::MySQL and

my $d = DateTime::Format::Duration->new(
pattern => '%Y years, %m months, %e days, '
. '%H hours, %M minutes, %S seconds' );

and get

1975-01-01 08:05:00
1993-12-13 17:40:17
0 years, 227 months, 12 days, 00 hours, 575 minutes, 17 seconds

Shouldn't there be days and hours, instead of >12 months and >60 minutes?

mjgardner, (edited )
@mjgardner@social.sdf.org avatar

@jacobydave Without a base DateTime object or enabling normalization, 's DateTime::Format::Duration has no reliable way to combine certain units into larger ones. See https://metacpan.org/pod/DateTime::Format::Duration#Normalisation for a full discussion.

Here’s a version that uses the first DateTime as a base. It should output:

1975-01-01 08:05:00
1993-12-13 17:40:17
18 years, 11 months, 12 days, 09 hours, 35 minutes, 17 seconds

jacobydave,
@jacobydave@mastodon.xyz avatar

@mjgardner Thanks again. I used base => DateTime->now, and that worked.

I saw the use statements in the top of the code, but I haven't dived in to see what they're doing yet. Your sample really helped. especially DateTime->new( formatter => 'Whatever' ). I don't think I've ever tried just printing a DT object before.

mjgardner,
@mjgardner@social.sdf.org avatar

@jacobydave Yah, DateTime objects will do the right thing (they “overload stringification” to call the stringify method) when interpolated into double quotes or the qq function (https://perldoc.perl.org/perlop#qq/STRING/)

By default, DateTime objects call the iso8601 method when interpolated or calling stringify, but you can set formatter(or set_formatter later) to any class you’ve used with a format_datetime method. That’s documented here: https://metacpan.org/pod/DateTime#$dt-%3Estringify

jacobydave,
@jacobydave@mastodon.xyz avatar

@mjgardner iso8601, or as we programmers call it, "correct". B)

I appreciate House Absolute more every time I use DT.

mjgardner,
@mjgardner@social.sdf.org avatar

@jacobydave DateTime's iso8601 is okay but it doesn't output time zone. You can find more complete parser/formatter classes on .

For scripts that need to run quickly and with low memory, you can also try @xdg's https://metacpan.org/pod/DateTime::Tiny, which provides a subset of the DateTime API for immutable objects while enabling you to "inflate" them to full DateTimes as needed

mjgardner, (edited )
@mjgardner@social.sdf.org avatar

@jacobydave Also, we all know from that April 25 is the perfect date: https://youtube.com/watch?v=-BNwiqDGz5g

#!/usr/bin/env perl

use Date::Extract;
use DateTime::Format::ISO8601::Format;

local $/;
print DateTime::Format::ISO8601::Format->new->format_date(
Date::Extract->new->extract(<DATA>) ), "\n";

END
I'd have to say April 25 because it's not too hot and not too cold.
All you need is a light jacket!

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • ethstaker
  • DreamBathrooms
  • GTA5RPClips
  • magazineikmin
  • InstantRegret
  • cubers
  • thenastyranch
  • Youngstown
  • rosin
  • slotface
  • osvaldo12
  • ngwrru68w68
  • kavyap
  • everett
  • megavids
  • Durango
  • normalnudes
  • Leos
  • mdbf
  • khanakhh
  • tester
  • modclub
  • cisconetworking
  • anitta
  • tacticalgear
  • provamag3
  • JUstTest
  • lostlight
  • All magazines