File Coverage

blib/lib/Date/Format/ISO8601.pm
Criterion Covered Total %
statement 42 47 89.3
branch 22 26 84.6
condition 10 12 83.3
subroutine 7 10 70.0
pod 6 6 100.0
total 87 101 86.1


line stmt bran cond sub pod time code
1             package Date::Format::ISO8601;
2              
3 1     1   49756 use strict;
  1         9  
  1         23  
4 1     1   4 use warnings;
  1         1  
  1         21  
5              
6 1     1   4 use Exporter qw(import);
  1         1  
  1         434  
7              
8             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9             our $DATE = '2022-07-23'; # DATE
10             our $DIST = 'Date-Format-ISO8601'; # DIST
11             our $VERSION = '0.012'; # VERSION
12              
13             our @EXPORT_OK = qw(
14             gmtime_to_iso8601_date
15             gmtime_to_iso8601_time
16             gmtime_to_iso8601_datetime
17              
18             localtime_to_iso8601_date
19             localtime_to_iso8601_time
20             localtime_to_iso8601_datetime
21             );
22              
23             sub _format {
24 8     8   11 my $local_or_gm = shift;
25 8         7 my $which = shift;
26 8 100       18 my $opts = ref $_[0] eq 'HASH' ? shift : {};
27 8         9 my $timestamp = shift;
28              
29 8 50       8 my $tz = $opts->{tz}; $tz = ($local_or_gm eq 'gm' ? 'Z':'') unless defined $tz;
  8 100       21  
30              
31 8 50       46 my ($sec, $min, $hour, $day, $mon, $year) =
32             $local_or_gm eq 'local' ? localtime($timestamp) : gmtime($timestamp);
33 8         16 $year+=1900; $mon++;
  8         7  
34 8         12 my $sec_frac = $timestamp - int($timestamp);
35              
36 8         8 my $s_date = '';
37 8         8 my $s_time = '';
38              
39 8 100 100     22 if ($which eq 'date' || $which eq 'datetime') {
40 3 100       4 my $date_sep = $opts->{date_sep}; $date_sep = '-' unless defined $date_sep;
  3         4  
41 3         13 $s_date = sprintf "%04d%s%02d%s%02d", $year, $date_sep, $mon, $date_sep, $day;
42             }
43              
44 8 100 100     21 if ($which eq 'time' || $which eq 'datetime') {
45 6 100       9 my $time_sep = $opts->{time_sep}; $time_sep = ':' unless defined $time_sep;
  6         9  
46 6         19 $s_time = sprintf "%02d%s%02d%s%02d", $hour, $time_sep, $min, $time_sep, $sec;
47 6 100 66     20 if ($sec_frac &&
      66        
48             !defined($opts->{second_precision}) ||
49             $opts->{second_precision}) {
50 2         3 my $s_secfrac;
51 2 50       4 if (!defined($opts->{second_precision})) {
52 0         0 $s_secfrac = sprintf("%s", $sec_frac);
53             } else {
54 2         12 $s_secfrac = sprintf("%.$opts->{second_precision}f",
55             $sec_frac);
56             }
57 2         5 $s_time .= substr($s_secfrac, 1); # remove the "0" part
58             }
59 6         10 $s_time .= $tz;
60             }
61              
62 8 100       14 if ($which eq 'date') {
    100          
    50          
63 2         7 return $s_date;
64             } elsif ($which eq 'time') {
65 5         18 return $s_time;
66             } elsif ($which eq 'datetime') {
67 1         5 return $s_date . 'T' . $s_time;
68             } else {
69 0         0 die "BUG: Unknown which '$which'"; # shouldn't happen
70             }
71             }
72              
73 2     2 1 1379 sub gmtime_to_iso8601_date { _format('gm' , 'date' , @_) }
74 5     5 1 1978 sub gmtime_to_iso8601_time { _format('gm' , 'time' , @_) }
75 1     1 1 1836 sub gmtime_to_iso8601_datetime { _format('gm' , 'datetime', @_) }
76 0     0 1   sub localtime_to_iso8601_date { _format('local', 'date' , @_) }
77 0     0 1   sub localtime_to_iso8601_time { _format('local', 'time' , @_) }
78 0     0 1   sub localtime_to_iso8601_datetime { _format('local', 'datetime', @_) }
79              
80             1;
81             # ABSTRACT: Format date (Unix timestamp) as ISO8601 datetime/date/time string
82              
83             __END__