File Coverage

blib/lib/Data/Sah/Coerce/perl/To_date/From_str/flexible.pm
Criterion Covered Total %
statement 17 17 100.0
branch 5 6 83.3
condition 3 4 75.0
subroutine 5 5 100.0
pod 0 2 0.0
total 30 34 88.2


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::To_date::From_str::flexible;
2              
3 1     1   658579 use 5.010001;
  1         6  
4 1     1   6 use strict;
  1         2  
  1         21  
5 1     1   5 use warnings;
  1         3  
  1         278  
6              
7             # AUTHOR
8             our $DATE = '2021-09-07'; # DATE
9             our $DIST = 'Data-Sah-Coerce-perl-To_date-From_str-flexible'; # DIST
10             our $VERSION = '0.009'; # VERSION
11              
12             sub meta {
13             +{
14 4     4 0 43005 v => 4,
15             summary => 'Coerce date from string parsed by DateTime::Format::Flexible (UTC time zone)',
16             might_fail => 1,
17             prio => 60, # a bit lower than normal
18             precludes => [qr/\A(From_str::alami(_.+)?|From_str::natural)\z/],
19             };
20             }
21              
22             sub coerce {
23 4     4 0 50 my %args = @_;
24              
25 4         12 my $dt = $args{data_term};
26 4   100     16 my $coerce_to = $args{coerce_to} // 'float(epoch)';
27              
28 4         10 my $res = {};
29              
30 4         12 $res->{expr_match} = "!ref($dt)";
31 4   50     25 $res->{modules}{"DateTime::Format::Flexible"} //= 0;
32 4 50       23 $res->{expr_coerce} = join(
    100          
    100          
33             "",
34             "do { my \$datetime; eval { \$datetime = DateTime::Format::Flexible->parse_datetime($dt)->set_time_zone('UTC') }; ",
35             ($coerce_to eq 'float(epoch)' ? "if (\$@) { ['Invalid date format'] } else { [undef, \$datetime->epoch] } " :
36             $coerce_to eq 'Time::Moment' ? "if (\$@) { ['Invalid date format'] } else { [undef, Time::Moment->from_object(\$datetime) ] } " :
37             $coerce_to eq 'DateTime' ? "if (\$@) { ['Invalid date format'] } else { [undef, \$datetime] } " :
38             (die "BUG: Unknown coerce_to '$coerce_to'")),
39             "}",
40             );
41              
42 4         14 $res;
43             }
44              
45             1;
46             # ABSTRACT: Coerce date from string parsed by DateTime::Format::Flexible (UTC time zone)
47              
48             __END__
49              
50             =pod
51              
52             =encoding UTF-8
53              
54             =head1 NAME
55              
56             Data::Sah::Coerce::perl::To_date::From_str::flexible - Coerce date from string parsed by DateTime::Format::Flexible (UTC time zone)
57              
58             =head1 VERSION
59              
60             This document describes version 0.009 of Data::Sah::Coerce::perl::To_date::From_str::flexible (from Perl distribution Data-Sah-Coerce-perl-To_date-From_str-flexible), released on 2021-09-07.
61              
62             =head1 SYNOPSIS
63              
64             To use in a Sah schema:
65              
66             ["date",{"x.perl.coerce_rules"=>["From_str::flexible"]}]
67              
68             =head1 DESCRIPTION
69              
70             To clarify your intent, please use L<Data::Sah::Coerce::perl::To_date::From_str::flexible_utc> instead.
71              
72             =for Pod::Coverage ^(meta|coerce)$
73              
74             =head1 HOMEPAGE
75              
76             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-Coerce-perl-To_date-From_str-flexible>.
77              
78             =head1 SOURCE
79              
80             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-Coerce-perl-To_date-From_str-flexible>.
81              
82             =head1 SEE ALSO
83              
84             L<DateTime::Format::Flexible> - the backend used to parse the date strings.
85              
86             L<Data::Sah::Coerce::perl::To_date::From_str::flexible_utc>
87              
88             L<Data::Sah::Coerce::perl::To_date::From_str::flexible_local>
89              
90             L<Data::Sah::Coerce::perl::To_date::From_str::natural>
91              
92             =head1 AUTHOR
93              
94             perlancar <perlancar@cpan.org>
95              
96             =head1 CONTRIBUTING
97              
98              
99             To contribute, you can send patches by email/via RT, or send pull requests on
100             GitHub.
101              
102             Most of the time, you don't need to build the distribution yourself. You can
103             simply modify the code, then test via:
104              
105             % prove -l
106              
107             If you want to build the distribution (e.g. to try to install it locally on your
108             system), you can install L<Dist::Zilla>,
109             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
110             Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required
111             beyond that are considered a bug and can be reported to me.
112              
113             =head1 COPYRIGHT AND LICENSE
114              
115             This software is copyright (c) 2021, 2019, 2018, 2016 by perlancar <perlancar@cpan.org>.
116              
117             This is free software; you can redistribute it and/or modify it under
118             the same terms as the Perl 5 programming language system itself.
119              
120             =head1 BUGS
121              
122             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-Coerce-perl-To_date-From_str-flexible>
123              
124             When submitting a bug or request, please include a test-file or a
125             patch to an existing test-file that illustrates the bug or desired
126             feature.
127              
128             =cut