Commit 46098df7 authored by Dave Morriss's avatar Dave Morriss

Adjusted parse_8601_duration

Allowed a sign before the 'P' in duration expressions.
Added a signed test to the test data.
parent da96b30e
......@@ -9,3 +9,4 @@ P0.5YT0.5M
P0Y1347M
P0YT
P0Y2H
-P23DT23H
......@@ -23,7 +23,7 @@
# AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
# VERSION: 1.0
# CREATED: 04/07/2013 17:20:09
# REVISION: 06/07/2013 10:41:27
# REVISION: 06/07/2013 12:51:47
#
#===============================================================================
......@@ -33,6 +33,7 @@ use warnings;
my $duration;
my $sign;
my @labels = qw{Years Months Days Hours Minutes Seconds};
my @fields;
......@@ -51,7 +52,8 @@ my $frac = qr{[0-9]+(?:\.[0-9]+)?};
# need their designators.
#
my $re = qr{(?x) # Enable embedded whitespace and comments
^P # Assume the string begins with P
^([+-]?) # Assume the string begins with the optional sign
P # The 'P' is mandatory
(?:($int)Y)? # Years
(?:($int)M)? # Months
(?:($int)D)? # Days
......@@ -71,7 +73,12 @@ while ( $duration = shift ) {
#
# Perform the comparison
#
if ( @fields = ( $duration =~ $re ) ) {
if ( ( $sign, @fields ) = ( $duration =~ $re ) ) {
#
# Optional sign defaults to '+'
#
$sign = '+' unless $sign;
#
# Force all undefined fields to be zero
#
......@@ -80,11 +87,13 @@ while ( $duration = shift ) {
#
# Build a duration hash (just to show how to do it in a cool Perly way)
#
$iso_duration{sign} = $sign;
@iso_duration{@labels} = @fields;
#
# Display the fields in the right order
# Display the sign and the fields in the right order
#
printf "%-7s: %6s\n","Sign",$sign;
for ( my $i = 0; $i <= $#fields; $i++ ) {
printf "%-7s: %6.2f\n", $labels[$i], $fields[$i];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment