Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Perl date calculation and formatting

by Anonymous Monk
on Jul 10, 2015 at 06:13 UTC ( [id://1134083]=perlquestion: print w/replies, xml ) Need Help??

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

For example I read a file listed with dates:

in.txt: Mon Mar 30 10:00:00 2013 Sun Mar 29 10:00:00 2013 Sat Mar 28 10:00:00 2013

and I want to subtract 30 days from each date and save them to out.txt in exactly this same format Of course I can read the file and tokenize them, but I don't know how to subtract date(and with same format).. what is the proper module for this job?

Replies are listed 'Best First'.
Re: Perl date calculation and formatting
by pme (Monsignor) on Jul 10, 2015 at 07:45 UTC
Re: Perl date calculation and formatting
by i5513 (Pilgrim) on Jul 10, 2015 at 07:00 UTC
    If you are on linux, you can always play with date command:
    date -d 'Mon Mar 30 10:00:00 2013 + 30 day'
    with perl, I would try DateTime
    Regards,
      Okay thanks!!
Re: Perl date calculation and formatting
by ambrus (Abbot) on Jul 13, 2015 at 10:53 UTC

    I recommend the Date-Manip module for most date calculations in perl.

    Sadly, this is one of those date formats that Date::Manip does not currently automatically detect, so you have to use an explicit format string to parse it. If you try to parse the dates with an explicit format string, you will find that they are invalid, because Mar 30, 2013 was a Saturday, but the first line says "Mon" instead of "Sat". The other two dates are wrong as well. So let's parse the dates but ignore the day of week given in the input, and print the correct date 30 days before that.

    use warnings; use Date::Manip::Date 6.30; my$base = Date::Manip::Date->new; $base->config("setdate" => "now,utc" +); my $delta = $base->new_delta("-30 days"); open my$infile, "<", "in.txt" or die; while (<$infile>) { my$cur = $base->new; my $parse_err = $cur->parse_format(q"\w+ %b %d %H:%M:%S %Y", $_); if ($parse_err) { 1; # input line does not contain a valid date, just skip it } else { my$mod = $cur->calc($delta); print $mod->printf("%a %b %d %H:%M:%S %Y"); } print "\n"; } __END__

    The output you get is this:

    Thu Feb 28 10:00:00 2013 Wed Feb 27 10:00:00 2013 Tue Feb 26 10:00:00 2013

    See also the related questions: Find 30 days from today's date and Re: Can Date::Manip parse a unix timestamp?.

Re: Perl date calculation and formatting
by hippo (Bishop) on Jul 10, 2015 at 09:01 UTC

    To my mind the FAQ is both clear and comprehensive on this. Did you have any trouble understanding it?

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1134083]
Approved by kevbot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2024-04-23 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found