line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
25
|
|
|
25
|
|
1078
|
use strict; |
|
25
|
|
|
|
|
61
|
|
|
25
|
|
|
|
|
1739
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package HTML::FormFu::Inflator::DateTime; |
4
|
|
|
|
|
|
|
$HTML::FormFu::Inflator::DateTime::VERSION = '2.07'; |
5
|
|
|
|
|
|
|
# ABSTRACT: DateTime inflator |
6
|
|
|
|
|
|
|
|
7
|
25
|
|
|
25
|
|
159
|
use Moose; |
|
25
|
|
|
|
|
57
|
|
|
25
|
|
|
|
|
209
|
|
8
|
25
|
|
|
25
|
|
173737
|
use MooseX::Attribute::Chained; |
|
25
|
|
|
|
|
88
|
|
|
25
|
|
|
|
|
1040
|
|
9
|
|
|
|
|
|
|
extends 'HTML::FormFu::Inflator'; |
10
|
|
|
|
|
|
|
|
11
|
25
|
|
|
25
|
|
152
|
use HTML::FormFu::Constants qw( $EMPTY_STR ); |
|
25
|
|
|
|
|
56
|
|
|
25
|
|
|
|
|
3259
|
|
12
|
25
|
|
|
25
|
|
7876
|
use DateTime::Format::Builder; |
|
25
|
|
|
|
|
6770223
|
|
|
25
|
|
|
|
|
303
|
|
13
|
25
|
|
|
25
|
|
954
|
use DateTime::Format::Strptime; |
|
25
|
|
|
|
|
71
|
|
|
25
|
|
|
|
|
205
|
|
14
|
25
|
|
|
25
|
|
1637
|
use Scalar::Util qw( reftype ); |
|
25
|
|
|
|
|
70
|
|
|
25
|
|
|
|
|
11728
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has strptime => ( is => 'rw', traits => ['Chained'] ); |
17
|
|
|
|
|
|
|
has time_zone => ( is => 'rw', traits => ['Chained'] ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has _builder => ( |
20
|
|
|
|
|
|
|
is => 'rw', |
21
|
|
|
|
|
|
|
default => sub { DateTime::Format::Builder->new }, |
22
|
|
|
|
|
|
|
lazy => 1, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub parser { |
26
|
26
|
|
|
26
|
1
|
84
|
my ( $self, $arg ) = @_; |
27
|
|
|
|
|
|
|
|
28
|
26
|
100
|
100
|
|
|
159
|
if ( exists $arg->{regex} && !ref $arg->{regex} ) { |
29
|
1
|
|
|
|
|
53
|
$arg->{regex} = qr/$arg->{regex}/; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
26
|
|
|
|
|
861
|
$self->_builder->parser($arg); |
33
|
|
|
|
|
|
|
|
34
|
26
|
|
|
|
|
53385
|
return $self; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub inflator { |
38
|
27
|
|
|
27
|
0
|
90
|
my ( $self, $value ) = @_; |
39
|
|
|
|
|
|
|
|
40
|
27
|
100
|
100
|
|
|
293
|
return if !defined $value || $value eq $EMPTY_STR; |
41
|
|
|
|
|
|
|
|
42
|
25
|
|
|
|
|
1233
|
my $dt = $self->_builder->parse_datetime($value); |
43
|
|
|
|
|
|
|
|
44
|
22
|
100
|
|
|
|
20839
|
if ( defined $self->time_zone ) { |
45
|
1
|
|
|
|
|
31
|
$dt->set_time_zone( $self->time_zone ); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
22
|
100
|
|
|
|
15916
|
if ( defined $self->strptime ) { |
49
|
11
|
|
|
|
|
335
|
my $strptime = $self->strptime; |
50
|
11
|
|
|
|
|
27
|
my %args; |
51
|
|
|
|
|
|
|
|
52
|
11
|
100
|
100
|
|
|
99
|
if ( ( reftype($strptime) || '' ) eq 'HASH' ) { |
53
|
2
|
|
|
|
|
11
|
%args = %$strptime; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
9
|
|
|
|
|
42
|
%args = ( pattern => $strptime ); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Make strptime format the date with the specified time_zone, |
60
|
|
|
|
|
|
|
# this is most likely what the user wants |
61
|
11
|
100
|
|
|
|
360
|
if ( defined $self->time_zone ) { |
62
|
1
|
|
|
|
|
31
|
$args{time_zone} = $self->time_zone; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
11
|
50
|
33
|
|
|
146
|
if ( !exists $args{locale} |
66
|
|
|
|
|
|
|
&& defined( my $locale = $self->locale ) ) |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
|
|
|
|
0
|
$args{locale} = $locale; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
11
|
|
|
|
|
79
|
my $formatter = DateTime::Format::Strptime->new(%args); |
72
|
|
|
|
|
|
|
|
73
|
11
|
|
|
|
|
16213
|
$dt->set_formatter($formatter); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
22
|
|
|
|
|
761
|
return $dt; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub clone { |
80
|
2
|
|
|
2
|
0
|
6
|
my $self = shift; |
81
|
|
|
|
|
|
|
|
82
|
2
|
|
|
|
|
29
|
my $clone = $self->SUPER::clone(@_); |
83
|
|
|
|
|
|
|
|
84
|
2
|
|
|
|
|
82
|
$clone->_builder( $self->_builder->clone ); |
85
|
|
|
|
|
|
|
|
86
|
2
|
|
|
|
|
65
|
return $clone; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=pod |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=encoding UTF-8 |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 NAME |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
HTML::FormFu::Inflator::DateTime - DateTime inflator |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 VERSION |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
version 2.07 |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 SYNOPSIS |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
--- |
110
|
|
|
|
|
|
|
elements: |
111
|
|
|
|
|
|
|
- type: Text |
112
|
|
|
|
|
|
|
name: start_date |
113
|
|
|
|
|
|
|
inflators: |
114
|
|
|
|
|
|
|
- type: DateTime |
115
|
|
|
|
|
|
|
parser: |
116
|
|
|
|
|
|
|
strptime: '%d-%m-%Y' |
117
|
|
|
|
|
|
|
strptime: |
118
|
|
|
|
|
|
|
pattern: '%d-%b-%Y' |
119
|
|
|
|
|
|
|
locale: de |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
- type: Text |
122
|
|
|
|
|
|
|
name: end_time |
123
|
|
|
|
|
|
|
inflators: |
124
|
|
|
|
|
|
|
- type: DateTime |
125
|
|
|
|
|
|
|
time_zone: Europe/Rome |
126
|
|
|
|
|
|
|
parser: |
127
|
|
|
|
|
|
|
regex: '^ (\d{2}) - (\d{2}) - (\d{4}) $' |
128
|
|
|
|
|
|
|
params: [day, month, year] |
129
|
|
|
|
|
|
|
strptime: '%d-%m-%Y' |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
An example of using the same parser declaration for both a DateTime |
132
|
|
|
|
|
|
|
constraint and a DateTime inflator, using YAML references: |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
--- |
135
|
|
|
|
|
|
|
elements: |
136
|
|
|
|
|
|
|
- type: Text |
137
|
|
|
|
|
|
|
name: date |
138
|
|
|
|
|
|
|
constraints: |
139
|
|
|
|
|
|
|
- type: DateTime |
140
|
|
|
|
|
|
|
parser: &PARSER |
141
|
|
|
|
|
|
|
strptime: '%d-%m-%Y' |
142
|
|
|
|
|
|
|
inflators: |
143
|
|
|
|
|
|
|
- type: DateTime |
144
|
|
|
|
|
|
|
parser: *PARSER |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 DESCRIPTION |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Inflate dates into L<DateTime> objects. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
For a corresponding deflator, see L<HTML::FormFu::Deflator::Strftime>. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 METHODS |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 parser |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Arguments: \%args |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Required. Define the expected input string, so L<DateTime::Format::Builder> |
159
|
|
|
|
|
|
|
knows how to inflate it into a L<DateTime> object. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Accepts arguments to be passed to L<DateTime::Format::Builder/parser>. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 strptime |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Arguments: \%args |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Arguments: $string |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Optional. Define the format that should be used if the L<DateTime> object is |
170
|
|
|
|
|
|
|
stringified. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 time_zone |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Arguments: $string |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Optional. You can pass along a time_zone in which the DateTime will be |
177
|
|
|
|
|
|
|
created. This is useful if the string to parse does not contain time zone |
178
|
|
|
|
|
|
|
information and you want the DateTime to be in a specific zone instead |
179
|
|
|
|
|
|
|
of the floating one (which is likely). |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Accepts a hashref of arguments to be passed to |
182
|
|
|
|
|
|
|
L<DateTime::Format::Strptime/new>. Alternatively, accepts a single string |
183
|
|
|
|
|
|
|
argument, suitable for passing to |
184
|
|
|
|
|
|
|
C<< DateTime::Format::Strptime->new( pattern => $string ) >>. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 AUTHOR |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Carl Franks, C<cfranks@cpan.org> |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 LICENSE |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
This library is free software, you can redistribute it and/or modify it under |
193
|
|
|
|
|
|
|
the same terms as Perl itself. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 AUTHOR |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Carl Franks <cpan@fireartist.com> |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
This software is copyright (c) 2018 by Carl Franks. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
204
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=cut |