Can I please get some assistance with this issue. Im trying to convert given dates in the format YYYY-MM-DD ( from the inputmodfile file) to epoch seconds in order to determine what day it was. I saw that I could parse the date and using localtime and timelocal, achieve this but its not working. I'm getting the following errors from the part in red
"Use of uninitialized value in subtraction (-) at ./test3.pl line 30. Use of uninitialized value in subtraction (-) at ./test3.pl line 31. Month '-1' out of range 0..11 at ./test3.pl line 35"
This is the script:
#!/usr/bin/perl use strict; use warnings; use Time::Local; #use Time::gmtime; my $inputfile = "/home/vdelaney/test.txt"; my $inputmodfile = "/home/vdelaney/test_mod.txt"; my $outputfile = "/home/vdelaney/test_out.txt"; system("cat \"$inputfile\" | awk '{print \$2}'| awk -F 'T' '{print \$1 +}' > $inputmodfile"); my $seventh_days= 7 * 24 * 60 * 60; print " seventh_day from today=$seventh_days\n"; my ($old_day, $old_month, $old_year) = (localtime(time - $seventh_days +))[3..5]; #print "old_day=$old_day ,old_month= $old_month,old_year= $old_year \ +n"; my $cutoff = sprintf('%04d-%02d-%02d', $old_year + 1900, $old_month + 1, $old_day); print "cutoff date=$cutoff\n"; open my $handle, '<', $inputmodfile; chomp(my @lines = <$handle>); close $handle; #print "List of lines that have dates older than the cutoff date\n"; my ($yyyy, $mm, $dd); my $epoch_seconds; my $strings; for my $date (@lines) {
($yyyy, $mm, $dd) = ($date =~ /(\d+)-(\d+)-(\d+)/); $epoch_seconds = timelocal(0, 0, 0, $dd, $mm, $yyyy-1900); $strings = localtime($epoch_seconds);
print "$date : $epoch_seconds\n" if $date lt $cutoff; }

In reply to convert a given YYYY-MM-DD to epoch time by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.