laziness, impatience, and hubris PerlMonks

### Re: Calculate seconds between dates

by ikegami (Pope)
 on Nov 23, 2017 at 22:45 UTC ( #1204173=note: print w/replies, xml ) Need Help??

in reply to Calculate seconds between dates

```# We assume \$dt1 and \$dt2 are during working hours, and
# we assume that DST changes will never occur during working hours, so
# we can ignore time zones.

use DateTime::Format::RFC3339 qw( );

my \$format = DateTime::Format::Strptime->new(
format    => '%Y-%m-%dT%H:%M:%S',
time_zone => 'floating',
on_error  => 'croak',
);

my \$dt1 = \$format->parse_datetime('2017-11-17T16:00:00');
my \$dt2 = \$format->parse_datetime('2017-11-22T17:10:00');

my \$total = 0;

if (is_workday(\$dt1)) {
( my \$eod = \$dt1->clone() )
->set( hour => 17, minute => 30, second => 0, nanosecond => 0 );
\$total += \$eod->delta_ms(\$dt1)->in_units('seconds')
if \$eod > \$dt1;
}

if (is_workday(\$dt2)) {
( my \$sod = \$dt2->clone() )
->set( hour => 9, minute => 0, second => 0, nanosecond => 0 );
\$total += \$sod->delta_ms(\$dt2)->in_units('seconds')
if \$sod < \$dt2;
}

\$dt1->truncate( to => 'day' );
\$dt2->truncate( to => 'day' );
while (1) {
last if \$dt1 >= \$dt2;
next if !is_workday(\$dt1);
\$total += 8.5 * 60;
}

Create A New User
Node Status?
node history
Node Type: note [id://1204173]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2018-06-20 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (117 votes). Check out past polls.

Notices?