Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed DateTime string formatter for subseconds
  • Loading branch information
Andy Weidenbaum committed Oct 6, 2015
1 parent 5a09711 commit 37894ab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
42 changes: 39 additions & 3 deletions lib/Config/TOML/Parser/Actions.pm
Expand Up @@ -276,11 +276,19 @@ method time_offset($/)
method partial_time($/)
{
my Rat $second = Rat($<time_second>.made);
$second += Rat($<time_secfrac>.made) if $<time_secfrac>;
my Bool $subseconds = False;

if $<time_secfrac>
{
$second += Rat($<time_secfrac>.made);
$subseconds = True;
}

make %(
:hour(Int($<time_hour>.made)),
:minute(Int($<time_minute>.made)),
:$second
:$second,
:$subseconds
);
}

Expand All @@ -299,20 +307,48 @@ method full_time($/)
:hour(Int($<partial_time>.made<hour>)),
:minute(Int($<partial_time>.made<minute>)),
:second(Rat($<partial_time>.made<second>)),
:subseconds(Bool($<partial_time>.made<subseconds>)),
:timezone(Int($<time_offset>.made))
);
}

method date_time($/)
{
my %fmt;
%fmt<formatter> =
{
# adapted from rakudo/src/core/Temporal.pm
# needed in place of passing a True :$subseconds arg to
# the rakudo DateTime default-formatter subroutine
# for DateTimes with defined time_secfrac
my $o = .offset;
$o %% 60
or warn "DateTime subseconds formatter: offset $o not
divisible by 60.";
my $year = sprintf(
(0 <= .year <= 9999 ?? '%04d' !! '%+05d'),
.year
);
sprintf '%s-%02d-%02dT%02d:%02d:%s%s',
$year, .month, .day, .hour, .minute,
.second.fmt('%09.6f'),
do $o
?? sprintf '%s%02d:%02d',
$o < 0 ?? '-' !! '+',
($o.abs / 60 / 60).floor,
($o.abs / 60 % 60).floor
!! 'Z';
} if $<full_time>.made<subseconds>;

make DateTime.new(
:year(Int($<full_date>.made<year>)),
:month(Int($<full_date>.made<month>)),
:day(Int($<full_date>.made<day>)),
:hour(Int($<full_time>.made<hour>)),
:minute(Int($<full_time>.made<minute>)),
:second(Rat($<full_time>.made<second>)),
:timezone(Int($<full_time>.made<timezone>))
:timezone(Int($<full_time>.made<timezone>)),
|%fmt
);
}

Expand Down
12 changes: 1 addition & 11 deletions t/grammar-actions/01-primitives.t
Expand Up @@ -1403,17 +1403,7 @@ subtest
);
is(
$match_date_time3.made,
# is there a built-in way to stringify a DateTime with fractional
# seconds displayed by default?
DateTime.new(
:year(1979),
:month(5),
:day(27),
:hour(0),
:minute(32),
:second(0.999999),
:timezone(-25200)
),
'1979-05-27T00:32:00.999999-07:00',
q:to/EOF/
♪ [Is expected date_time value?] - 84 of 84
┏━━━━━━━━━━━━━┓
Expand Down

0 comments on commit 37894ab

Please sign in to comment.