Hi guys I'm weaning myself back on the Perl trail after a long illness though I've already hit a problem that's baffling me! I need to write a script that is supplied with 2 dates that must be consecutive ( date1 is read from a file & date2 is a parameter. Date2 must be 1 day later than date1. If you look at the attached rough version of this script - it looks like it should do the job (Okay maybe only to my naive eye!) but.... It looked like it was working if the dates were mid month but as soon as the dates were the 31st October & 1st Nov the results were unexpected - to this novice at least! Anyone shed any light on this or am I being very foolish? (It is amazing how much you forget in just 6 months!!)
#!/usr/bin/perl -w # use Time::Local ; # my $sec1 = '01' ; my $min1 = '00' ; my $hour1 = '00' ; my $day1 = '24' ; my $mon1 = '02' ; my $year1 = '2005' ; my $TIME1 = undef ; my $date1 = undef ; # my $tm = undef ; my $now = time ; # my $sec2 = '01' ; my $min2 = '00' ; my $hour2 = '00' ; my $day2 = '25' ; my $mon2 = '02' ; my $year2 = '2005' ; my $TIME2 = undef ; my $date2 = undef ; # print "\n\tTimestamp test begins\n" ; # $TIME1 = timegm($sec1,$min1,$hour1,$day1,$mon1,$year1) ; # print "\n\tTimestamp1 :: $TIME1\n" ; # ($sec1,$min1,$hour1,$day1,$mon1,$year1) = gmtime($TIME1) ; print "\n\tDay :: $day1" ; print "\n\tMon :: $mon1" ; $year1 += 1900 ; print "\n\tYear :: $year1\n" ; # print "\n\tDATE1 :: $year1\/$mon1\/$day1\n" ; # $TIME2 = timegm($sec2,$min2,$hour2,$day2,$mon2,$year2) ; # print "\n\tTimestamp2 :: $TIME2\n" ; # ($sec2,$min2,$hour2,$day2,$mon2,$year2) = gmtime($TIME2) ; print "\n\tDay :: $day2" ; print "\n\tMon :: $mon2" ; $year2 += 1900 ; print "\n\tYear :: $year2\n" ; # print "\n\tDATE2 :: $year2\/$mon2\/$day2\n" ; # my $dif = $TIME2 - $TIME1 ; my $days = ($dif / (60 * 60 * 24)) ; # print "\n\tThe difference is :: $days\n" ; # print "\n\tTimestamp test ends\n" ;
br Running with the above params results in -
$ xxrctimestamp.pl Timestamp test begins Timestamp1 :: 1111622401 Day :: 24 Mon :: 2 Year :: 2005 DATE1 :: 2005/2/24 Timestamp2 :: 1111708801 Day :: 25 Mon :: 2 Year :: 2005 DATE2 :: 2005/2/25 The difference is :: 1 Timestamp test ends $
Using the following parameters however -
my $sec1 = '01' ; my $min1 = '00' ; my $hour1 = '00' ; my $day1 = '31' ; my $mon1 = '10' ; my $year1 = '2005' ; my $TIME1 = undef ; my $date1 = undef ; # my $tm = undef ; my $now = time ; # my $sec2 = '01' ; my $min2 = '00' ; my $hour2 = '00' ; my $day2 = '01' ; my $mon2 = '11' ; my $year2 = '2005' ; my $TIME2 = undef ; my $date2 = undef ;
br Results in the less than successful -
$ xxrctimestamp.pl Timestamp test begins Timestamp1 :: 1133395201 Day :: 1 Mon :: 11 Year :: 2005 DATE1 :: 2005/11/1 Timestamp2 :: 1133395201 Day :: 1 Mon :: 11 Year :: 2005 DATE2 :: 2005/11/1 The difference is :: 0 Timestamp test ends
If there is something mind bogglingly stupid/dumb about this please feel free to say because I can't see my error! Cheers, Ronnie

In reply to Date/Timestamp Puzzle by Ronnie

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.