File Coverage

blib/lib/Data/Sah/Format/perl/iso8601_datetime.pm
Criterion Covered Total %
statement 16 16 100.0
branch 4 4 100.0
condition 6 6 100.0
subroutine 4 4 100.0
pod 0 1 0.0
total 30 31 96.7


line stmt bran cond sub pod time code
1             package Data::Sah::Format::perl::iso8601_datetime;
2              
3             our $DATE = '2017-07-10'; # DATE
4             our $VERSION = '0.003'; # VERSION
5              
6 1     1   32 use 5.010001;
  1         7  
7 1     1   7 use strict;
  1         4  
  1         31  
8 1     1   7 use warnings;
  1         3  
  1         189  
9              
10             sub format {
11 5     5 0 35 my %args = @_;
12              
13 5         18 my $dt = $args{data_term};
14 5   100     43 my $fargs = $args{args} // {};
15              
16 5   100     39 my $format_datetime = $fargs->{format_datetime} // 1;
17 5   100     35 my $format_timemoment = $fargs->{format_timemoment} // 1;
18              
19 5         17 my $res = {};
20              
21 5 100       75 $res->{expr} = join(
    100          
22             "",
23             "$dt =~ /\\A\\d+(\\.\\d+)?\\z/ ? do { my \@t = gmtime($dt); sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ', \$t[5]+1900, \$t[4]+1, \$t[3], \$t[2], \$t[1], \$t[0]) } : ",
24             ($format_datetime ?
25             # convert to UTC first
26             "ref($dt) eq 'DateTime' ? DateTime->from_epoch(epoch => $dt\->epoch)->iso8601 . 'Z' : " : ""),
27             ($format_timemoment ?
28             "ref($dt) eq 'Time::Moment' ? $dt\->at_utc->strftime('%Y-%m-%dT%H:%M:%SZ') : " : ""),
29             $dt,
30             );
31              
32 5         29 $res;
33             }
34              
35             1;
36             # ABSTRACT: Format date as ISO8601 datetime (e.g. 2016-06-13T03:08:00Z)
37              
38             __END__
39              
40             =pod
41              
42             =encoding UTF-8
43              
44             =head1 NAME
45              
46             Data::Sah::Format::perl::iso8601_datetime - Format date as ISO8601 datetime (e.g. 2016-06-13T03:08:00Z)
47              
48             =head1 VERSION
49              
50             This document describes version 0.003 of Data::Sah::Format::perl::iso8601_datetime (from Perl distribution Data-Sah-Format), released on 2017-07-10.
51              
52             =head1 DESCRIPTION
53              
54             Will format epoch as ISO8601 datetime. By default will also format L<DateTime>
55             and L<Time::Moment> instances, but this can be turned off. Will leave other kind
56             of data unformatted.
57              
58             =for Pod::Coverage ^(format)$
59              
60             =head1 FORMATTER ARGUMENTS
61              
62             =head2 format_datetime => bool (default: 1)
63              
64             =head2 format_timemoment => bool (default: 1)
65              
66             =head1 HOMEPAGE
67              
68             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-Format>.
69              
70             =head1 SOURCE
71              
72             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-Format>.
73              
74             =head1 BUGS
75              
76             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-Format>
77              
78             When submitting a bug or request, please include a test-file or a
79             patch to an existing test-file that illustrates the bug or desired
80             feature.
81              
82             =head1 AUTHOR
83              
84             perlancar <perlancar@cpan.org>
85              
86             =head1 COPYRIGHT AND LICENSE
87              
88             This software is copyright (c) 2017, 2016 by perlancar@cpan.org.
89              
90             This is free software; you can redistribute it and/or modify it under
91             the same terms as the Perl 5 programming language system itself.
92              
93             =cut