Forum Moderators: coopster & phranque

Message Too Old, No Replies

Detecting the time in perl?

I am really useless with perl...

         

Richie0x

5:43 pm on May 21, 2004 (gmt 0)

10+ Year Member



How do you detect the time in perl?

Like in PHP you would put:

$time = date("H");

And that would give you the hour.

Then I want to check if $time equals something, and if so, do something, if not do something else, like in php you would do:

if ($time == "10") { do something }
if ($time!= "10") { do something else }

i am really used to PHP and know nothing about perl. Is there a manual or function list somewhere?

DrDoc

6:15 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use the localtime() function, which returns an array... The most common usage is something like this:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

$sec, $min, and $hour are the seconds, minutes, and hours of the specified time. $mday is the day of the month, and $mon is the month itself, in the range 0..11 with 0 indicating January and 11 indicating December. $year is the number of years since 1900. That is, $year is 123 in year 2023. $wday is the day of the week, with 0 indicating Sunday and 3 indicating Wednesday. $yday is the day of the year, in the range 0..364 (or 0..365 in leap years.) $isdst is true if the specified time occurs during daylight savings time, false otherwise.

Richie0x

6:44 pm on May 21, 2004 (gmt 0)

10+ Year Member



Thanks DrDoc

Why is perl so complicated!? :/

DrDoc

6:55 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because everything is possible :)

Richie0x

7:00 pm on May 21, 2004 (gmt 0)

10+ Year Member



So would it be like this:

--

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

if (($hour >= "00")&&($hour =< "07")) { $switch = "on"; }
if ($switch == "on") { do something }
if ($switch == "off") { do something else }

--

Is that correct?

Instead of asking for the year, month, day, week etc, couldn't you just have:

($sec,$min,$hour) = localtime(time);

?

moltar

7:21 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can make in nicer like this:


#!/usr/bin/perl

my $hour = (localtime(time))[2];
my $switch = 0;

if ( $hour >= 0 && $hour <= 7 ) {
$switch = 1;
}

if ( $switch ) {
# do something
} else {
# do something else
}

volatilegx

2:06 pm on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice code, moltar, I was thinking of something similar, but was going to key the array on the left side of the "=" sign. Yours is better :)