Time Calculation problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajiv07
    New Member
    • Jun 2007
    • 141

    Time Calculation problem

    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.

    [CODE=perl]#!/usr/bin/perl
    use Time::Local;

    my $time=timelocal (0,0,0,21,4,200 7);

    my $warrenty="";

    $warrenty=local time($time+60*6 0*24*365);

    print $warrenty;

    print "\n";

    $warrenty=local time(time()+60* 60*24*365);

    print $warrenty;
    [/CODE]

    The output
    --------------
    Tue May 20 00:00:00 2008

    Thu May 21 12:29:46 2009

    Regards

    Rajiv
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by rajiv07
    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.

    [CODE=perl]#!/usr/bin/perl
    use Time::Local;

    my $time=timelocal (0,0,0,21,4,200 7);

    my $warrenty="";

    $warrenty=local time($time+60*6 0*24*365);

    print $warrenty;

    print "\n";

    $warrenty=local time(time()+60* 60*24*365);

    print $warrenty;
    [/CODE]

    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.

    Code:
    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

    Comment

    • rajiv07
      New Member
      • Jun 2007
      • 141

      #3
      Originally posted by gpraghuram
      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.

      Code:
      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.

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by rajiv07
        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.

        Comment

        • rajiv07
          New Member
          • Jun 2007
          • 141

          #5
          Originally posted by nithinpes
          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

          Comment

          Working...