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   997 use 5.010001;
  4         17  
4 4     4   23 use strict;
  4         8  
  4         88  
5 4     4   21 use warnings;
  4         7  
  4         1506  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2021-11-28'; # DATE
9             our $DIST = 'Data-Sah-Coerce'; # DIST
10             our $VERSION = '0.052'; # VERSION
11              
12             sub meta {
13             +{
14 14     14 0 139 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 54 my %args = @_;
23              
24 14         35 my $dt = $args{data_term};
25 14   50     42 my $coerce_to = $args{coerce_to} // 'float(epoch)';
26              
27 14         33 my $res = {};
28              
29 14         126 $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       79 if ($coerce_to eq 'float(epoch)') {
    100          
    50          
38 7         16 $res->{expr_coerce} = $dt;
39             } elsif ($coerce_to eq 'DateTime') {
40 4   50     40 $res->{modules}{DateTime} //= 0;
41 4         31 $res->{expr_coerce} = "DateTime->from_epoch(epoch => $dt)";
42             } elsif ($coerce_to eq 'Time::Moment') {
43 3   50     33 $res->{modules}{'Time::Moment'} //= 0;
44 3         15 $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         55 $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.052 of Data::Sah::Coerce::perl::To_date::From_float::epoch (from Perl distribution Data-Sah-Coerce), released on 2021-11-28.
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