DateTime::Locale包含各种语言环境和国家的日期和时间格式的非常全面的列表。我想用电子邮件给客户,这取决于客户来自哪个国家。如何在DateTime :: Locale中使用DateTime对象的格式?
不幸的是,从文档中了解如何实际使用中型或长型日期的函数有点困难。例如,DateTime::Locale::de_DE列出了这些文档date formats(节选):
Long 2008-02-05T18:30:30 = 5. Februar 2008 1995-12-22T09:05:02 = 22. Dezember 1995 -0010-09-15T04:44:23 = 15. September -10 Medium 2008-02-05T18:30:30 = 05.02.2008 1995-12-22T09:05:02 = 22.12.1995 -0010-09-15T04:44:23 = 15.09.-010
这是伟大的。根据DateTime::Locale::Base,语言环境对象中有方法获取这些格式:$locale->date_format_long()
和$locale->date_format_medium()
。
一些google搜索后,我来到了Sinan Ünür's blog,在那里他显示了这个代码(节选):
for my $locale (qw(ar da de en_GB es fr ru tr)) { $dt->set_locale($locale); print_date($dt); } sub print_date { my ($dt) = @_; my $locale = $dt->locale; printf( "In %s: %s\n", $locale->name, $dt->format_cldr($locale->date_format_full) ); }
这样出来的这些方法的格式是CLDR格式。凉。但是思南的表演看起来很乏味。总之,这将是:
for (qw(ar da de en_GB es fr ru tr)) {
my $dt2 = DateTime->now(locale => $_);
printf "%s: %s\n", $_, $dt2->format_cldr($dt2->locale->date_format_long);
}
为了使该短,我当然可以做这样的事情:
package DateTime;
sub stringify_long {
return $_[0]->format_cldr($_[0]->locale->date_format_long);
}
package Main;
use strict; use warnings;
use DateTime;
my $dt = DateTime->now(locale => 'de_DE');
print $dt->stringify_long;
但我并不想这样做。所以我的问题:有没有一种方法根据其语言环境中的这些格式之一使用我缺少的内置方法对DateTime对象进行串联化?
格式化程序似乎是一个不错的选择。感谢您的证明。如果我做很多事情,我主要是在寻找一个不太重复的选项。 – simbabque