Can someone please tell me the best (most efficient) way for setting a night time period to be between 8:30pm and 7am and then detecting when the program in question is running at night time.

Do you mean: "How can a perl program detect that it is running between 8:30pm and 7am local time?" If that's the question, one answer would be a subroutine like the following. I've decided to set it up as a subroutine because that makes it easier to parameterize the conditions -- who knows, next week you might want a program to check whether it's running between 9:45am and 11:53am...

use POSIX; sub timenow_is_between { my ( $bgn, $end ) = @_; # args are "HH:MM" strings (length==5) # e.g. 20:30 or 07:00 my $timenow = strftime( "%H:%M", localtime ); if ( $bgn ge $end ) { return ( $timenow ge $bgn or $timenow lt $end ); } else { return ( $timenow ge $bgn and $timenow lt $end ); } }
(Update: decided to use "ge" instead of "gt" when testing $bgn against $end, and when testing $timenow against $bgn; this should "do the right thing" in various boundary conditions, like $bgn and $end being identical, or different by just one minute.)

Here's an example of how you would use it:

if ( timenow_is_between( "20:30", "07:00" ) { print "this program is running at night\n"; } else { print "it is not night time right now\n"; }
But having seen the longer snippets of code you posted in a couple of your replies on this thread, it's not clear to me what your goal really is (and I wonder whether you have a clear idea of what your goal really is).

Note that POSIX (which provides the "strftime" function) is a "core" module -- it's available for use in any standard perl installation.


In reply to Re: working with time ranges that cross days by graff
in thread working with time ranges that cross days by Win

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.