如何在Perl中将日期转换为纪元时间?

时间:2022-05-07 15:48:11

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:

Solaris 10似乎不太喜欢我。我正在尝试运行一个简单的脚本来接受日期并返回该日期的时代:

#!/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.

在RHEL盒子上工作得很好,但是在Solaris上都搞砸了(都有5.8.8 Perl),给出了以下错误信息。

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.

无法在@INC中找到Date / Parse.pm(@INC包含:/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。)在try1.pl第3行.BEGIN失败 - 编译在try1.pl第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. :(

哦..差点忘了,我无法在这个Solaris盒子上修改/安装/修改任何东西,这个脚本需要运送给运行Solaris 10的客户!所以要求他安装模块绝对不是一个选择。 :(

6 个解决方案

#1


The error message says it cannot find the module Date::Parse in your Perl library include path (@INC).

错误消息说它无法在Perl库中找到模块Date :: Parse包含路径(@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.

该模块可从CPAN(Comprehensive Perl Archive Network)获得。如果您需要一个未包含在基本安装中的Perl模块,通常(> 90%)可以从事实上的Perl模块归档站点CPAN获得。

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?"

您的问题通过CPAN模块解决(CPAN模块用于从CPAN检索模块)文档。我建议从FAQ问题5开始,“我不是root用户,如何在个人目录中安装模块?”

#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.)

你应该可以用Time :: Piece来做到这一点,它是Perl 5.10的核心(在这里进行修正:它不是5.8中的核心)。这是我机器上的一个例子。参见man strftime,man strptime和perldoc Time :: Piece。在Time :: Piece文档中,您需要检查Date Scarsing部分。 (转换细节可能在Solaris计算机上有所不同。我现在正在使用OS X.)

#!/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:

编辑:解析器在CDT上阻塞(%Z似乎不识别它),因此您可能需要分两步完成此操作。以下为我工作:

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.

但是,现在我们还有CDT => -0500而CST => -0600的额外问题。我正式讨厌夏令时。

#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.

你确定这是正确的错误信息吗? Date :: Parse是一个与Time :: ParseDate完全独立的模块。它们提供类似的功能,但不要互相使用。

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.

您可以使用Module :: CoreList获取5.8.8附带的模块列表。我不知道Solaris 10是否包含任何其他模块。

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.)

您可能需要从适当的模块中复制内容并将其粘贴到脚本中。 (请记住,Perl文件可能有多个包语句。)

#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).

由于您需要将程序发送给不会为您安装模块的客户,因此您需要使用PAR :: Packer来创建包含所有依赖项(或仅使用核心模块)的本机可执行文件。

#5


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

Solaris 10似乎不太喜欢我。我正在尝试运行一个简单的脚本来接受日期并返回该日期的时代

Silly question, but what is the source of the input date string? If the string is fetched from the system your code can be a lot more simple.

愚蠢的问题,但输入日期字符串的来源是什么?如果从系统中获取字符串,则代码可以简单得多。

#6


Old thread, but this may help people with the same problem. Use the timelocal function in the standard Time::Local module. See perlfaq 4

旧线程,但这可能会帮助人们同样的问题。使用标准Time :: Local模块中的timelocal函数。见perlfaq 4

#1


The error message says it cannot find the module Date::Parse in your Perl library include path (@INC).

错误消息说它无法在Perl库中找到模块Date :: Parse包含路径(@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.

该模块可从CPAN(Comprehensive Perl Archive Network)获得。如果您需要一个未包含在基本安装中的Perl模块,通常(> 90%)可以从事实上的Perl模块归档站点CPAN获得。

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?"

您的问题通过CPAN模块解决(CPAN模块用于从CPAN检索模块)文档。我建议从FAQ问题5开始,“我不是root用户,如何在个人目录中安装模块?”

#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.)

你应该可以用Time :: Piece来做到这一点,它是Perl 5.10的核心(在这里进行修正:它不是5.8中的核心)。这是我机器上的一个例子。参见man strftime,man strptime和perldoc Time :: Piece。在Time :: Piece文档中,您需要检查Date Scarsing部分。 (转换细节可能在Solaris计算机上有所不同。我现在正在使用OS X.)

#!/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:

编辑:解析器在CDT上阻塞(%Z似乎不识别它),因此您可能需要分两步完成此操作。以下为我工作:

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.

但是,现在我们还有CDT => -0500而CST => -0600的额外问题。我正式讨厌夏令时。

#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.

你确定这是正确的错误信息吗? Date :: Parse是一个与Time :: ParseDate完全独立的模块。它们提供类似的功能,但不要互相使用。

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.

您可以使用Module :: CoreList获取5.8.8附带的模块列表。我不知道Solaris 10是否包含任何其他模块。

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.)

您可能需要从适当的模块中复制内容并将其粘贴到脚本中。 (请记住,Perl文件可能有多个包语句。)

#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).

由于您需要将程序发送给不会为您安装模块的客户,因此您需要使用PAR :: Packer来创建包含所有依赖项(或仅使用核心模块)的本机可执行文件。

#5


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

Solaris 10似乎不太喜欢我。我正在尝试运行一个简单的脚本来接受日期并返回该日期的时代

Silly question, but what is the source of the input date string? If the string is fetched from the system your code can be a lot more simple.

愚蠢的问题,但输入日期字符串的来源是什么?如果从系统中获取字符串,则代码可以简单得多。

#6


Old thread, but this may help people with the same problem. Use the timelocal function in the standard Time::Local module. See perlfaq 4

旧线程,但这可能会帮助人们同样的问题。使用标准Time :: Local模块中的timelocal函数。见perlfaq 4