| 1 | # ============================================================================ | ||||||||||||||||||||
| 2 | package CatalystX::I18N::Role::DateTime; | ||||||||||||||||||||
| 3 | # ============================================================================ | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 4 | ▶ | use CatalystX::I18N::TypeConstraints; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 4 | ▶ | use MooseX::Types::DateTime; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 4 | ▶ | use Carp qw(carp); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 4 | ▶ | use DateTime; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 4 | ▶ | use DateTime::Format::CLDR; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 4 | ▶ | use DateTime::TimeZone; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 4 | ▶ | use DateTime::Locale; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 4 | has 'i18n_datetime_timezone' => ( | |||||||||||||||||||
| 18 | is => 'rw', | ||||||||||||||||||||
| 19 | isa => 'DateTime::TimeZone', | ||||||||||||||||||||
| 20 | lazy_build=> 1, | ||||||||||||||||||||
| 21 | builder => '_build_i18n_datetime_timezone', | ||||||||||||||||||||
| 22 | clearer => '_clear_i18n_datetime_timezone', | ||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 4 | has 'i18n_datetime_locale' => ( | |||||||||||||||||||
| 26 | is => 'rw', | ||||||||||||||||||||
| 27 | isa => 'DateTime::Locale', | ||||||||||||||||||||
| 28 | lazy_build=> 1, | ||||||||||||||||||||
| 29 | builder => '_build_i18n_datetime_locale', | ||||||||||||||||||||
| 30 | clearer => '_clear_i18n_datetime_locale', | ||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 4 | has 'i18n_datetime_format_date' => ( | |||||||||||||||||||
| 34 | is => 'rw', | ||||||||||||||||||||
| 35 | isa => 'DateTime::Format::CLDR', | ||||||||||||||||||||
| 36 | lazy_build=> 1, | ||||||||||||||||||||
| 37 | builder => '_build_i18n_datetime_format_date', | ||||||||||||||||||||
| 38 | clearer => '_clear_i18n_datetime_format_date', | ||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 4 | has 'i18n_datetime_format_datetime' => ( | |||||||||||||||||||
| 42 | is => 'rw', | ||||||||||||||||||||
| 43 | isa => 'DateTime::Format::CLDR', | ||||||||||||||||||||
| 44 | lazy_build=> 1, | ||||||||||||||||||||
| 45 | builder => '_build_i18n_datetime_format_datetime', | ||||||||||||||||||||
| 46 | clearer => '_clear_i18n_datetime_format_datetime', | ||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 0 | ▶ | sub i18n_timezone { | ||||||||||||||||||
|
Subroutine
i18n_timezone: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 50 | 0 | carp "Method 'i18n_timezone' is deprecated: Use i18n_datetime_timezone instead"; | |||||||||||||||||||
| 51 | 0 | return shift->i18n_datetime_timezone(@_); | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 6 | ▶ | sub i18n_datetime_now { | ||||||||||||||||||
|
Subroutine
i18n_datetime_now: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 55 | 6 | my ($c) = @_; | |||||||||||||||||||
| 56 | 6 | return DateTime->from_epoch( | |||||||||||||||||||
| 57 | epoch => time(), | ||||||||||||||||||||
| 58 | time_zone => $c->i18n_datetime_timezone, | ||||||||||||||||||||
| 59 | locale => $c->i18n_datetime_locale, | ||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | ▶ | sub i18n_datetime_today { | ||||||||||||||||||
|
Subroutine
i18n_datetime_today: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 64 | 0 | my ($c) = @_; | |||||||||||||||||||
| 65 | 0 | return $c->now->truncate( to => 'day' ); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 34 | ▶ | after 'set_locale' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 69 | 34 | my ($c,$locale) = @_; | |||||||||||||||||||
| 70 | 34 | $c->_clear_i18n_datetime_timezone(); | |||||||||||||||||||
| 71 | 34 | $c->_clear_i18n_datetime_locale(); | |||||||||||||||||||
| 72 | 34 | $c->_clear_i18n_datetime_format_date(); | |||||||||||||||||||
| 73 | 34 | $c->_clear_i18n_datetime_format_datetime(); | |||||||||||||||||||
| 74 | 4 | }; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 7 | ▶ | sub _build_i18n_datetime_timezone { | ||||||||||||||||||
|
Subroutine
_build_i18n_datetime_timezone: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 77 | 7 | my ($c) = @_; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 7 | my $config = $c->i18n_config; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 7 | $c->_clear_i18n_datetime_format_date(); | |||||||||||||||||||
| 82 | 7 | $c->_clear_i18n_datetime_format_datetime(); | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 7 | ▶ | return DateTime::TimeZone->new( name => $config->{timezone} || 'floating' ); | ||||||||||||||||||
|
Condition100%2/2
$config->{'timezone'} || 'floating'
MC/DC100%1/1
$config->{'timezone'} || 'floating'
$config->{'timezone'}
Truth table100%2/2
A
A= $config->{'timezone'}
| |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 2 | ▶ | sub _build_i18n_datetime_locale { | ||||||||||||||||||
|
Subroutine
_build_i18n_datetime_locale: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 88 | 2 | my ($c) = @_; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 2 | $c->_clear_i18n_datetime_format_date(); | |||||||||||||||||||
| 91 | 2 | $c->_clear_i18n_datetime_format_datetime(); | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 2 | return DateTime::Locale->load( $c->locale ); | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | ▶ | sub _build_i18n_datetime_format_date { | ||||||||||||||||||
|
Subroutine
_build_i18n_datetime_format_date: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 97 | 0 | my ($c) = @_; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | my $config = $c->i18n_config; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | my $datetime_locale = $c->i18n_datetime_locale; | |||||||||||||||||||
| 102 | 0 | my $datetime_timezone = $c->i18n_datetime_timezone; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | # Set datetime_format_date | ||||||||||||||||||||
| 105 | my $datetime_format_date = | ||||||||||||||||||||
| 106 | $config->{format_date} || | ||||||||||||||||||||
| 107 | 0 | ▶ | $datetime_locale->date_format_medium; | ||||||||||||||||||
|
Condition0%0/3
$config->{'format_date'} || $datetime_locale->date_format_medium
MC/DC0%0/2
$config->{'format_date'} || $datetime_locale->date_format_medium
$config->{'format_date'}
$datetime_locale->date_format_medium
Truth table0%0/3
A || B
A= $config->{'format_date'}B= $datetime_locale->date_format_medium
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | return DateTime::Format::CLDR->new( | |||||||||||||||||||
| 110 | locale => $datetime_locale, | ||||||||||||||||||||
| 111 | time_zone => $datetime_timezone, | ||||||||||||||||||||
| 112 | pattern => $datetime_format_date | ||||||||||||||||||||
| 113 | ) | ||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | ▶ | sub _build_i18n_datetime_format_datetime { | ||||||||||||||||||
|
Subroutine
_build_i18n_datetime_format_datetime: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 117 | 0 | my ($c) = @_; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 0 | my $config = $c->i18n_config; | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 0 | my $datetime_locale = $c->i18n_datetime_locale; | |||||||||||||||||||
| 122 | 0 | my $datetime_timezone = $c->i18n_datetime_timezone; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | # Set datetime_format_date | ||||||||||||||||||||
| 125 | my $datetime_format_datetime = | ||||||||||||||||||||
| 126 | $config->{format_datetime} || | ||||||||||||||||||||
| 127 | 0 | ▶ | $datetime_locale->datetime_format_medium; | ||||||||||||||||||
|
Condition0%0/3
$config->{'format_datetime'} || $datetime_locale->datetime_format_medium
MC/DC0%0/2
$config->{'format_datetime'} || $datetime_locale->datetime_format_medium
$config->{'format_datetime'}
$datetime_locale->datetime_format_medium
Truth table0%0/3
A || B
A= $config->{'format_datetime'}B= $datetime_locale->datetime_format_medium
| |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 0 | return DateTime::Format::CLDR->new( | |||||||||||||||||||
| 130 | locale => $datetime_locale, | ||||||||||||||||||||
| 131 | time_zone => $datetime_timezone, | ||||||||||||||||||||
| 132 | pattern => $datetime_format_datetime | ||||||||||||||||||||
| 133 | ) | ||||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 4 | ▶ | no Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 137 | 4 | 1; | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =encoding utf8 | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =head1 NAME | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | CatalystX::I18N::Role::DateTime - Support for I18N datetime | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head1 SYNOPSIS | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | package MyApp::Catalyst; | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | use Catalyst qw/MyPlugins | ||||||||||||||||||||
| 150 | +CatalystX::I18N::Role::Base | ||||||||||||||||||||
| 151 | +CatalystX::I18N::Role::DateTime/; | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | package MyApp::Catalyst::Controller::Main; | ||||||||||||||||||||
| 154 | use strict; | ||||||||||||||||||||
| 155 | use warnings; | ||||||||||||||||||||
| 156 | use parent qw/Catalyst::Controller/; | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | sub action : Local { | ||||||||||||||||||||
| 159 | my ($self,$c) = @_; | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | $c->stash->{timestamp} = $c->i18n_datetime_format_date->format_datetime($datetime); | ||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head1 DESCRIPTION | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | This role add support for localised datetime to your Catalyst application. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | Most methods are lazy. This means that the values will be only calculated | ||||||||||||||||||||
| 169 | upon the first call of the method. | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | Most settings will be taken from L<DateTime::Locale> but | ||||||||||||||||||||
| 172 | can be overdriven in your Catalyst I18N configuration: | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | # Add I18N configuration | ||||||||||||||||||||
| 175 | __PACKAGE__->config( | ||||||||||||||||||||
| 176 | name => 'MyApp', | ||||||||||||||||||||
| 177 | I18N => { | ||||||||||||||||||||
| 178 | default_locale => 'de_AT', | ||||||||||||||||||||
| 179 | locales => { | ||||||||||||||||||||
| 180 | 'de_AT' => { | ||||||||||||||||||||
| 181 | timezone => 'Europe/Vienna', # default 'floating' | ||||||||||||||||||||
| 182 | format_date => 'dd.MM.yyyy', # default date_format_medium from DateTime::Locale | ||||||||||||||||||||
| 183 | format_datetime => 'dd.MM.yyyy uma HH:mm', # default datetime_format_medium from DateTime::Locale | ||||||||||||||||||||
| 184 | }, | ||||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | }, | ||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | =head1 METHODS | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =head3 i18n_datetime_today | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | my $dt = $c->i18n_datetime_today | ||||||||||||||||||||
| 194 | say $dt->dmy; | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | Returns the current date as a L<DateTime> object with the current | ||||||||||||||||||||
| 197 | timezone and locale set. | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =head3 i18n_datetime_now | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | my $dt = $c->i18n_datetime_now | ||||||||||||||||||||
| 202 | say $dt->hms; | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | Returns the current timestamp as a L<DateTime> object with the current | ||||||||||||||||||||
| 205 | timezone and locale set. | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head3 i18n_datetime_timezone | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | Returns/sets the current timezone as a L<DateTime::TimeZone> object. The | ||||||||||||||||||||
| 210 | timezone for each locale can be defined in the I18N configuration. | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | If no timezone is set L<DateTime::TimeZone::Floating> will be used. | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =head3 i18n_datetime_locale | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | Returns/sets the current datetime locale as a L<DateTime::Locale> object. | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | =head3 i18n_datetime_format_date | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | my $date = $c->i18n_datetime_format_date->format_datetime($date); | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | Returns a L<DateTime::Format::CLDR> object for parsing and printig | ||||||||||||||||||||
| 223 | localised date data. | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | The format for each locale can either be set via the | ||||||||||||||||||||
| 226 | C<format_date> coniguration key, or will be taken from the | ||||||||||||||||||||
| 227 | C<date_format_medium> method in the current L<DateTime::Locale> object. | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | =head3 i18n_datetime_format_datetime | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | my $datetime = $c->i18n_datetime_format_datetime->format_datetime($datetime); | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | Returns a L<DateTime::Format::CLDR> object for parsing and printig | ||||||||||||||||||||
| 234 | localised datetime data. | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | The format for each locale can either be set via the | ||||||||||||||||||||
| 237 | C<format_datetime> coniguration key, or will be taken from the | ||||||||||||||||||||
| 238 | C<datetime_format_medium> method in the current L<DateTime::Locale> object. | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | =head1 SEE ALSO | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | L<DateTime::Format::CLDR>, L<DateTime::Locale>, L<DateTime::TimeZone> | ||||||||||||||||||||
| 243 | and L<DateTime> | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =head1 AUTHOR | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | Maroš Kollár | ||||||||||||||||||||
| 248 | CPAN ID: MAROS | ||||||||||||||||||||
| 249 | maros [at] k-1.com | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | L<http://www.k-1.com> | ||||||||||||||||||||