1829103 Members
2252 Online
109986 Solutions
New Discussion

Re: perl localtime help

 
J. Callender
Frequent Advisor

perl localtime help

All,

Please advise what I'm doing wrong. Following is my script. I want to stop process if
day = day && start hour = hour && min => start min

The following is not working.

#!/opt/OV/nonOV/perl/a/bin/perl
#
use strict;

my @Array = localtime(time );
my $yy = sprintf("%4.2d", $Array[5] + 1900);
my $dd = sprintf("%2.2d",$Array[3]);
my $mm = sprintf("%2.2d",$Array[4] + 1);

my $hr = sprintf("%2.2d",$Array[2]);
my $mn = sprintf("%2.2d",$Array[1]);
my $sec = sprintf("%2.2d",$Array[0]);

my $wday = (localtime(time))[6];
my $day_of_week = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')[$wday];

my $start_hr = "09";
my $start_min = "30";

if (($wday == 1) && ($hr == $start_hr) && ($mn => $start_min)) {
print "Working.. $start_hr = $hr $mn => $start_min\n";
} else {
print "Not Working..\n";
}


Output:

juniorc->./test_hr_mn.pl
Working.. 09 = 09 23 => 30

Appreciate all help.




8 REPLIES 8
Alexander Chuzhoy
Honored Contributor

Re: perl localtime help

What would you like it to print?
J. Callender
Frequent Advisor

Re: perl localtime help

Alexander,

What I'm looking for/ want to do is.

if day=day && hr = hr && min = min

system("do something")
else
do nothing


output above
start hr =09 current hr =09
current mn = 23 start min = 30

output: working should only print
if (start hr)09 = 09 && mn =>(start mn)30
23 is not = or > than 30
Alexander Chuzhoy
Honored Contributor

Re: perl localtime help

I mean it works (as long as you try it on Monday after 9 am and before 10 am.

One more condition:
your perl really is at that path:
#!/opt/OV/nonOV/perl/a/bin/perl
Alexander Chuzhoy
Honored Contributor

Re: perl localtime help

I tried your code on my machine and it works just fine ( I mean it prints working only during specified time).

One little suggestion, that will probably make your program shorter.
I see no reason to use sprintf here-correct me if I'm wrong.
you could take all variables in one line:

my ($sec,$mn,$hr,$dd,$mm,$yy,$wday)=localtime;
correct the month and the year:
$mm+=1;
$yy+=1900;
J. Callender
Frequent Advisor

Re: perl localtime help

I work if day = Mon, Tue, Wed, etc...
and hr = 09

I want it to do something if day = Mon, Tue etc... and hr =09 and min => 30, 40, etc.

I run the script every 15 min.

Yes perl in #!/opt/OV/nonOV/perl/a/bin/perl

This server have OpenView installed, and that where OpenView install perl.
Alexander Chuzhoy
Honored Contributor

Re: perl localtime help

I suggest you to double check the week day of execution, because it works just fine on my system...
When it's Monday - it works.
all other days it prints : not working.
Alexander Chuzhoy
Honored Contributor

Re: perl localtime help

It could be the version of Perl that you use-try to replace the first line with perl that comes with installation and see if it works the way you expect.
J. Callender
Frequent Advisor

Re: perl localtime help

Thread Closed