[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Simplifying Time?



If you use DateTime instead of Class::Date, there's the DateTime::Duration class which will let you add durations:

my $duration = DateTime::Duration->new( seconds => 15 );
$duration->add( seconds => 73 );
$duration->add( weeks => 12 );

There are some modules to help with formatting durations too. I think the nicest of which is this one:

https://metacpan.org/pod/DateTime::Format::Human::Duration

It's a pretty nice module.


On Thu, Mar 20, 2014 at 11:23 AM, Jay Hannah <jay@jays.net> wrote:
On Mar 20, 2014, at 11:06 AM, Robert L. Harris <robert.l.harris@gmail.com> wrote:
> It looks like the reldate is what I want, not figure out how to use it.  No clear examples on "convert 561 seconds to 9 minutes, 21 seconds" :)

Does this help?

https://github.com/jhannah/sandbox/blob/master/robertlharris/go.pl


$ cat go.pl
use Class::Date;
use 5.10.0;

my $reldate = new Class::Date::Rel "561s";
say "In minutes: " . $reldate->min;
say "In seconds: " . $reldate->sec;

my $seconds_only = $reldate - (int($reldate->min) * 60 . 'm');  # subtract minutes away
say "Seconds only: $seconds_only";
printf("%d minutes %d seconds\n", int($reldate / 60), $seconds_only);


$ perl go.pl
In minutes: 9.35
In seconds: 561
Seconds only: 21
9 minutes 21 seconds


j


_______________________________________________
Omaha-pm mailing list
Omaha-pm@pm.org
http://mail.pm.org/mailman/listinfo/omaha-pm



--
Andrew Sterling Hanenkamp
sterling@hanenkamp.com
785.370.4454