On Fri, Nov 27, 2009 at 5:43 AM, raphael() <raphael.j...@gmail.com> wrote:

> Hi,
>
> I have to code a script to calc time elapsed in days as to calc the rent to
> be charged.
> The person would write an object number and date in a text file like
>
> db.txt
> OBJECT_NUM, DATE_GIVEN
>
> 2525,25.11.2008
> 2526,01.01.2009
> 2527,26.11.2009
>
> Now when he enter "OBJECT_NUM" I have to tell him the numbers of days
> elapsed since "DATE_GIVEN"
> and the rent he should charge which increases every 15 days like
>
> first 15 days = 2
> next 15 days = 3
> next 15 days = 5
> next 15 days = 8
> next 15 days = 10
> now its always 10
>
> So if the OBJECT is returned in say 40 days the rent charged is 10 (2 + 3 +
> 5)
>
> Being a beginner, I thought of hard coding the values in the code.
>
>    if ( $days_elapsed <= 15 ) {
>        $total_cost = 2;
>    } elsif ( $days_elapsed <= 30 ) {
>        $total_cost = 5;
>    } elsif ( $days_elapsed <= 45 ) {
>        $total_cost = 10;
>    } elsif ( $days_elapsed <= 60 ) {
>        $total_cost = 18;
>    } elsif ( $days_elapsed <= 75 ) {
>        $total_cost = 28;
>    } elsif ( $days_elapsed <= 90 ) {
>        $total_cost = 38;
>    } else {
>        print "More than 90 days! SPECIAL RATE\n";
>        exit;
>    }
>
>
> HOW CAN I COUNT ELAPSED DAYS ?
>
> I tried in localtime() days value like
>
> my @array_date = localtime();
> my $current_dayofyear = @array_date[7];
>
>
> But this would break on New year as "$current_dayofyear" would be reset.
>
>
> SO HOW CAN I COUNT ELAPSED DAYS IF THE DATE FORMAT IS  01.01.2009?
>


Hi,

This is a very old problem pretty much every single programmer has bumped
into this one... So looking on CPAN <http://search.cpan.org> should be able
to provide you literally tens of modules used for date calculations in all
sorts and forms. I would advise having a look at Date::Manip which I found
very helpful for these kinds of problems. But if you are stubborn and like
to reinvent the wheel that is no problem of course but might be a little
more difficult...

Now I could try and write a whole story about how this can be done but the
problem is that there are so many issues, leap years, but also leap seconds,
and a few other tricks and problems. Personally I prefer to work with tried
and tested modules that have resolved all these problems for me so I can
focus on the actual problem rather then getting bogged down with the kinds
of only somewhat related things.

Regards,

Rob

Reply via email to