in reply to Calculating between times

I've always found that when doing calcuations for things as in your example (eg. things older than x days) it can be easier to leave it in the time format of "seconds since the epoch". This way you can simply get your result by finding anything older than x days (times the number of seconds in those days eg "X" times 24 hours times 60 seconds times 60 seconds.

For example work got me to write a script to purge our share drive everyday of any files older than 7 days. Instead of adjusting times bewtween times formats I read the creation and modified dates for each file and simply compared them to a current time stamp. If the files mod time and creation time where both greater then 7*24*60*60 seconds I delete them.

Its easier to compute things to a base and then work from that base. Seconds is a good base.

If you have time in a mm/dd/yy format push this through the Time::Local() module of Perl to convert it back seconds. This module is the inverse of the localtime() function.

Also the Date::Calc module should blow your hair back as far as date calcuations go.

As for the leading zero try this:

#!perl $var = 5; $var = "0".$var if (length($var) < 2); print $var;


Dean

Replies are listed 'Best First'.
Re: Re: Calculating between times
by Anonymous Monk on Jan 08, 2004 at 19:09 UTC
    It turns out I can't use epoch. The way it's setup, I have three pull down menus (one for each: day, month, year) then I join them my $newdate = join(".", $day, $month, $year). And since that's as specific as the time is, I can't calculate epoch as a way to determine how long ago it was.

      You have users specifying what the date is when they create entries? Do they ever remember to do so? Why not simply use the current time, as provideed by localtime?

      --
      TTTATCGGTCGTTATATAGATGTTTGCA