LMPX.COM |
Home | Linux | Mysql | PHP | XML | ||
|
|
|||
From: Daisuke Maki Date: Wed Oct 1 10:32:57 2008 Subject: Re: I'd tell you how long it'll take for my hair to fall out but
I'm struggling with duration!
this seems to do what you wanted?
my $dt1 = DateTime->new(
year => 2008,
month => 10,
day => 2,
);
my $dt2 = DateTime->new(
year => 2008,
month => 10,
day => 5,
);
my $diff = $dt2 - $dt1;
print $diff->delta_days, "\n";
--d
Kristian Flint wrote:
> Hi everybody!
>
> You may remember me from a while back wanting to make a module
> pertaining to UK bank holidays. I got a bit stuck on some other work and
> have finally had some time to come back to it. I'm pleased to say that
> it's going very well but I've hit a small snag, which I could fix with a
> hack but part of doing all this is wanting to contribute back to the
> community so I'd rather try and work out why something isn't working...
>
> So here's the problem, I've got two DateTime objects and I want to work
> out how many days there are between them. Simple I thought:
>
> my $dt1 = DateTime->new(
> year => 2008,
> month => 10,
> day => 2,
> );
>
> my $dt2 = DateTime->new(
> year => 2008,
> month => 10,
> day => 5,
> );
>
> my $span = DateTime::Span->from_datetimes( start => $dt1, end => $dt2 );
> my $d = $span->duration;
> print $d->days;
>
> That didn't work so I went back to the documentation and spotted that
> "The last example demonstrates that there will not be any conversion
> between units which don't have a fixed conversion rate."
> Which if someone could clarify would be great because the
> $span->duration seems to return a duration object in seconds, there's
> definitely a fixed number of seconds in a day (actually unless we're
> talking about that tiny fraction that ends up counting towards a quarter
> of a day per year?).
>
> Anyways, I thought fair enough, I read on and got to:
> "If you need more flexibility in presenting information about durations,
> please take a look a DateTime::Format::Duration."
> Great, nearly there.
> Except this doesn't seem to be working : (
>
>
> Can somebody just check I'm not mental:
>
> FROM DOCS:
> http://search.cpan.org/~rickm/DateTime-Format-Duration-1.03a/lib/DateTim
> e/Format/Duration.pm
>
> $d = DateTime::Format::Duration->new(
> pattern => '%Y years, %m months, %e days, '.
> '%H hours, %M minutes, %S seconds'
> );
>
> print $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
> END FROM DOCS
>
> This doesn't actually return what's stated above (#) but actually
> returns
> 0 years, 41 months, 1 days, 00 hours, 375 minutes, 45 seconds
> And if I try and make the pattern => '%e' to just get the number of
> days, this returns 0!
>
>
> Have I missed something?! I'm thinking there's probably a _much_ easier
> way of getting what I want and it's just passed by my small boy brain.
>
> Appreciative of any help,
>
> Kristian
>
>
| Navigate in group perl.datetime at sever nntp.perl.org | |
| Previous | Next |
| © No Copyright You are free to use Anything |
Site Maintained by PHP Developer
Powered By PHP Consultants |