Calculations with date

2004-01-18 Thread danield
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


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Calculations with date

2004-01-18 Thread Dan Anderson
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


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Calculations with date

2004-01-18 Thread Owen Cook

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;
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Calculations with date

2004-01-18 Thread James Edward Gray II
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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response