# Date calculation - PERL Beginners

Hi, I need to find the number of days between two dates. The Perl Cookbook provides this solution: use Date::Calc qw(Delta_Days); bree = (1981, 6, 16); # 16 Jun 1981 nat = (1973, 1, 18); # 18 Jan 1973 \$difference = Delta_Days(nat, bree); But there's a hook: I need to calculate a room price based on the number ofdays, where a day costs \$80 during summer (July 1 through September 15) and \$55 otherwise (September 16 through June 30). Since people can book a room for any interval, I somehow have to integrate the dividing day and month values, something ...

1. ## Date calculation

Hi,

I need to find the number of days between two dates. The Perl Cookbook provides this solution:

use Date::Calc qw(Delta_Days);
bree = (1981, 6, 16); # 16 Jun 1981
nat = (1973, 1, 18); # 18 Jan 1973
\$difference = Delta_Days(nat, bree);

But there's a hook: I need to calculate a room price based on the number ofdays, where a day costs \$80 during summer (July 1 through September 15) and \$55 otherwise (September 16 through June 30).

Since people can book a room for any interval, I somehow have to integrate the dividing day and month values, something along the lines of

if \$mmdd > 0915 and \$mmdd < 0630) ...

in my calculation to allow for any combination of \$80 and \$55 days.

Any hint on how to proceed from here?

Thanks,

Jan

--
There's no place like ~/
Jan Guest

2. ## Re: Date calculation

Jan Eden wrote:

((\$mmdd > 0915) && (\$mmdd < 0630)) ? \$55 : \$80;

--
_Sx_ http://youve-reached-the.endoftheinternet.org/ _____
http://jaxpm.insecurity.org/
http://cis4dl.insecurity.org/
Wc Guest

3. ## Re: Date calculation

----- Original Message -----
From: WC -Sx- Jones
Sent: 21.04.2004, 13:32 Uhr

>
>((\$mmdd > 0915) && (\$mmdd < 0630)) ? \$55 : \$80;[/ref]

Thanks, I imagined a test like this as a subroutine, returning

sub season {
\$date = shift;
((\$date > 0915) && (\$mmdd < 0630)) ? return 0 : return 0;
}

and then calculate like this (Delta_Days takes two three element lists, I know):

if (season(\$start_date) && season(\$end_date) {
\$season_days = Delta_Days(\$start_date, \$end_date)
} elsif season(\$start_date) {

[snip]

I will work it out.

Thanks again,

Jan
--
There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. - Jeremy S. Anderson
Jan Guest

4. ## Re: Date calculation

Jan Eden wrote:

try not to make it *more* complicated :)

Consider:

((season(\$start_date) && (season(\$end_date)) ?
go sub for \$80 : or go sub for \$55;

Or however you want to test for Summer.

Me, Im crazy -

# What you are trying, you don't neend a module:
my (\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday) = localtime;

# Then -
my \$vdate = sprintf("%02d", \$mon + 1) . '/' . sprintf("%02d", \$mday);
\$vdate .= '/' . (\$year += 1900);

# Year doesn't matter;
my \$xdate = sprintf("%02d", \$mon + 1) . sprintf("%02d", \$mday);

print "Today is \$vdate and our target is \$xdate\n\n";

# are we inside Summer?

my \$start = 701;
my \$end = 915;

((\$xdate >= \$start) &&
(\$xdate <= \$end)) ? print "\\$80 Woohooo\n\n"
: print "\\$55 isn't too shabby\n\n";

# Fake Summer -
\$xdate = 801;

((\$xdate >= \$start) &&
(\$xdate <= \$end)) ? print "\\$80 Woohooo\n\n"
: print "\\$55 isn't too shabby\n\n";

__END__

Cheers!
--
_Sx_ http://youve-reached-the.endoftheinternet.org/ _____
http://jaxpm.insecurity.org/
http://cis4dl.insecurity.org/
Wc Guest

5. ## Re: Date calculation

----- Original Message -----
From: "Jan Eden" <org>
Newsgroups: perl.beginners
To: "Perl Lists" <org>
Sent: Wednesday, April 21, 2004 1:29 PM
Subject: Date calculation

Hi,

I need to find the number of days between two dates. The Perl Cookbook
provides this solution:

use Date::Calc qw(Delta_Days);
bree = (1981, 6, 16); # 16 Jun 1981
nat = (1973, 1, 18); # 18 Jan 1973
\$difference = Delta_Days(nat, bree);

But there's a hook: I need to calculate a room price based on the number of
days, where a day costs \$80 during summer (July 1 through September 15) and
\$55 otherwise (September 16 through June 30).

Since people can book a room for any interval, I somehow have to integrate
the dividing day and month values

Hello Jan
Maybe something like this would do what you want :-)

#!/usr/bin/perl
use strict;
use warnings;

my \$beg = Date_to_Days(2004, 7, 1);
my \$end = Date_to_Days(2004, 9, 15);

my chk_in = (2004, 6, 28);
my chk_out = (2004, 7, 5);

my \$j = Delta_Days(chk_in,chk_out);

for ( my \$i = 0; \$i <= \$j; \$i++ ){
printf("%4d-%02d-%02d\t", date);
my \$day = Date_to_Days(date);
print \$day >= \$beg && \$day <= \$end ? "\\$80.00\n" : "\\$55.00\n";
}

I took this example from Dtae::Calc man pages
- perldoc Date::Calc

The output was
2004-06-28 \$55.00
2004-06-29 \$55.00
2004-06-30 \$55.00
2004-07-01 \$80.00
2004-07-02 \$80.00
2004-07-03 \$80.00
2004-07-04 \$80.00
2004-07-05 \$80.00

HTH,
Chris

Chris Guest

6. ## Re: Date calculation

----- Original Message -----
From: "Chris Charley" <com>
Newsgroups: perl.beginners
To: <org>
Sent: Wednesday, April 21, 2004 6:48 PM
Subject: Re: Date calculation

This for loop should be: for ( my \$i = 0; \$i < \$j; \$i++ )
so that \$i never equals \$j (checkout day).

Chris Guest

7. ## Re: Date calculation

----- Original Message -----
From: WC -Sx- Jones
Sent: 21.04.2004, 14:23 Uhr

>
>
>try not to make it *more* complicated :)
>
>
>Consider:
>
>((season(\$start_date) && (season(\$end_date)) ?
> go sub for \$80 : or go sub for \$55;
>
>Or however you want to test for Summer.[/ref]

I'm afraid it's not that simple. Consider a start date of June 13 and an end date of June 20. In that case, season(\$start_date) will return false, making the whole test expression false. But only the first three days will cost \$55 in that case. So the calculation should be:

\$other_days = Delta_Days(\$start_day, \$div_day);
\$season_days = Delta_Days(\$div_day, \$end_day);

And I have exactly three other situations:

\$season_days = Delta_Days(\$start_day, \$end_day);
---
\$other_days = Delta_Days(\$start_day, \$end_day);
---
\$season_days = Delta_Days(\$start_day, \$div_day2);
\$other_days = Delta_Days(\$div_day2, \$end_day);

To capture the four possible configurations correctly, the condition given above is not sufficient.

Thanks,

Jan
--
There are 10 kinds of people: those who understand binary, and those who don't
Jan Guest

8. ## Re: Date calculation

Hi Chris,

----- Original Message -----
From: Chris Charley
Sent: 21.04.2004, 18:48 Uhr

Thanks! That's exactly what I needed. Works perfectly - and if you need an apartment for rent at the Lago di Garda (Italy)... ;-)

- Jan
--
There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. - Jeremy S. Anderson
Jan Guest

9. ## RE: Date calculation

Hi Joel,

----- Original Message -----
From: Stout, Joel R
Sent: 21.04.2004, 17:46 Uhr

I thought about this method. But in the script based on Chris' suggestion (s. my reply on the list) I just have to change two vars (\$begin_season and \$end_season) to prolong/shorten the season.

Then again, I'd rather offer discounts (e.g. for booking more than 14 days)and discount coupons during winter time than modify the standard prices directly.

Jan
--
A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
Jan Guest

10. ## Re: Date calculation

----- Original Message -----
From: "Jan Eden" <org>
Newsgroups: perl.beginners
To: "Chris Charley" <com>; "Perl Lists"
<org>
Sent: Thursday, April 22, 2004 3:56 AM
Subject: Re: Date calculation

Hi Chris,

Thanks! That's exactly what I needed. Works perfectly - and if you need an
apartment for rent at the Lago di Garda (Italy)... ;-)

- Jan

Glad my suggestion worked for you Jan. Yes - my last time to Europe was on a
motorcycle tour but didn't make it to Italy!
Chris

Chris Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•