2

How can I take the current epoch time from perl and find the epoch time from 6 months ago?

I need to compare the create date of a file to see if it 6 months or older.

AtomicPorkchop
  • 2,625
  • 5
  • 36
  • 55
  • 1
    How is the difference between two numbers calculated? How can X seconds (or milliseconds, etc) be converted into days (or months)? (To be 100% accurate you'd need a Date module that understands leap-days and whatnot. However, for "6 months", basic math is A-OK.) –  Jun 29 '11 at 04:35
  • I just need a rough calculation, does not need nano seconds and what not. I just need to see if the file is older than 6 months, if it is we delete it. – AtomicPorkchop Jun 29 '11 at 04:41
  • @pst: For "a week" or "N days", basic math is OK, but the number of days in six months varies depending on the start/end date. – Dave Sherohman Jun 29 '11 at 09:24
  • POSIX files don’t have create dates. – tchrist Jun 29 '11 at 14:23
  • @Dave Sherohman "30.5 days" ;-) –  Jun 29 '11 at 18:09

3 Answers3

4

The difference can be calculated using DateTime:

use DateTime;

my $dt = DateTime->now;  # or if you have epoch: DateTime->from_epoch( 'epoch' => $epoch );
$dt->subtract( DateTime::Duration->new( 'months' => 6 ) );
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
1

The Date::Calc package has functions for that. Use Date_to_Time and Time_to_Date to convert to/from epoch time, and use Add_Delta_YM to add -6 months.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Bah! I cannot get that module on Windows. – AtomicPorkchop Jun 29 '11 at 04:45
  • Depending on how much error you can tolerate, you can compute epoch time minus six months by subtracting (365 * 24 * 60 * 60 * 500) from now. (Those should probably be long values.) The 500 (instead of 1000) at the end is for half a year instead of a full year. This is likely to be off by a day or two. – Ted Hopp Jun 29 '11 at 05:01
0

Rough and ready?

perl -le '$sixm = 30.5*6; for ( glob("*") ) { print "delete $_" if -M > $sixm }'
Ashley
  • 4,307
  • 2
  • 19
  • 28