I have a paltry, date string conundrum upon which I beseech the wisdom of those who frequent this site. I am doing basic date/string manipulation with the constraint of not using Date::Calc; I am just using Perl "out of the box". The below code snippet demonstrates the core issue. I take an input date and generate a date 6 days later. It works for every date except November 1st and I am not sure why.
#!/usr/bin/perl -w use warnings; use strict; use Getopt::Long; use Time::Local; use POSIX qw(strftime); # The idea is to take an input date and add 6 days to it. # It seems to work except when the start date is November 1! # # Below is an example: # my $input_date; GetOptions("start_date=s" => \$input_date); print "\nWe expect a start day and a day 6 days later than that!\n"; my @date_pieces = split('/',$input_date); my $start_day = timelocal(0, 0, 0, $date_pieces[1], $date_pieces[0]-1, + $date_pieces[2]); my $end_day = $start_day + 6*24*60*60; my $new_start_day = strftime "%m/%d/%Y", (localtime($start_day)); my $new_end_day = strftime "%m/%d/%Y", (localtime($end_day)); print "The start day is $new_start_day and the end day is $new_end_day +\n";
Below is a run of various inputs and you see that the only failure seen is Nov 1st:
$ LIST="09/20/2008 1/1/2009 11/3/2008 06/06/2006 11/1/2008 11/1/2008 1 +0/1/2008" $ for i in $LIST > do > booger -start_date $i > done We expect a start day and a day 6 days later than that! The start day is 09/20/2008 and the end day is 09/26/2008 We expect a start day and a day 6 days later than that! The start day is 01/01/2009 and the end day is 01/07/2009 We expect a start day and a day 6 days later than that! The start day is 11/03/2008 and the end day is 11/09/2008 We expect a start day and a day 6 days later than that! The start day is 06/06/2006 and the end day is 06/12/2006 We expect a start day and a day 6 days later than that! The start day is 11/01/2008 and the end day is 11/06/2008 We expect a start day and a day 6 days later than that! The start day is 11/01/2008 and the end day is 11/06/2008 We expect a start day and a day 6 days later than that! The start day is 10/01/2008 and the end day is 10/07/2008
Note how both November dates are 5 days apart as opposed to 6. Any help would be greatly appreciated.

In reply to date string conundrum by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.