Hello Monks,

I need your help to find the best algorithm for my task. The task is very simple, read lines form a file and check lines which the "Interval Date" contains a set of Hours (The Interval Date won't be greater than 24 Hours).

The file is in the form:

Start_Time | End Time | TEXT 2009-07-22 08:00:00|2009-07-22 08:00:00|blablalblabla 2009-07-22 01:00:00|2009-07-22 01:00:00|blablalblabla 2009-07-22 08:00:00|2009-07-22 21:00:00|blablalblabla 2009-07-22 23:00:00|2009-07-23 00:00:00|blablalblabla 2009-07-22 23:00:00|2009-07-23 02:00:00|blablalblabla
The "pseudo code" should be something like that:
my @hours (1, 11) ; # Hours to check open (FILE,"file.txt") or die "$!;"; <FILE>; # Skip first line while (<FILE>){ # Correction after [ack] comment chomp; my ($start_date,$stop_date,$text) = split '|'; print "\nInterval $start_date - $stop_date\n"; foreach my $hour(@hours){ if( ($start_date to $stop_date) contains $hour){ # pseudo code print "Hour $hour: Match"; }else{ print "Hour $hour:Not Match"; } } close(FILE);
The output for the sample file should be (corrected after graff comments):
Interval 2009-07-22 08:00:00 - 2009-07-22 08:00:00 Hour 1 :Not Match Hour 11:Not Match Interval 2009-07-22 01:00:00 - 2009-07-22 01:00:00 Hour 1 :Match Hour 11:Not Match Interval 2009-07-22 08:00:00 - 2009-07-22 21:00:00 Hour 1 :Not Match Hour 11:Match Interval 2009-07-22 23:00:00 - 2009-07-23 00:00:00 Hour 1 :Not Match Hour 11:Not Match Interval 2009-07-22 23:00:00 - 2009-07-23 02:00:00 Hour 1 :Match Hour 11:Not Match

Any help will be helpful.


«A contentious debate is always associated with a lack of valid arguments.»

In reply to Check if Date interval contains Hour X by gulden

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.