Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

convert a given YYYY-MM-DD to epoch time

by Anonymous Monk
on Dec 04, 2015 at 22:07 UTC ( [id://1149414]=perlquestion: print w/replies, xml ) Need Help??

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

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; }

Replies are listed 'Best First'.
Re: convert a given YYYY-MM-DD to epoch time
by ambrus (Abbot) on Dec 04, 2015 at 22:26 UTC

    Are you sure that this is the exact code you're running? The line numbers don't seem to match.

    In any case, if it is the same code, then you never seem to handle the case when the regular expression actually matches. Maybe your input file contains lines where it doesn't match. Use an if statement around the assignment, such as

    if (($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; } else { # do something here when the line does not contain a date }

    You might also want to use some anchors in the regex if you want to make sure the whole line matches.

Re: convert a given YYYY-MM-DD to epoch time
by jeffa (Bishop) on Dec 04, 2015 at 22:28 UTC

    Your main requirement seems to simply be to "determine what day it was," and you can do that with several CPAN modules. I really like Time::Piece

    use strict; use warnings; use Time::Piece; my $date = shift || '1999-12-31'; my $time = Time::Piece->strptime( $date, '%Y-%m-%d' ); print $time, $/; print $time->fullday, $/;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found