| 1 | package TOML::Tiny::Writer; | ||||||||||||||||||||||||||||
| 2 | 286 | $TOML::Tiny::Writer::VERSION = '0.20'; | |||||||||||||||||||||||||||
| 3 | 286 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 286 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 286 | ▶ | no warnings qw(experimental); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 286 | ▶ | use v5.18; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 286 | ▶ | use B qw(SVf_IOK SVf_NOK svref_2object); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 286 | ▶ | use Data::Dumper qw(Dumper); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 286 | ▶ | use TOML::Tiny::Grammar qw($BareKey $DateTime $SpecialFloat); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 286 | ▶ | use TOML::Tiny::Util qw(is_strict_array); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 286 | ▶ | use constant CORE_BOOL => defined &builtin::is_bool; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 286 | my @KEYS; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub to_toml { | ||||||||||||||||||||||||||||
| 18 | 111 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
to_toml: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 111 | ▶ | die 'toml: data to encode must be a hashref' if ref $data ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 109 | return _to_toml( $data, { @_ } ); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub _to_toml ($$); | ||||||||||||||||||||||||||||
| 24 | sub _to_toml ($$) { | ||||||||||||||||||||||||||||
| 25 | 809 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
_to_toml: called CC=26 SCAR=35.9
| |||||||||||||||||||||||||||||
| 26 | 809 | my $param = shift; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 809 | ▶ | die 'toml: found undefined value, which is unsupported by TOML' if ! defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 808 | my $ref = ref $data; | |||||||||||||||||||||||||||
| 31 | 808 | ▶ | if ($ref eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 291 | return to_toml_table($data, $param); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 517 | ▶ | if ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 67 | return to_toml_array($data, $param); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 450 | ▶ | if ($ref eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($$data eq '1') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 41 | 0 | return 'true'; | |||||||||||||||||||||||||||
| 42 | } elsif ($$data eq '0') { | ||||||||||||||||||||||||||||
| 43 | 0 | return 'false'; | |||||||||||||||||||||||||||
| 44 | } else { | ||||||||||||||||||||||||||||
| 45 | 0 | return _to_toml($$_, $param); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 450 | ▶ | if ($ref eq 'JSON::PP::Boolean') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 17 | ▶ | return $$data ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 433 | ▶ | if ($ref eq 'Types::Serializer::Boolean') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | return $data ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 433 | ▶ | if ($ref eq 'DateTime') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 2 | return strftime_rfc3339($data); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 431 | ▶ | if ($ref eq 'Math::BigInt') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 2 | return $data->bstr; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 429 | ▶ | if ($ref eq 'Math::BigFloat') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 6 | ▶ | if ($data->is_inf || $data->is_nan) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$data->is_inf or $data->is_nan
MC/DC0%0/2
$data->is_inf or $data->is_nan
$data->is_inf
$data->is_nan
Truth table33%1/3
A or B
A= $data->is_infB= $data->is_nan
| |||||||||||||||||||||||||||||
| 67 | 0 | return lc $data->bstr; | |||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||
| 69 | 6 | return $data->bstr; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 423 | ▶ | if ($ref eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 423 | ▶ | if (CORE_BOOL && builtin::is_bool($data)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | return $data ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | # Thanks to ikegami on Stack Overflow for the trick! | ||||||||||||||||||||||||||||
| 78 | # https://stackoverflow.com/questions/12686335/how-to-tell-apart-numeric-scalars-and-string-scalars-in-perl/12693984#12693984 | ||||||||||||||||||||||||||||
| 79 | # note: this must come before any regex can flip this flag off | ||||||||||||||||||||||||||||
| 80 | 421 | ▶ | if (svref_2object(\$data)->FLAGS & (SVf_IOK | SVf_NOK)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 186 | ▶ | return 'inf' if Math::BigFloat->new($data)->is_inf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 186 | ▶ | return '-inf' if Math::BigFloat->new($data)->is_inf('-'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 186 | ▶ | return 'nan' if Math::BigFloat->new($data)->is_nan; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 186 | return $data; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 235 | ▶ | return to_toml_string($data) if $param->{no_string_guessing}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | #return $data if svref_2object(\$data)->FLAGS & (SVf_IOK | SVf_NOK); | ||||||||||||||||||||||||||||
| 88 | 227 | ▶ | return $data if $data =~ /^$DateTime$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 203 | ▶ | return lc($data) if $data =~ /^$SpecialFloat$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 194 | return to_toml_string($data); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | die 'unhandled: '.Dumper($ref); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub to_toml_inline_table { | ||||||||||||||||||||||||||||
| 98 | 6 | ▶ | my ($data, $param) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_toml_inline_table: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 6 | my @buff; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 6 | for my $k (keys %$data) { | |||||||||||||||||||||||||||
| 102 | 7 | my $key = to_toml_key($k); | |||||||||||||||||||||||||||
| 103 | 7 | my $val = $data->{$k}; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 7 | ▶ | if (ref $val eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | push @buff, $key . '=' . to_toml_inline_table($val); | |||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||
| 108 | 7 | push @buff, $key . '=' . _to_toml($val, $param); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 6 | return '{' . join(', ', @buff) . '}'; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub to_toml_table { | ||||||||||||||||||||||||||||
| 116 | 291 | ▶ | my ($data, $param) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_toml_table: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | 291 | my @buff_assign; | |||||||||||||||||||||||||||
| 118 | my @buff_tables; | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Generate simple key/value pairs for scalar data | ||||||||||||||||||||||||||||
| 121 | 291 | for my $k (grep{ ref($data->{$_}) !~ /HASH|ARRAY/ } sort keys %$data) { | |||||||||||||||||||||||||||
| 122 | 348 | my $key = to_toml_key($k); | |||||||||||||||||||||||||||
| 123 | 348 | my $val = _to_toml($data->{$k}, $param); | |||||||||||||||||||||||||||
| 124 | 348 | push @buff_assign, "$key=$val"; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # For arrays, generate an array of tables if all elements of the array are | ||||||||||||||||||||||||||||
| 128 | # hashes. For mixed arrays, generate an inline array. | ||||||||||||||||||||||||||||
| 129 | 291 | ARRAY: for my $k (grep{ ref $data->{$_} eq 'ARRAY' } sort keys %$data) { | |||||||||||||||||||||||||||
| 130 | # Empty table | ||||||||||||||||||||||||||||
| 131 | 59 | ▶ | if (!@{$data->{$k}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | my $key = to_toml_key($k); | |||||||||||||||||||||||||||
| 133 | 0 | push @buff_assign, "$key=[]"; | |||||||||||||||||||||||||||
| 134 | 0 | next ARRAY; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Mixed array | ||||||||||||||||||||||||||||
| 138 | 59 | ▶ | if (grep{ ref $_ ne 'HASH' } @{$data->{$k}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 39 | my $key = to_toml_key($k); | |||||||||||||||||||||||||||
| 140 | 39 | my $val = _to_toml($data->{$k}, $param); | |||||||||||||||||||||||||||
| 141 | 38 | push @buff_assign, "$key=$val"; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | # Array of tables | ||||||||||||||||||||||||||||
| 144 | else { | ||||||||||||||||||||||||||||
| 145 | 20 | push @KEYS, $k; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 20 | for (@{ $data->{$k} }) { | |||||||||||||||||||||||||||
| 148 | 37 | push @buff_tables, '', '[[' . join('.', map{ to_toml_key($_) } @KEYS) . ']]'; | |||||||||||||||||||||||||||
| 149 | 37 | push @buff_tables, _to_toml($_, $param); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 20 | pop @KEYS; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Sub-tables | ||||||||||||||||||||||||||||
| 157 | 290 | for my $k (grep{ ref $data->{$_} eq 'HASH' } sort keys %$data) { | |||||||||||||||||||||||||||
| 158 | 169 | ▶ | if (!keys(%{$data->{$k}})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | # Empty table | ||||||||||||||||||||||||||||
| 160 | 24 | my $key = to_toml_key($k); | |||||||||||||||||||||||||||
| 161 | 24 | push @buff_assign, "$key={}"; | |||||||||||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||||||||||
| 163 | # Generate [table] | ||||||||||||||||||||||||||||
| 164 | 145 | push @KEYS, $k; | |||||||||||||||||||||||||||
| 165 | 145 | push @buff_tables, '', '[' . join('.', map{ to_toml_key($_) } @KEYS) . ']'; | |||||||||||||||||||||||||||
| 166 | 145 | push @buff_tables, _to_toml($data->{$k}, $param); | |||||||||||||||||||||||||||
| 167 | 143 | pop @KEYS; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 288 | join "\n", @buff_assign, @buff_tables; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub to_toml_array { | ||||||||||||||||||||||||||||
| 175 | 68 | ▶ | my ($data, $param) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_toml_array: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 68 | ▶ | if (@$data && $param->{strict}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$data and $param->{"strict"}
MC/DC100%2/2
@$data and $param->{"strict"}
@$data
$param->{"strict"}
Truth table100%3/3
A and B
A= @$dataB= $param->{"strict"}
| |||||||||||||||||||||||||||||
| 178 | 2 | my ($ok, $err) = is_strict_array($data); | |||||||||||||||||||||||||||
| 179 | 2 | ▶ | die "toml: found heterogenous array, but strict is set ($err)\n" unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 68 | my @items; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 68 | for my $item (@$data) { | |||||||||||||||||||||||||||
| 185 | 130 | ▶ | if (ref $item eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 6 | push @items, to_toml_inline_table($item, $param); | |||||||||||||||||||||||||||
| 187 | } else { | ||||||||||||||||||||||||||||
| 188 | 124 | push @items, _to_toml($item, $param); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 67 | return "[\n" . join("\n", map{ " $_," } @items) . "\n]"; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | sub to_toml_key { | ||||||||||||||||||||||||||||
| 196 | 752 | ▶ | my $str = shift; | ||||||||||||||||||||||||||
|
Subroutine
to_toml_key: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 752 | ▶ | if ($str =~ /^$BareKey$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 718 | return $str; | |||||||||||||||||||||||||||
| 200 | } else { | ||||||||||||||||||||||||||||
| 201 | # Not valid as a "bare key". Encode it as a "quoted key" | ||||||||||||||||||||||||||||
| 202 | # (in TOML terminology), using the "literal string" format. | ||||||||||||||||||||||||||||
| 203 | 34 | return to_toml_string($str); | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | sub to_toml_string { | ||||||||||||||||||||||||||||
| 208 | 236 | ▶ | state $escape = { | ||||||||||||||||||||||||||
|
Subroutine
to_toml_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 209 | "\n" => '\n', | ||||||||||||||||||||||||||||
| 210 | "\r" => '\r', | ||||||||||||||||||||||||||||
| 211 | "\t" => '\t', | ||||||||||||||||||||||||||||
| 212 | "\f" => '\f', | ||||||||||||||||||||||||||||
| 213 | "\b" => '\b', | ||||||||||||||||||||||||||||
| 214 | "\"" => '\"', | ||||||||||||||||||||||||||||
| 215 | "\\" => '\\\\', | ||||||||||||||||||||||||||||
| 216 | "\'" => '\\\'', | ||||||||||||||||||||||||||||
| 217 | }; | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 236 | my ($arg) = @_; | |||||||||||||||||||||||||||
| 220 | 236 | $arg =~ s/(["\\\b\f\n\r\t])/$escape->{$1}/g; | |||||||||||||||||||||||||||
| 221 | 236 | $arg =~ s/([\p{General_Category=Control}])/'\\u00' . unpack('H2', $1)/eg; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 236 | return '"' . $arg . '"'; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 227 | # Adapted from DateTime::Format::RFC3339. | ||||||||||||||||||||||||||||
| 228 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 229 | sub strftime_rfc3339 { | ||||||||||||||||||||||||||||
| 230 | 8 | ▶ | my ($dt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
strftime_rfc3339: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 231 | 8 | my $tz; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 234 | # Calculate the time zone offset for non-UTC time zones. | ||||||||||||||||||||||||||||
| 235 | # | ||||||||||||||||||||||||||||
| 236 | # TOML uses RFC3339 for datetimes, but supports a "local datetime" which | ||||||||||||||||||||||||||||
| 237 | # excludes the timezone offset. A DateTime with a floating time zone | ||||||||||||||||||||||||||||
| 238 | # indicates a TOML local datetime. | ||||||||||||||||||||||||||||
| 239 | # | ||||||||||||||||||||||||||||
| 240 | # DateTime::Format::RFC3339 requires a time zone, however, and defaults to | ||||||||||||||||||||||||||||
| 241 | # +00:00 for floating time zones. To support local datetimes in output, | ||||||||||||||||||||||||||||
| 242 | # format the datetime as RFC3339 and strip the timezone when encountering a | ||||||||||||||||||||||||||||
| 243 | # floating time zone. | ||||||||||||||||||||||||||||
| 244 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 245 | 8 | ▶ | if ($dt->time_zone_short_name eq 'floating') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 246 | 2 | $tz = ''; | |||||||||||||||||||||||||||
| 247 | } elsif ($dt->time_zone->is_utc) { | ||||||||||||||||||||||||||||
| 248 | 1 | $tz = 'Z'; | |||||||||||||||||||||||||||
| 249 | } else { | ||||||||||||||||||||||||||||
| 250 | 5 | ▶ | my $sign = $dt->offset < 0 ? '-' : '+'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 5 | my $secs = abs $dt->offset; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 5 | my $mins = int($secs / 60); | |||||||||||||||||||||||||||
| 254 | 5 | $secs %= 60; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 5 | my $hours = int($mins / 60); | |||||||||||||||||||||||||||
| 257 | 5 | $mins %= 60; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 5 | ▶ | if ($secs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 1 | $dt = $dt->clone; | |||||||||||||||||||||||||||
| 261 | 1 | $dt->set_time_zone('UTC'); | |||||||||||||||||||||||||||
| 262 | 1 | $tz = 'Z'; | |||||||||||||||||||||||||||
| 263 | } else { | ||||||||||||||||||||||||||||
| 264 | 4 | $tz = sprintf '%s%02d:%02d', $sign, $hours, $mins; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 8 | ▶ | my $format = $dt->nanosecond ? '%Y-%m-%dT%H:%M:%S.%9N' : '%Y-%m-%dT%H:%M:%S'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 8 | return $dt->strftime($format) . $tz; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 286 | 1; | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | __END__ | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =pod | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =head1 NAME | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | TOML::Tiny::Writer | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =head1 VERSION | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | version 0.20 | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | Jeff Ober <sysread@fastmail.fm> | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | This software is copyright (c) 2025 by Jeff Ober. | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 297 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =cut | ||||||||||||||||||||||||||||