How to see if a time is within two other times.

Discussion in 'Perl Misc' started by Ninja67, Jun 29, 2005.

  1. Ninja67

    Ninja67 Guest

    I need to modify a script in such a way that it checks the current time
    (according to the server) to determine if the current time falls within
    a particular time span such as midnight to 4:00 am.

    For example:
    if (current_time > earliest_time_request_accepted && current_time <
    latest_time_request_accepted) {
    ...process request...
    } else {
    print "please try your request at a different time.";
    }

    I struggle with date-time values in perl.
     
    Ninja67, Jun 29, 2005
    #1
    1. Advertising

  2. Ninja67

    Mark Guest

    Ninja67 wrote:
    > I need to modify a script in such a way that it checks the current time
    > (according to the server) to determine if the current time falls within
    > a particular time span such as midnight to 4:00 am.
    >
    > For example:
    > if (current_time > earliest_time_request_accepted && current_time <
    > latest_time_request_accepted) {
    > ...process request...
    > } else {
    > print "please try your request at a different time.";
    > }
    >
    > I struggle with date-time values in perl.
    >

    and what happens when you try to translate this to Perl?

    a few points:

    If you're going to compare dates and times, then you're going to need to
    compare epoch seconds.

    bob 567 $ perldoc -q epoch
    Found in /usr/local/lib/perl5/5.8.2/pod/perlfaq4.pod
    How can I take a string and turn it into epoch seconds?

    If it's a regular enough string that it always has
    the same format, you can split it up and pass the
    parts to "timelocal" in the standard Time::Local
    module. Otherwise, you should look into the
    Date::Calc and Date::Manip modules from CPAN.

    That if statement would be better off written the (OK - an) other way
    round in order to avoid confusion

    ie
    if($endtime > $checkTime && $checkTime > $startTime){

    }

    As per usual, make sure

    use strict;
    use warnings;

    are set at the top of your script.

    Mark
     
    Mark, Jun 29, 2005
    #2
    1. Advertising

  3. Ninja67

    James Taylor Guest

    In article <>,
    Ninja67 <> wrote:
    >
    > I need to modify a script in such a way that it checks the current time
    > (according to the server) to determine if the current time falls within
    > a particular time span such as midnight to 4:00 am.


    You could just use the built in localtime function like this:

    my $current_hour = (localtime)[2];
    if ($current_hour < 4) {
    # Time is after midnight and before 4am
    } else {
    # Time is after 4am and before midnight
    }

    Read up on localtime(). It behaves differently depending on context.

    --
    James Taylor, London, UK PGP key: 3FBE1BF9
    To protect against spam, the address in the "From:" header is not valid.
    In any case, you should reply to the group so that everyone can benefit.
    If you must send me a private email, use james at oakseed demon co uk.
     
    James Taylor, Jun 29, 2005
    #3
  4. Ninja67

    Ninja67 Guest

    Mark wrote:
    > Ninja67 wrote:
    > > I need to modify a script in such a way that it checks the current time
    > > (according to the server) to determine if the current time falls within
    > > a particular time span such as midnight to 4:00 am.
    > >
    > > For example:
    > > if (current_time > earliest_time_request_accepted && current_time <
    > > latest_time_request_accepted) {
    > > ...process request...
    > > } else {
    > > print "please try your request at a different time.";
    > > }
    > >
    > > I struggle with date-time values in perl.
    > >

    > and what happens when you try to translate this to Perl?
    >
    > a few points:
    >
    > If you're going to compare dates and times, then you're going to need to
    > compare epoch seconds.
    >
    > bob 567 $ perldoc -q epoch
    > Found in /usr/local/lib/perl5/5.8.2/pod/perlfaq4.pod
    > How can I take a string and turn it into epoch seconds?
    >
    > If it's a regular enough string that it always has
    > the same format, you can split it up and pass the
    > parts to "timelocal" in the standard Time::Local
    > module. Otherwise, you should look into the
    > Date::Calc and Date::Manip modules from CPAN.
    >
    > That if statement would be better off written the (OK - an) other way
    > round in order to avoid confusion
    >
    > ie
    > if($endtime > $checkTime && $checkTime > $startTime){
    >
    > }
    >
    > As per usual, make sure
    >
    > use strict;
    > use warnings;
    >
    > are set at the top of your script.
    >
    > Mark


    Actually, I discovered that my problem was *far easier* than I first
    thought. The following worked for me:
    my $start = 0;
    my $end = 4;
    if ($hour >= $start && $hour < $end) {
    print "go $hour:$min<br />\n";
    } else {
    print "stop $hour:$min<br />\n";
    }

    With just a few more lines, I could work in minutes and seconds if I
    needed. I guess this is an example of "the best solution is the
    simplest."
     
    Ninja67, Jun 29, 2005
    #4
  5. "Ninja67" <> wrote in
    news::

    > I need to modify a script in such a way that it checks the current
    > time (according to the server) to determine if the current time falls
    > within a particular time span such as midnight to 4:00 am.


    Time::Local can help:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use Time::Local;

    # 2005/06/29 16:00:00
    my $start = timelocal(0, 0, 16, 29, 5, 2005);
    my $end = $start + (4 * 60 * 60);


    if($start <= time and time <= $end) {
    run();
    } else {
    expired();
    }

    sub run { print "Running\n" }

    sub expired { print "Expired\n" }

    __END__


    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jun 29, 2005
    #5
  6. Ninja67

    Ninja67 Guest

    James Taylor wrote:
    > In article <>,
    > Ninja67 <> wrote:
    > >
    > > I need to modify a script in such a way that it checks the current time
    > > (according to the server) to determine if the current time falls within
    > > a particular time span such as midnight to 4:00 am.

    >
    > You could just use the built in localtime function like this:
    >
    > my $current_hour = (localtime)[2];
    > if ($current_hour < 4) {
    > # Time is after midnight and before 4am
    > } else {
    > # Time is after 4am and before midnight
    > }
    >
    > Read up on localtime(). It behaves differently depending on context.
    >
    > --
    > James Taylor, London, UK PGP key: 3FBE1BF9
    > To protect against spam, the address in the "From:" header is not valid.
    > In any case, you should reply to the group so that everyone can benefit.
    > If you must send me a private email, use james at oakseed demon co uk.


    Thank you. I derived at that solution independently, but that is
    indeed what I ended up doing. I was over-complicating it at first.

    Thanks again.
     
    Ninja67, Jun 29, 2005
    #6
  7. Ninja67

    Ninja67 Guest

    A. Sinan Unur wrote:
    > "Ninja67" <> wrote in
    > news::
    >
    > > I need to modify a script in such a way that it checks the current
    > > time (according to the server) to determine if the current time falls
    > > within a particular time span such as midnight to 4:00 am.

    >
    > Time::Local can help:
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > use Time::Local;
    >
    > # 2005/06/29 16:00:00
    > my $start = timelocal(0, 0, 16, 29, 5, 2005);
    > my $end = $start + (4 * 60 * 60);
    >
    >
    > if($start <= time and time <= $end) {
    > run();
    > } else {
    > expired();
    > }
    >
    > sub run { print "Running\n" }
    >
    > sub expired { print "Expired\n" }
    >
    > __END__
    >
    >
    > Sinan
    > --
    > A. Sinan Unur <>
    > (reverse each component and remove .invalid for email address)
    >
    > comp.lang.perl.misc guidelines on the WWW:
    > http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


    Very elegant. Thank you. I found a simpler solution for my script,
    but yours would work in a larger variety of situations, so I'll
    definitely file it away under "good scripts".
     
    Ninja67, Jun 29, 2005
    #7
  8. Ninja67

    John Bokma Guest

    John Bokma, Jun 29, 2005
    #8
  9. "Ninja67" <> wrote in news:1120077284.168933.224500
    @g47g2000cwa.googlegroups.com:

    > A. Sinan Unur wrote:


    ....

    >> --
    >> A. Sinan Unur <>
    >> (reverse each component and remove .invalid for email address)
    >>
    >> comp.lang.perl.misc guidelines on the WWW:
    >> http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    >
    > Very elegant. Thank you.


    You are welcome.

    I realize you are one of those Google posters who is making an effort to
    quote properly, so thank you for that.

    Please note that you should not quote signatures unless you are
    commenting the signature itself.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jun 29, 2005
    #9
  10. "A. Sinan Unur" <> wrote in
    news:Xns9684A8284206Dasu1cornelledu@127.0.0.1:

    > "Ninja67" <> wrote in
    > news::
    >
    >> I need to modify a script in such a way that it checks the current
    >> time (according to the server) to determine if the current time falls
    >> within a particular time span such as midnight to 4:00 am.

    >
    > Time::Local can help:
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > use Time::Local;
    >
    > # 2005/06/29 16:00:00
    > my $start = timelocal(0, 0, 16, 29, 5, 2005);
    > my $end = $start + (4 * 60 * 60);
    >
    >
    > if($start <= time and time <= $end) {


    Actually, it is useless to call time twice (not likely to matter too much
    given the resolution is in seconds, but still).

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jun 29, 2005
    #10
  11. Ninja67

    Arne Ruhnau Guest

    Ninja67 wrote:
    > Mark wrote:
    >
    >>Ninja67 wrote:
    >>
    >>>I need to modify a script in such a way that it checks the current time
    >>>(according to the server) to determine if the current time falls within
    >>>a particular time span such as midnight to 4:00 am.

    <snip>
    > Actually, I discovered that my problem was *far easier* than I first
    > thought. The following worked for me:
    > my $start = 0;
    > my $end = 4;
    > if ($hour >= $start && $hour < $end) {
    > print "go $hour:$min<br />\n";
    > } else {
    > print "stop $hour:$min<br />\n";
    > }
    >
    > With just a few more lines, I could work in minutes and seconds if I
    > needed. I guess this is an example of "the best solution is the
    > simplest."


    If it is relevant to you, remember daylight saving times. In Germany, e.g.,
    there are days which have the period 2:00am - 2:59am twice, but the latter
    is surely after the former. On this day, a range 2:00am - 3:00am is
    ambigous unless you go with the convention to name the first 2a o'Clock and
    the second 2b o'Clock. (That is one of the reasons why not every day has
    86400 seconds *sigh*)

    hth,

    Arne Ruhnau
     
    Arne Ruhnau, Jun 30, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWF2cmlja18xMDE=?=

    SetAuthCookie works some times and fails some times?

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    544
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 23, 2006
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Forms Authentication Fails some times and not some times???

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    507
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 28, 2006
  3. djskrill
    Replies:
    9
    Views:
    713
    djskrill
    Oct 1, 2003
  4. Peng Yu
    Replies:
    17
    Views:
    723
    Peng Yu
    Sep 14, 2008
  5. Abby Lee
    Replies:
    5
    Views:
    450
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page