Calculations with date

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Calculations with date

    Hello all,

    I am looking for an advice or suggestion in this task:

    I do have a Perl script which contains 2 variables ($low, $high). The
    value assigned to them depends on which day am I running that Perl
    script. Moreover, the variable $low has always be the first day of the
    previous month and similarly the variable $high the last day of the
    previous month.

    For instance:
    If I run this script today (2004, January 18), the low should be (18
    days of current month + total days of previous month (2003, December
    (31)))= 49. The $high
    would get (how many days ago was the last day of the previous month) =
    18.

    $low = 49
    $high = 18

    I am doing this calculations manually before I run the script. I would
    like to have this value determined and substituted inside the script. In
    addition the script should know how much day each month has and also
    correctly recognize when the month February has 28 and when 29 days and
    so on...


    I would appreciate any suggestions or examples. Thank you for your time.

    danield

    Danield Guest

  2. Similar Questions and Discussions

    1. Date calculations from FORM field
      Hello everyone, I am trying to build a site for my company that to replace our current leave request database. This is how I would like the site...
    2. negative calculations
      Not sure if this is within CF or MySQL, but I have a item with I want to multiply quantity by a negative dollar value. The number and amount are...
    3. Date calculations for a countdown
      I'm trying to display a countdown clock to display H:M:S to go to an event and it needs to be independant of the client's clock. I can send the...
    4. date calculations
      Nadya, In days? =DateDiff("d",,) or simply = - -- Fred Please reply only to this newsgroup.
    5. column calculations
      create a computed column to add year to the existing column See following example. CREATE TABLE dbo.t2 ( dt datetime NULL, next_yr AS...
  3. #2

    Default Re: Calculations with date

    100:1 there's a date arithmetic module on CPAN which would do exactly
    what you need.

    However, (at least from my point on the net), CPAN appears to be down.

    -Dan

    Dan Anderson Guest

  4. #3

    Default Re: Calculations with date


    On Sun, 18 Jan 2004, danield wrote:
    >
    > I am looking for an advice or suggestion in this task:
    >
    > I do have a Perl script which contains 2 variables ($low, $high). The
    > value assigned to them depends on which day am I running that Perl
    > script. Moreover, the variable $low has always be the first day of the
    > previous month and similarly the variable $high the last day of the
    > previous month.
    >
    > For instance:
    > If I run this script today (2004, January 18), the low should be (18
    > days of current month + total days of previous month (2003, December
    > (31)))= 49. The $high
    > would get (how many days ago was the last day of the previous month) =
    > 18.
    >
    > $low = 49
    > $high = 18
    >
    > I am doing this calculations manually before I run the script. I would
    > like to have this value determined and substituted inside the script. In
    > addition the script should know how much day each month has and also
    > correctly recognize when the month February has 28 and when 29 days and
    > so on...

    If you have Date::Calc installed, run this and see if it gives you any
    clues

    #!/usr/bin/perl -w

    use Date::Calc qw(Days_in_Month Add_Delta_YM);

    @t = localtime(time);
    $mon = $t[4]+1; $yr = $t[5]+1900; $day=$t[3];

    $days_in_month = Days_in_Month($yr,$mon);
    print "$days_in_month \t $yr \t $mon\n";

    ($yr,$mon,$day) = Add_Delta_YM($yr,$mon,$day,0,-1);

    $days_in_month = Days_in_Month($yr,$mon);
    print "$days_in_month \t $yr \t $mon\n";


    Owen Cook Guest

  5. #4

    Default Re: Calculations with date

    On Jan 18, 2004, at 5:20 PM, danield wrote:
    > Hello all,
    >
    > I am looking for an advice or suggestion in this task:
    >
    > I do have a Perl script which contains 2 variables ($low, $high). The
    > value assigned to them depends on which day am I running that Perl
    > script. Moreover, the variable $low has always be the first day of the
    > previous month and similarly the variable $high the last day of the
    > previous month.
    >
    > For instance:
    > If I run this script today (2004, January 18), the low should be (18
    > days of current month + total days of previous month (2003, December
    > (31)))= 49. The $high
    > would get (how many days ago was the last day of the previous month) =
    > 18.
    >
    > $low = 49
    > $high = 18
    If I understand the request correctly, the following seems to work:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $high = (localtime)[3];
    my $low = (localtime time - $high * 24 * 60 * 60)[3] + $high;

    print "Low: $low, High: $high\n";

    __END__

    Hope that helps.

    James

    James Edward Gray II Guest

Posting Permissions

  • You may not post new threads
  • You may 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