Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
sudo: required
dist: trusty
language: cpp
compiler:
- gcc
before_install:
# for gcc with C++11 support
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
- sudo apt-get -qq update
install:
# install GTest and GMock
- sudo apt-get -qq install libgtest-dev
- "cd /usr/src/gtest && sudo cmake . && sudo cmake --build . && sudo mv libg* /usr/local/lib/ ; cd -"
- sudo apt-get -qq install google-mock
- "cd /usr/src/gmock && sudo cmake . && sudo cmake --build . && sudo mv libg* /usr/local/lib/ ; cd -"
# update to gcc with C++11 support
- sudo apt-get -qq install gcc-4.9 g++-4.9
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 90
- sudo update-alternatives --install /usr/bin/gcov gcov /usr/bin/gcov-4.9 90
- sudo apt-get -qq install libtest-simple-perl
- sudo apt-get -qq install libtest-harness-perl
- sudo apt-get -qq install libdatetime-perl
# install latest LCOV (1.9 was failing)
- cd ${TRAVIS_BUILD_DIR}
- wget http://ftp.de.debian.org/debian/pool/main/l/lcov/lcov_1.11.orig.tar.gz
- tar xf lcov_1.11.orig.tar.gz
- sudo make -C lcov-1.11/ install
- gem install coveralls-lcov
- lcov --version
- g++ --version
before_script:
- cd ${TRAVIS_BUILD_DIR}
# init coverage to 0 (optional)
- lcov --directory . --zerocounters
script:
- cd ${TRAVIS_BUILD_DIR}
# Check that this works this way
- prove -l
28 changes: 24 additions & 4 deletions lib/DateTime/Format/Duration.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ my %formats =
'e' => sub { sprintf( '%d', $_[0]->{days} ) },
'F' => sub { sprintf( '%04d-%02d-%02d', $_[0]->{years}, $_[0]->{months}, $_[0]->{days} ) },
'H' => sub { sprintf( '%02d', $_[0]->{hours} ) },
'-H'=> sub { sprintf( '%d', $_[0]->{hours} ) },
'I' => sub { sprintf( '%02d', $_[0]->{hours} ) },
'-I'=> sub { sprintf( '%d', $_[0]->{hours} ) },
'j' => sub { $_[1]->as_days($_[0]) },
'k' => sub { sprintf( '%2d', $_[0]->{hours} ) },
'-k'=> sub { sprintf( '%d', $_[0]->{hours} ) },
'l' => sub { sprintf( '%2d', $_[0]->{hours} ) },
'-l'=> sub { sprintf( '%d', $_[0]->{hours} ) },
'm' => sub { sprintf( '%02d', $_[0]->{months} ) },
'-m'=> sub { sprintf( '%d', $_[0]->{months} ) },
'M' => sub { sprintf( '%02d', $_[0]->{minutes} ) },
'-M'=> sub { sprintf( '%d', $_[0]->{minutes} ) },
'n' => sub { "\n" }, # should this be OS-sensitive?"
'N' => sub { _format_nanosecs(@_) },
'p' => sub { ($_[0]->{negative}) ? '-' : '+' },
Expand Down Expand Up @@ -176,6 +182,17 @@ sub format_duration_from_deltas {

/sgex;

# H or I = 2 digit hours, k or l = space padded hours, m or M = minutes
$f =~ s/
%-(\d*)([HIklmM])
/
$formats{"-$2"}
? ($1)
? sprintf("%0$1d", substr($formats{"-$2"}->(\%duration, $self),$1*-1) )
: $formats{"-$2"}->(\%duration, $self)
: $1
/sgex;

# %3N
$f =~ s/
%(\d*)N
Expand Down Expand Up @@ -508,11 +525,14 @@ sub _format_nanosecs {
sub _build_parser {
my $self = shift;
my $regex = my $field_list = shift || $self->pattern;
my @fields = $field_list =~ m/(%\{\w+\}|%\d*.)/g;
my @fields = $field_list =~ m/(%\{\w+\}|%\-*\d*.)/g;
$field_list = join('',@fields);

my $tempdur = DateTime::Duration->new( seconds => 0 ); # Created just so we can do $tempdt->can(..)

# Don't need the padding remover for parsing
$regex =~ s/%\-([HIklmM])/%$1/g;

# I'm absoutely certain there's a better way to do this:
$regex=~s|([\/\.\-])|\\$1|g;

Expand Down Expand Up @@ -545,7 +565,7 @@ sub _build_parser {

# Months:
$regex =~ s/%(\d*)[m]/($1) ? " *([+-]?\\d{$1})" : " *([+-]?\\d+)"/eg;
$field_list =~ s/%(\d*)[m]/#months#/g;
$field_list =~ s/%(-*\d*)[m]/#months#/g;

# Weeks:
$regex =~ s/%(\d*)[GV]/($1) ? " *([+-]?\\d{$1})" : " *([+-]?\\d+)"/eg;
Expand All @@ -559,11 +579,11 @@ sub _build_parser {

# Hours:
$regex =~ s/%(\d*)[HIkl]/($1) ? " *([+-]?\\d{$1})" : " *([+-]?\\d+)"/eg;
$field_list =~ s/%(\d*)[HIkl]/#hours#/g;
$field_list =~ s/%(-*\d*)[HIkl]/#hours#/g;

# Minutes:
$regex =~ s/%(\d*)[M]/($1) ? " *([+-]?\\d{$1})" : " *([+-]?\\d+)"/eg;
$field_list =~ s/%(\d*)[M]/#minutes#/g;
$field_list =~ s/%(-*\d*)[M]/#minutes#/g;

# Seconds:
$regex =~ s/%(\d*)[sS]/($1) ? " *([+-]?\\d{$1})" : " *([+-]?\\d+)"/eg;
Expand Down
44 changes: 43 additions & 1 deletion t/7_misc.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use Test::More tests => 7;
use Test::More tests => 20;

use warnings;
use DateTime;
Expand Down Expand Up @@ -45,3 +45,45 @@ is( $strf->format_duration( $duration ), (22*24*60*60) + (36*60*60), '22 days, 3
$strf->set_pattern('%u');
is( $strf->format_duration( $duration ), '2', '22 days, 36 hours as days modulus 7' );


$duration = DateTime::Duration->new( months => 2, days => 2, hours => 30, minutes => 3 );
$strf->set_normalising(1);
$strf->set_pattern('%H');
is( $strf->format_duration( $duration ), '06','format %H 30 - 24 = 06' );

$strf->set_pattern('%-H');
is( $strf->format_duration( $duration ), '6','format %-H 30 - 24 = 6' );

$strf->set_pattern('%I');
is( $strf->format_duration( $duration ), '06','format %I 30 - 24 = 06' );

$strf->set_pattern('%-I');
is( $strf->format_duration( $duration ), '6','format %-I 30 - 24 = 6' );

$strf->set_pattern('%k');
is( $strf->format_duration( $duration ), ' 6','format %k 30 - 24 = 6' );

$strf->set_pattern('%-k');
is( $strf->format_duration( $duration ), '6','format %-k 30 - 24 = 6' );

$strf->set_pattern('%l');
is( $strf->format_duration( $duration ), ' 6','format %l 30 - 24 = 6' );

$strf->set_pattern('%-l');
is( $strf->format_duration( $duration ), '6','format %-l 30 - 24 = 6' );

$strf->set_pattern('%e');
is( $strf->format_duration( $duration ), '3','format %e 3 days' );

$strf->set_pattern('%m');
is( $strf->format_duration( $duration ), '02','format %m 02 months' );

$strf->set_pattern('%-m');
is( $strf->format_duration( $duration ), '2','format %-m 2 months' );

$strf->set_pattern('%M');
is( $strf->format_duration( $duration ), '03','format %M 03 minutes' );

$strf->set_pattern('%-M');
is( $strf->format_duration( $duration ), '3','format %-M 3 minutes' );

54 changes: 54 additions & 0 deletions t/9_doc_examples.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use Test::More tests => 9;

use warnings;
use DateTime;
use DateTime::Duration;
use DateTime::Format::Duration;
use Data::Dumper;

my $d = DateTime::Format::Duration->new(
pattern => '%Y years, %-m months, %e days, '.
'%-H hours, %M minutes, %S seconds',
normalise => 1
);

is($d->format_duration(
DateTime::Duration->new(
years => 3,
months => 5,
days => 1,
hours => 6,
minutes => 15,
seconds => 45,
nanoseconds => 12000
)
),'3 years, 5 months, 1 days, 6 hours, 15 minutes, 45 seconds','First full format');

# Returns DateTime::Duration object
my $duration = $d->parse_duration(
'3 years, 5 months, 1 days, 6 hours, 15 minutes, 45 seconds'
);

is($d->format_duration_from_deltas(
years => 3,
months => 5,
days => 1,
hours => 6,
minutes => 15,
seconds => 45,
nanoseconds => 12000
),'3 years, 5 months, 1 days, 6 hours, 15 minutes, 45 seconds',
'From deltas');

my %deltas = $d->parse_duration_as_deltas(
'3 years, 5 months, 1 days, 6 hours, 15 minutes, 45 seconds'
);

is($deltas{years},3,'3 years');
is($deltas{months},5,'5 months');
is($deltas{days},1,'1 days');
is($deltas{hours},6,'6 hours');
is($deltas{minutes},15,'15 minutes');
is($deltas{seconds},45,'45 seconds');
is($deltas{nanoseconds},0,'0 nanoseconds');