Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

specific task at each time. 2

Status
Not open for further replies.

perlone

Programmer
May 20, 2001
438
US
Hi,

I have a script that'll check to see if it's a specific time. For example, if the time is 8:33 P.M., then i wanna leave a message on the browser. Here's half of my script:


($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;

if ($hour > 12) {
$hour = $hour - 12;
$ampm = "P.M.";
} else {
$ampm = " A.M.";
}
if ($min eq "0") {
$min = "00";
}
elsif ($min eq "1") {
$min = "01";
}
elsif ($min eq "2") {
$min = "02";
}
elsif ($min eq "3") {
$min = "03";
}
elsif ($min eq "4") {
$min = "04";
}
elsif ($min eq "5") {
$min = "05";
}
elsif ($min eq "6") {
$min = "06";
}
elsif ($min eq "7") {
$min = "07";
}
elsif ($min eq "8") {
$min = "08";
}
elsif ($min eq "9") {
$min = "09";
}

if ((($hour == 8) && ($min == 33) && ($amp eq "P.M."))) {
print "Server Message";
exit;
}else{
&main_page;
}

I don't know but it's not displaying the message. Is it beacuse of the 'exit' function? Anyone knows what's wrong?
 
If that is exactly the code you're using, it might be because you have $amp instead of $ampm in the if statement. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
hehe, silly me. That was the problem. Thanks tsdragon.:)
 
It's always easier for someone else to catch a simple error like that.

Why don't you use sprintf to format the hours like you want instead of that hairy if construction?

Also, your AM/PM logic is incorrect. If the hour is equal to 12 it is already PM. Hours run 00 thru 23. 00-11 is AM, 12-23 is PM. You really have to set the AM/PM indicator and adjust the hours as two separate steps. If hour >= 12 it is PM. If hour > 12, subtract 12. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
There is a much easier way to make $min take up 2 digits (e.g. "01" instead of "1"). The POSIX module, which is part of the standard Perl distribution, contains "strftime" which can be used to do lots of handy things to the output from localtime. To use it, add a "use POSIX;" line to your script. Here are a few examples:

To set $hour=HH, and $min=MM
($hour, $min) = POSIX::strftime("%H%M",localtime()) =~ m/^(\d{2})(\d{2})$/;

To set $date="YYYYMMDD" and $time="HHMM"
($date, $time) = POSIX::strftime("%Y%m%d%H%M",localtime()) =~ m/^(\d{8})(\d{4})$/;

To set $date="YYYY-MM-DD"
$date = POSIX::strftime("%Y-%m-%d",localtime());
 
I don't know, but, personally, to use a whole module just to do something simple, in terms of efficiency, is not really my preference =)...


something like:


if(length($min)==1) { $min="0".$min;}





would work just well...





just a suggestion :)
 
isivt, I agree with that sentiment. I hate to include a whole module just because of one simple thing that I can do other ways. I think sprintf would be even more efficient (but I can't remember the format characters for leading zeroes).
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks, that was VERY helpful. I printed that page for reference. So I guess to format the minutes to two digits you would use:
Code:
$mm = sprintf("%02u", $mm);
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top