File Coverage

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