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



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

.



Relevant Pages

  • lk-changelog.pl
    ... This script is used by Linus and Marcelo to rearrange and reformat BK ... retrieving revision 0.327 ... 'davidm:hpl.hp.com' => 'David Mosberger', ... 'mark.fasheh:oracle.com' => 'Mark Fasheh', ...
    (Linux-Kernel)
  • 2005-10-23 [de.comp.lang.perl.cgi] FAQ
    ... Programmiersprache Perl in CGI-Scripten. ... Wo gibt es Dokumentation über Perl und CGI? ... Hilfe zum Programm perldoc kann man mit 'perldoc perldoc' ... Mein Script läuft nicht bei meinem Provider. ...
    (de.comp.lang.perl.cgi)
  • 2005-10-23 [de.comp.lang.perl.cgi] FAQ
    ... Programmiersprache Perl in CGI-Scripten. ... Wo gibt es Dokumentation über Perl und CGI? ... Hilfe zum Programm perldoc kann man mit 'perldoc perldoc' ... Mein Script läuft nicht bei meinem Provider. ...
    (de.comp.lang.perl.cgi)
  • 2005-10-23 [de.comp.lang.perl.cgi] FAQ
    ... Programmiersprache Perl in CGI-Scripten. ... Wo gibt es Dokumentation über Perl und CGI? ... Hilfe zum Programm perldoc kann man mit 'perldoc perldoc' ... Mein Script läuft nicht bei meinem Provider. ...
    (de.comp.lang.perl.cgi)
  • 2005-11-20 [de.comp.lang.perl.cgi] FAQ
    ... Programmiersprache Perl in CGI-Scripten. ... Wo gibt es Dokumentation über Perl und CGI? ... Hilfe zum Programm perldoc kann man mit 'perldoc perldoc' ... Mein Script läuft nicht bei meinem Provider. ...
    (de.comp.lang.perl.cgi)