File Coverage

blib/lib/Data/Sah/Coerce/perl/To_date/From_float/epoch.pm
Criterion Covered Total %
statement 21 22 95.4
branch 5 6 83.3
condition 3 6 50.0
subroutine 5 5 100.0
pod 0 2 0.0
total 34 41 82.9


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