470,833 Members | 1,468 Online

# Time Calculation problem

141 100+
I have a script to calculate the warranty the period of one year.I add 365 days of current date.I got the correct output for the year 2007 but the 2008 year calculation getting incorrect value.please anybody explain me what is happening here.

Expand|Select|Wrap|Line Numbers
1. #!/usr/bin/perl
2. use Time::Local;
3.
4. my \$time=timelocal(0,0,0,21,4,2007);
5.
6. my \$warrenty="";
7.
8. \$warrenty=localtime(\$time+60*60*24*365);
9.
10. print \$warrenty;
11.
12. print "\n";
13.
14. \$warrenty=localtime(time()+60*60*24*365);
15.
16. print \$warrenty;
17.
The output
--------------
Tue May 20 00:00:00 2008

Thu May 21 12:29:46 2009

Regards

Rajiv
May 21 '08 #1
4 1608
gpraghuram
1,275 Expert 1GB
I have a script to calculate the warranty the period of one year.I add 365 days of current date.I got the correct output for the year 2007 but the 2008 year calculation getting incorrect value.please anybody explain me what is happening here.

Expand|Select|Wrap|Line Numbers
1. #!/usr/bin/perl
2. use Time::Local;
3.
4. my \$time=timelocal(0,0,0,21,4,2007);
5.
6. my \$warrenty="";
7.
8. \$warrenty=localtime(\$time+60*60*24*365);
9.
10. print \$warrenty;
11.
12. print "\n";
13.
14. \$warrenty=localtime(time()+60*60*24*365);
15.
16. print \$warrenty;
17.
The output
--------------
Tue May 20 00:00:00 2008

Thu May 21 12:29:46 2009

Regards

Rajiv
To get a future date or a previous date you can use this idea.
Use Tick count and add or subtract the number of days.

Expand|Select|Wrap|Line Numbers
1. my @temp_var = split(/ /,localtime(time() - (\$day_sub*86400)));
Here \$day_sub is the number of days and here i am subtracting .
To get a later date use + instead of -

Raghuram
May 22 '08 #2
rajiv07
141 100+
To get a future date or a previous date you can use this idea.
Use Tick count and add or subtract the number of days.

Expand|Select|Wrap|Line Numbers
1. my @temp_var = split(/ /,localtime(time() - (\$day_sub*86400)));
Here \$day_sub is the number of days and here i am subtracting .
To get a later date use + instead of -

Raghuram

Thanks Raghuram,

Actually my doubt is if i add 365 days in 22-05-2007 the output comes like 21-05-2008.But if i add the same 365 days in 22-05-2008 the output comes like
22-05-2009,But my expected output is 21-05-2009.
May 22 '08 #3
nithinpes
410 Expert 256MB
Thanks Raghuram,

Actually my doubt is if i add 365 days in 22-05-2007 the output comes like 21-05-2008.But if i add the same 365 days in 22-05-2008 the output comes like
22-05-2009,But my expected output is 21-05-2009.
That is because year 2008 is a leap year! The output is the expected one.
May 22 '08 #4
rajiv07
141 100+
That is because year 2008 is a leap year! The output is the expected one.
Thank nithinpes

But If i apply for this script for 01-21-2009 this gives 01-21-2010.but after 2007 the 2011 and 2015.. are gives my expected output.The years 2008,2009,2010 are not giving the expected output.

Regards
Rajiv
May 22 '08 #5