gossamer has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm definitely a perl newbie, but eager to learn. I'm trying to use Time::Piece to print the the ISO8601 date on this day last month. Is this built in or will I have to use other functions to do this?

I have a bunch of DB_File hash files that have one of their values contains an ISO8601 date from some time in the past. I need to scan through all of the hash files and delete any record that contains a date older than 30 days.

The hashes were created by parsing the ISO8601 date as part of a filename such as myfile-b2291ce2b21d1cf6f6af68bd3e355505-20120112T115534.

I need to write a function that scans the hashes and delete any entries that have a date older than the day component of the filename above.

Thanks for any ideas,
Alex

Replies are listed 'Best First'.
Re: Using Time::Piece and ISO8601
by frozenwithjoy (Priest) on Jun 22, 2012 at 01:56 UTC
    Hi. Maybe you can incorporate something like this:
    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; use DateTime; my $date_in = "myfile-b2291ce2b21d1cf6f6af68bd3e355505-20120112T115534 +"; my ($year, $month, $day ) = $date_in =~ m| myfile- .* - ( \d{4} ) ( \d +{2} ) ( \d{2} ) T \d{6} |x; my $now = DateTime->now; my $record = DateTime->new( year => $year, month => $month, day => $day, ); my $duration = $now->delta_days($record)->in_units('days'); #just reporting stuff to make sure all is well say "Now is: $now"; say "The record was: $record"; say "The record is $duration days old..."; say "Too old!" if $duration > 30;

    The output:

    Now is: 2012-06-22T01:55:10 The record was: 2012-01-12T00:00:00 The record is 162 days old... Too old!
      That's awesome. Thanks so much for spending the time to help.

      Thanks,
      Alex