By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,220 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Time Calculation problem

rajiv07
100+
P: 141
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
Share this Question
Share on Google+
4 Replies


gpraghuram
Expert 100+
P: 1,275
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
100+
P: 141
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
Expert 100+
P: 410
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
100+
P: 141
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

Post your reply

Sign in to post your reply or Sign up for a free account.