Professional Web Applications Themes

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. #1

    Default 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. #2

    Default 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. #3

    Default 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. #4

    Default Re: Date calculation

    Jan Eden wrote:
     


    try not to make it *more* complicated :)

    You posted your own answer in the first post.

    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. #5

    Default 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;
    use Date::Calc qw/Delta_Days Date_to_Days Add_Delta_Days/;

    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++ ){
    my date = Add_Delta_Days(chk_in,$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. #6

    Default 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. #7

    Default Re: Date calculation


    ----- Original Message -----
    From: WC -Sx- Jones
    Sent: 21.04.2004, 14:23 Uhr
     
    >
    >
    >try not to make it *more* complicated :)
    >
    >You posted your own answer in the first post.
    >
    >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. #8

    Default 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. #9

    Default 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.

    Thanks for your help,

    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. #10

    Default 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

Similar Threads

  1. Date calculation in ActionScript
    By gregd in forum Coldfusion Flash Integration
    Replies: 2
    Last Post: February 25th, 01:25 AM
  2. Date calculation
    By pugs in forum Macromedia ColdFusion
    Replies: 1
    Last Post: June 1st, 12:36 PM
  3. Date Calculation & Formatting of Output
    By coryd218 in forum Macromedia ColdFusion
    Replies: 12
    Last Post: May 16th, 04:02 PM
  4. Calculation based on calculation
    By Point_Man@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 0
    Last Post: April 20th, 03:35 AM
  5. Find Date in Calculation field
    By Bigfoot1956 in forum FileMaker
    Replies: 3
    Last Post: July 21st, 09:40 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139