DateTime::Format::CLDR 解析长捷克日期的问题

Posted

技术标签:

【中文标题】DateTime::Format::CLDR 解析长捷克日期的问题【英文标题】:DateTime::Format::CLDR issues parsing long Czech dates 【发布时间】:2016-01-23 23:22:13 【问题描述】:

我正在尝试使用 DateTime::Locale::cs_CZ 指定的 CLDR 长格式解析捷克日期。我看不出它应该失败的明显原因。

my $locale2 = DateTime::Locale->load('cs_CZ');
my $cldr5 = DateTime::Format::CLDR->new(
    pattern     => $locale2->date_format_long,
    locale      => $locale2,
    );

print $cldr5->pattern, "\n";
print $cldr5->parse_datetime('17. listopad 1989');

输出是

d. MMMM y

Use of uninitialized value in print at ./test-cldr.pl line 54.

我做错了什么?为什么不解析日期?

更新

这是我在命令行上实际使用的测试:

perl -MData::Dumper -MDateTime -MDateTime::Format::CLDR -wle'
    local $SIG__DIE__ = sub  print( Carp::longmess (shift) ); ;
    $x="19. září 1979";
    my $loc = DateTime::Locale->load("cs_CZ");
    print Dumper($loc), "\n";
    my $cldr = DateTime::Format::CLDR->new(
       pattern=>$loc->date_format_long, locale=>$loc, on_error => "croak");
    print Dumper($cldr->parse_datetime($x));
'

这是我得到的输出:

$VAR1 = bless( 
                 'en_language' => 'Czech',
                 'native_territory' => "\x10cesk\xe1 republika",
                 'en_territory' => 'Czech Republic',
                 'native_language' => "\x10de\x161tina",
                 'default_time_format_length' => 'medium',
                 'id' => 'cs_CZ',
                 'native_complete_name' => "\x10de\x161tina \x10cesk\xe1 republika",
                 'default_date_format_length' => 'medium',
                 'en_complete_name' => 'Czech Czech Republic'
               , 'DateTime::Locale::cs_CZ' );


Could not get datetime for 19. září 1979 (Error marked by 'HERE-->'): '19.  HERE-->září 1979' at -e line 7.
 at /usr/share/perl5/DateTime/Format/CLDR.pm line 965.
    DateTime::Format::CLDR::_local_croak(DateTime::Format::CLDR=HASH(0x166bcd8), "Could not get datetime for 19. z\xc3\xa1\xc5\x99\xc3\xad 1979 (Error marked by "...) called at /usr/share/perl5/DateTime/Format/CLDR.pm line 558
    DateTime::Format::CLDR::__ANON__("z\xc3\xa1\xc5\x99\xc3\xad 1979") called at /usr/share/perl5/DateTime/Format/CLDR.pm line 582
    DateTime::Format::CLDR::parse_datetime(DateTime::Format::CLDR=HASH(0x166bcd8), "19. z\xc3\xa1\xc5\x99\xc3\xad 1979") called at -e line 7

Could not get datetime for 19. září 1979 (Error marked by 'HERE-->'): '19.  HERE-->září 1979' at -e line 7.

【问题讨论】:

您定义了$locale2,但使用了$locale。你是use strict;use warnings; 吗? @glennjackman:你是对的。不幸的是,即使修复了这个问题,它仍然失败。 【参考方案1】:

17.列表板 1989

print(Dumper($self->_build_pattern())); 可以窥探模块的预期。在这种情况下,它会输出以下内容。

$VAR1 = [
          [
            qr/(3[01]|[12]\d|0?[1-9])/,
            'd',
            1
          ],
          '\\.',
          '\\s+',
          [
            "(listopadu|\x10dervence|prosince|b\x159ezna|kv\x11btna|\x10dervna|ledna|\xfanora|dubna|srpna|\x159\xedjna|z\xe1\x159\xed)",
            'M',
            4
          ],
          '\\s+',
          [
            qr/(-?\d1,4)/,
            'y',
            1
          ]
        ];

它需要listopadu,但不是listopad。我对捷克语一无所知,但该模块的行为似乎是correct。您可以在解析日期之前“修复”它们。

$date_string =~ s/\blistopad\b\K/u/;

19. září 1979

您实际上并没有通过září ("z\xe1\x159\xed")。您正在传递其 UTF-8 编码 ("z\xc3\xa1\xc5\x99\xc3\xad")。添加use utf8; 告诉 Perl 源代码是使用 UTF-8 编码的。

【讨论】:

好的,但是我发布的第一个测试没有使用 ASCII 以外的任何东西。 我显然比我想象的更讨厌自己的母语!我也很擅长提出正确的 SO 问题。 :) 如果可以的话,我会给你 10 票。你最终在所有方面都是对的。月份的属格形式 实际上用于我从中获取日期的来源。 不用担心。你添加的那个可执行位太棒了:)我希望所有问题都是可执行的。

以上是关于DateTime::Format::CLDR 解析长捷克日期的问题的主要内容,如果未能解决你的问题,请参考以下文章

第4.4节 Python解析与推导:列表解析字典解析集合解析

云解析DNS如何实现智能解析?

全民解析vip视频哪个好

mybatis源码配置文件解析之五:解析mappers标签(解析class属性)

什么是DNS域名解析,怎样解析?

DNS域名解析服务(正向解析反向解析)