Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Very nice example of these DateTime modules. I installed them and hope they are useful. But, it doesn't exactly figure the intersecting time of 2 sets of start, end times.

But here is a way to do it that produces output like he wanted. There are other modules besides DateTime that would provide solutions, Time::Piece (Time::Piece was first released with perl v5.9.5), Date::Parse, Time::Local, and probably others.

Either set of beg/end times could be switched, ($d1_beg and $d1_end could have been named $d2_beg, $d2_end and vice versa and this method would still be correct.

Chris

#!/usr/bin/perl use strict; use warnings; use DateTime::Format::Strptime; use List::Util qw/ maxstr minstr /; # first released with perl v5.7.3 my $d1_beg = '2012-08-17 12:00:00'; my $d1_end = '2012-08-17 13:00:00'; my $d2_beg = '2012-08-17 09:00:00'; my $d2_end = '2012-08-17 12:30:00'; if ($d2_beg ge $d1_end or $d2_end le $d1_beg) { print "No common time\n"; } else { my $start = maxstr($d1_beg, $d2_beg); my $end = minstr($d1_end, $d2_end); my $dt = DateTime::Format::Strptime->new(pattern => "%F %T"); my @dt = map $dt->parse_datetime($_), $start, $end; my $dur = $dt[1]->delta_ms($dt[0])->in_units('minutes'); # duratio +n print "$start for $dur minutes\n"; } __END__ *** prints 2012-08-17 12:00:00 for 30 minutes

Update: There is a nice discussion here about handling durations with DateTime.


In reply to Re^2: timestamp manipulation and evaluation by Cristoforo
in thread timestamp manipulation and evaluation by jonathancnewcomb

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (13)
    As of 2014-07-28 12:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (196 votes), past polls