如何修复 Perl 脚本中 strftime 的使用错误

How to fix Usage error in strftime in Perl script

我正在编写 Perl 脚本 signature.pl 来构建 PDF 字典和签名字典对象 这在声明的变量中有 PEM 和 PDF。我已经将 POSIX 用于 strftime

use strict;
use warnings;
use PDF::API2;
use PDF::API2::Basic::PDF::Utils;
use File::Slurp;
use POSIX;

use Crypt::OpenSSL::RSA;
use Crypt::OpenSSL::X509 qw/FORMAT_UNDEF FORMAT_ASN1 FORMAT_TEXT FORMAT_PEM/;
use Digest::SHA1 qw(sha1 sha1_hex);

my $add_mdp = 0;
my $input_filename = "signature.pdf";
my $tempfilename   = 'tmp.pdf';
my $outfilename    = 'tmp.pdf';

my $sig_algorithm  = 'pkcs7';
my $sig_length     = 20480;

my $cacert_filename   = "signature.pem";
my $x509_filename     = "signature.pem";
my $priv_key_filename = "signature.pem";

my $cacert   = Crypt::OpenSSL::X509->new_from_file($cacert_filename);
my $x509     = Crypt::OpenSSL::X509->new_from_file($x509_filename);
my $priv_key = read_file($priv_key_filename);

my $timestamp  = localtime;
my $tz         = POSIX::strftime("%z", $timestamp);
$tz            =~ s/([\+\-])(\d\d)(\d\d)/''/;
my $timestring = POSIX::strftime("D:%Y%m%d%H%M%S$tz", $timestamp);

my $location   = `hostname`;
chomp $location;

my $contact_info = PDFStr($x509->email());

my $signer_name  = PDFStr($x509->subject_name->as_string);

my $pdf = PDF::API2->open($input_filename);
my $p   = $pdf->{catalog}->{' parent'};

my $sigdict             = PDF::API2::Basic::PDF::Dict->new();
$sigdict->{Type}        = PDFName("Sig");
$sigdict->{Filter}      = PDFName("Adobe.PPKLite");
$sigdict->{Reason}      = PDFStr("Testing my PDF Signature Demo Tool");
$sigdict->{Name}        = $signer_name;
$sigdict->{ContactInfo} = $contact_info;
$sigdict->{Location}    = $location;
$sigdict->{M}           = $timestring;

if ($sig_algorithm eq 'rsa') {
    $sigdict->{SubFilter} = PDFName('adbe.x509.rsa.sha1');
    my @certs;
    push @certs, PDFStr $x509->as_string(FORMAT_ASN1);
    push @certs, PDFStr $cacert->as_string(FORMAT_ASN1);
    $sigdict->{Cert}      = PDFArray @certs if ($sig_algorithm eq 'rsa');
} else {
    $sigdict->{SubFilter} = PDFName('adbe.pkcs7.detached');
}

$sigdict->{Contents}  = PDFStrHex("[=10=]" x $sig_length);
$sigdict->{ByteRange} = PDF::API2::Basic::PDF::Literal->new("[0 00000000 00000000 00000000]");

if ($add_mdp) {
    my $sigrefdict                 = PDF::API2::Basic::PDF::Dict->new();
    $sigrefdict->{Type}            = PDFName("SigRef");
    $sigrefdict->{TransformMethod} = PDFName("DocMDP");
    $sigrefdict                    = $p->new_obj($sigrefdict);
    $sigdict->{Reference}          = PDFArray($sigrefdict);
}

$sigdict = $p->new_obj($sigdict);

my $sigannotdict         = PDF::API2::Basic::PDF::Dict->new();
my $sigformdict          = PDF::API2::Basic::PDF::Dict->new();

$sigannotdict->{Type}    = PDFName("Annot");
$sigannotdict->{Subtype} = PDFName("Widget");
$sigannotdict->{F}       = PDFNum(4);
$sigannotdict->{Parent}  = $sigformdict;
$sigannotdict->{Rect}    = PDF::API2::Basic::PDF::Literal->new("[0 0 0 0]");
$sigannotdict->{P}       = $pdf->openpage(1);
$sigannotdict->{H}       = PDFName("N");
$sigannotdict            = $p->new_obj($sigannotdict);

$sigformdict->{FT}       = PDFName("Sig");
$sigformdict->{T}        = PDFStr("Demo Signature");
$sigformdict->{V}        = $sigdict;
$sigformdict->{Kids}     = PDFArray($sigannotdict);
$sigformdict             = $p->new_obj($sigformdict);

if ($add_mdp) {
    my $permdict              = PDF::API2::Basic::PDF::Dict->new();
    $permdict->{DocMDP}       = $sigdict;
    $permdict                 = $p->new_obj($permdict);
    $pdf->{catalog}->{'Perm'} = $permdict;
}

my @formarray;
push @formarray, $sigformdict;
my $acroformdict          = PDF::API2::Basic::PDF::Dict->new();
$acroformdict->{Fields}   = PDFArray @formarray;
$acroformdict->{SigFlags} = PDFNum(3);
$acroformdict             = $p->new_obj($acroformdict);

$pdf->{catalog}->{'AcroForm'} = $acroformdict;
$pdf->{pdf}->out_obj($pdf->{catalog});


$pdf->saveas($tempfilename);
print "added AcroForm: $input_filename --> $tempfilename";

我收到如下警告:

Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) at signature.pl line 29

我使用了 https://mschuette.name/files/pdfsign.pl and https://www.perlmonks.org/?node_id=1171455

中的代码

有人可以帮忙吗?提前致谢

您似乎只是在获取某项内容的日期和时间戳。所以这是我以特殊格式用于当前日期和时间的代码:

###########################################################################
# July 2014
# Usage: $ts=getfulldatetime();
# Return current date and time in format: YYMMDD-hhmmss. Used on filenames.
sub getfulldatetime
{

my(@a,@b,$i,$j,$procname,$s,$t);
my($day,$mo,$sec,$year,$hr,$min);

$procname="getfulldatetime";
@a=localtime();
$day=sprintf("%02d",$a[3]);
$mo=sprintf("%02d",$a[4]+1); # Month is 0-11.

($sec,$min,$hr,$day, $mo, $year) = (localtime)[0,1,2,3,4,5];
$year=sprintf("%04d",$year);
$mo = sprintf '%02d', $mo+1;
$day   = sprintf '%02d', $day;
$s= ($year+1900).$mo.$day;
$s.="-".sprintf("%02d",$hr).sprintf("%02d",$min).sprintf("%02d",$sec);

return $s; # getfulldatetime
}

删除不需要的任何部分。这应该不需要任何特殊的 use 语句,它非常通用。

在列表上下文中,localtime returns 9 个元素的列表。然而,

In scalar context, localtime returns the ctime(3) value:

如果你打印 $timestamp,你会得到如下内容:

Tue Sep 10 15:37:22 2019

strftime,如您的错误消息所述(并且如文档所示)需要 7 个参数。因此,您应该在列表上下文中而不是在标量上下文中传递 what localtime returns 。即使用:

my @localtime  = localtime;
my $tz         = POSIX::strftime("%z", @localtime);
$tz            =~ s/([\+\-])(\d\d)(\d\d)/''/;
my $timestring = POSIX::strftime("D:%Y%m%d%H%M%S$tz", @localtime);

举一个小例子来重现我在回答中所说的内容:

my $time = localtime;
say strftime "%Y", $time;

会崩溃,而

my @time = localtime;
say strftime "%Y", @time;

会很好用。

正确的答案是在列表上下文中调用 localtime,但另一种方法是使用核心 Time::Piece 模块,它提供了一个 localtime 函数,该函数 returns 一个具有strftime 方法。

use strict;
use warnings;
use Time::Piece;

my $timepiece = localtime;
my $tz = $timepiece->strftime("%z"); #etc