How do I convert a date into epoch time in Perl?
Solution 1
The error message says it cannot find the module Date::Parse in your Perl library include path (@INC).
The module is available from CPAN (Comprehensive Perl Archive Network). If you need a Perl module that is not included in the base install, typically (>90%) it is available from CPAN, the de facto Perl module archive site.
Your question is addressed with the CPAN module (the CPAN module is used to retrieve modules from CPAN) documentation. I suggest starting with FAQ question 5, "I am not root, how can I install a module in a personal directory?"
Solution 2
You should be able to do this with Time::Piece which is core on Perl 5.10 (correction here: it's not core in 5.8). Here's an example from my machine. See man strftime
, man strptime
and perldoc Time::Piece
. In the Time::Piece
docs, you want to check the section Date Parsing. (The conversion details may vary on a Solaris machine. I'm on OS X right now at work.)
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $date = Time::Piece->strptime("Tue Apr 7 12:46:59 2009",
"%a %b %e %H:%M:%S %Y");
my $epoch_date = $date->epoch;
print "$epoch_date\n";
Edit: The parser chokes on CDT (%Z doesn't appear to recognize it), so you may need to do this in two steps. The following works for me:
my $string = "Mon Mar 27 05:54:08 CDT 2009";
$string =~ s/CDT/-0500/; # Replace timezone with offset from UTC
my $date = Time::Piece->strptime($string, "%a %b %e %H:%M:%S %z %Y");
my $epoch_date = $date->epoch;
print "$epoch_date\n";
However, now we have the additional problem of CDT => -0500 versus CST => -0600. I officially hate daylight savings.
Solution 3
Are you sure that's the right error message? Date::Parse is an entirely separate module from Time::ParseDate. They provide similar functionality, but don't use each other.
You can use Module::CoreList to get a list of the modules that shipped with 5.8.8. I don't know if Solaris 10 included any additional modules.
You might have to copy the guts out of a suitable module and paste it into your script. (Remember that a Perl file may have multiple package
statements.)
Solution 4
Since you need to ship the program to a customer who will not install modules for you, you will need to use PAR::Packer to create a native executable that contains all of your dependencies (or use only Core Modules).
Comments
-
pavanlimo almost 2 years
Solaris 10 doesn't seem to like me a lot. I am trying to run a simple script to accept date and return epoch of that date:
#!/usr/bin/perl -w use strict; use Time::ParseDate; my $date1 = "Mon Mar 27 05:54:08 CDT 2009"; #Convert to seconds since start of epoch my $time1 = parsedate($date1); print $time1;
Works perfectly fine on RHEL box, but gets screwed on Solaris(both have 5.8.8 Perl), giving the following error message.
Can't locate Date/Parse.pm in @INC (@INC contains: /usr/perl5/5.8.4/lib/sun4-solaris-64int /usr/perl5/5.8.4/lib /usr/perl5/site_perl/5.8.4/sun4-solaris-64int /usr/perl5/site_perl/5.8.4 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.8.4/sun4-solaris-64int /usr/perl5/vendor_perl/5.8.4 /usr/perl5/vendor_perl .) at try1.pl line 3. BEGIN failed--compilation aborted at try1.pl line 3.
Whats wrong here?.. how to correct this?.
Oh.. almost forgot, I cannot alter/install/modify anything on this Solaris box, this script needs to be shipped to a customer who runs Solaris 10!. So asking him to install a module is definitely not an option. :(
-
pavanlimo about 15 yearsMesg on RHEL & Solaris Can't locate Time/Piece.pm in @INC (@INC contains: /usr/lib /usr/lib/site_perl /usr/local/sendmail/perl-1.3/lib/5.8.3/i686-linux /usr/local/sendmail/perl-1.3/lib/site_perl/5.8.3/i686-linux /usr/local/sendmail/perl-1.3/lib/site_perl .) at try2.pl line 4.
-
cjm about 15 yearsAccording to Module::CoreList, Time::Piece did not become a core module until 5.9.5, which was part of the development series leading up to Perl 5.10. It works with 5.8, but it didn't ship with it. (OS X might have included it as an extra.)
-
Telemachus about 15 yearsNo, actually, you're right. I was not careful enough about which core modules I was checking. It's not present in 5.8. Apologies for the mistake.