Day '29' out of range 1..28 at


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Day '29' out of range 1..28 at
# 1  
Old 02-01-2010
Day '29' out of range 1..28 at

Hi all,
I wrote few lines of code to check how long a process has been uptime.
I work on Solaris SunOS 5.10 and perl v5.8.4.

Here is the code:
Code:
#!/usr/bin/perl
use Time::Local;
$service="TCPIPSCH";
$PID_SERVICE=`ps -ef | grep -w $service | grep -v grep | awk '{print \$2}'`;
if ($PID_SERVICE == ''){
        print "$service down\n";
        exit;
}
       print"PID_SERVICE=".$PID_SERVICE;
$PID_PATH=sprintf("/proc/%d",$PID_SERVICE);
       print "PID_PATH=".$PID_PATH."\n";
@d=localtime((stat($PID_PATH))[9]);
#       printf "Localtime PID %4d/%02d/%02d %02d:%02d:%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0];
$ACTUAL_UNIX_TIME=time();
$sec=$d[0];
$min=$d[1];
$hours=$d[2];
$day=$d[3];
$month=$d[4]+1;
$year=$d[5]+1900;

$sec=27;
$min=40;
$hours=11;
$day=29;
$month=01;
$year=2010;


$PID_TIME = timelocal($sec,$min,$hours,$day,$month,$year);
# replace 'timelocal' with 'timegm' if your input date is GMT/UTC

print "ACTUAL UNIX TIME = \t".$ACTUAL_UNIX_TIME."\n";
print "        PID_TIME = \t".$PID_TIME."\n";
$DELTA=$ACTUAL_UNIX_TIME-$PID_TIME;
print "           DELTA = \t".$DELTA." seconds\n";
if ($DELTA > 300) {
        print "No restart detected!\n";
} else {
        print "$service restarted!\n";
}

You'll notice that PID_TIME refers to the 29th of january.
This is because executing the script today with that date I get the following error:
Code:
Day '29' out of range 1..28 at ./getpidtime.pl line 30

It seems that the timelocal function relates the day 29 to February instead of January.
But why? I clearly pass month = 1!

Thanks for your help...
# 2  
Old 02-01-2010
Read the docs for Time::Local (also available via "perldoc Time::Local" on the command line) (emphasis added):
Quote:
It is worth drawing particular attention to the expected ranges for the values provided. The value for the day of the month is the actual day (ie 1..31), while the month is the number of months since January (0..11). This is consistent with the values returned from localtime() and gmtime().
# 3  
Old 02-01-2010
Hi!
I found an article few minutes before your reply. Smilie

Anyway, thank you very much!

Regards,
Evan
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 Replies

2. UNIX for Dummies Questions & Answers

Move the files between Current day & a previous day

Hi All, I have a requirement where I need to first capture the current day & move all the files from a particular directory based on a previous day. i.e move all the files from one directory to another based on current day & a previous day. Here is what I am trying, but it gives me errors.... (2 Replies)
Discussion started by: dsfreddie
2 Replies

3. Shell Programming and Scripting

Script to check if last modified day is previous day

Hi, I would like to write a script that checks if a file ('counter') was modified the previous day, if so erase its contents and write 00000000 into it. For e.g. if the file 'counter' was last modified at 11.30pm on 24th May and the script runs at 12.15am of 25th May, it should erase it's... (1 Reply)
Discussion started by: hegdepras
1 Replies

4. Shell Programming and Scripting

Code creates day 32 instead of 1st day of next month.

I am using the code below modified from a post I saw here regarding having the script write out future dates. The problem is that instead of making 8/1 it makes 7/32! Please help! yy=`date +%Y` mm=`date +%m` dd=`date +%d` echo "Today is : $yy $mm $dd" #!/usr/bin/ksh date '+%m... (5 Replies)
Discussion started by: libertyforall
5 Replies

5. Solaris

May i know the day to day activities of a Solaris system administrator?

Recently i have attended a telephonic interview. As i dont have work experience in solaris i was not able to deliver correct answer for this question. Your answer will help for the people like me who is looking to become Solaris System administrator. (4 Replies)
Discussion started by: Sesha
4 Replies

6. Shell Programming and Scripting

processing for each day given a date range

hello, I have a date range submitted by the user in ddmmyy format. how can i do some processing for each day. E.g. date params are 020110 (start date 01 jan 10) and 300110 (30 jan 10), i.e for each day in the date range do some processing... until end date has been reached. ... (3 Replies)
Discussion started by: Wahmed9
3 Replies

7. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

8. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

9. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies
Login or Register to Ask a Question