Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple Time::Piece days comparison

5 views
Skip to first unread message

Tuxedo

unread,
May 9, 2017, 4:54:14 PM5/9/17
to
Hello,

Time::Piece appears to be a great toolkit for date functions including
comparing days beetween two given dates in a easy way.

Following some other examples, I found this to work:

use strict;
use warnings;
use Time::Piece;

my $date_start = '2016-05-09';
my $date_until = '2017-05-09';

my $format = '%Y-%m-%d';

my $diff = Time::Piece->strptime($date_until, $format)
- Time::Piece->strptime($date_start, $format);

print $diff / 86400;

However, as both the $date_start and $date_until placeholders will be
available in the %Y-%m-%d format already, later to be read in as result of
a
function, it appears I'm unecessarily repeating the formatting with the
Time::Piece->strptime(..., $format) parts in the above example.

How can this be constructed a bit better?

Many thanks,
Tuxedo

0 new messages