in reply to Using timelocal function

Called as :

#=== Assuming March 31, 2008 from the epoch(?) #=== This script will be running on both Windows and #=== unix machines $days = uniVerseDate(0,0,0,31,2,2008,0,0,0,1,0,1970 ... ... sub uniVerseDate { my ($fS,$fM,$fH,$fD,$fMth,$fY,$sS,$sM,$sH,$sD,$sMth,$sY) = @_; my ($fTot, $sTot, $diff, $secDays) = 0; $fTot = timelocal($fS,$fM,$fH,$fD,$fMth,$fY); $sTot = timelocal($sS,$sM,$sH,$sD,$sMth,$sY); $diff = $fTot - $sTot; $secsDay = 60 * 60 * 24; #=== Add 732 to get days from Jan 1, 1968 to #=== Jan 1, 1970 return (($diff / $secsDay) + 732); }

Replies are listed 'Best First'.
Re^2: Using timelocal function
by kyle (Abbot) on Feb 04, 2008 at 18:11 UTC

    I think 732 days is a bit long for two years, even given that 1968 was a leap year. According to my math (done with DateTime), there were 731 days.

    I think the reason you're getting a trailing .9583333333 days (a little under an hour) is because your date range crosses daylight savings time (which begins in March). It might also have to do with various leap seconds that have been added over the years.

    In any case, I think you'd be much better off using a module to do this calculation for you (see How do I find the difference in days between two dates, in a cool perl way?). They've already been through rigorous testing for this and many other problems.

      According to the values I was getting back from my function the 732 added to the difference found gave me the exact numeric value that I needed. How do I "install" modules such as DateTime? I tried a few minutes earlier to put it into one of my namespaces and kept getting a CGI error back when I tried putting in use DateTime at the top of my .pl script. Thanks for the info. I'll give DateTime a try instead as the whole offset from GMT isn't too appealing.

      Leap Seconds

      Unix, POSIX, and Perl know nothing about the various leap seconds that have been added over the years. Personally I'm glad.

      Read all about POSIX seconds.
      Daylight savings starts in April.
Re^2: Using timelocal function
by Narveson (Chaplain) on Feb 04, 2008 at 17:40 UTC
    Thank you for sharing your subroutine. This is an important piece of what is meant by an example. Now all you have to do is call the subroutine with specific values for its parameters, and show us the output.