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