# 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) { $dt1->add( days => 1 ); last if $dt1 >= $dt2; next if !is_workday($dt1); $total += 8.5 * 60; }

In reply to Re: Calculate seconds between dates by ikegami
in thread Calculate seconds between dates by packetstormer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.