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

N

Ninja67

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.
 
M

Mark

Ninja67 said:
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
 
J

James Taylor

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.
 
N

Ninja67

Mark said:
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."
 
A

A. Sinan Unur

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
 
N

Ninja67

James said:
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.
 
N

Ninja67

A. Sinan Unur said:
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

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".
 
A

A. Sinan Unur

A. Sinan Unur wrote:
....


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

A. Sinan Unur

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

Arne Ruhnau

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top