Determine DaysInMonth($month)

E

Eric

Hi,

Can you help me write a piece of code to do this:

my $daysinmonth = &DaysInMonth($month)

Where $month is the month num (1-12).

I don't have the ability to install modules as I'm on a shared server. And I
don't have Time::DaysInMonth installed.

Thank you!


Eric
 
C

Carsten Aulbert

Eric said:
Hi,

Can you help me write a piece of code to do this:

my $daysinmonth = &DaysInMonth($month)

Where $month is the month num (1-12).

That's pretty simple, all month have (at least) 28 days ;-)

Ok, enough of the kidding:

sub DaysInMonth {
my $month = shift;
my $year = shift; # need full year, i.e. 03 is _not_ sufficient
my @month_length = (0,31,28,31,30,31,30,31,31,30,31,30,31);

# only special month is February
return $month_length[$month] unless $month == 2;

# February has always 28 days, except if the year is
# dividable by 4 (without remainder). Exceptions are
# full centuries

return 29 if $year % 4 == 0 and $year % 100 != 0;
return 29 if $year % 400 == 0;
return 28;
}

That should work and does not need extra modules.

HTH
Carsten
 
M

Matt Garrish

Eric said:
Hi,

Can you help me write a piece of code to do this:

my $daysinmonth = &DaysInMonth($month)

Where $month is the month num (1-12).

Create a hash with all the values:

my %DaysInMonth = ( 1 => 31, 2 => 28, 3 => 31);

my $daysinmonth = $DaysInMonth{$month};

Of course, you'll have to add an extra hash value for leap years and check
whether February should have 28 or 29 days.

Matt
 
M

Matt Garrish

Matt Garrish said:
Create a hash with all the values:

my %DaysInMonth = ( 1 => 31, 2 => 28, 3 => 31);

Too early. I was thinking of using the abbreviated month returned by
localtime and not paying attention to what I was writing (i.e., 'Jan' => 31,
'Feb' => 28). You'd be better off using an array and incrementing the value
of entry 2 if it happens to be a leap year...

Matt
 
M

Matt Garrish

Matt Garrish said:
You'd be better off using an array and incrementing the value
of entry 2 if it happens to be a leap year...

And before that gets corrected, I obviously mean [2], not the second entry
in the array, which would be January's. Ugh, I'm going back to bed...

Matt
 
C

Carsten Aulbert

Matt said:
Create a hash with all the values:

my %DaysInMonth = ( 1 => 31, 2 => 28, 3 => 31);

Why a hash? I think an array is not so redundent, isn't it?

CA
 
R

Richard Gration

Hi,
Can you help me write a piece of code to do this: my $daysinmonth =
&DaysInMonth($month) Where $month is the month num (1-12). I don't
have the ability to install modules as I'm on a shared server. And I
don't have Time::DaysInMonth installed. Thank you!
Eric

sub DaysInMonth {
return (31,28,31,30,31,30,31,31,30,31,30,31)[(my $month = shift) - 1]
}

Might be more useful with a year specification ...

Rich
 
R

Richard Gration

"Eric" <[email protected]> said:
Hi,
Can you help me write a piece of code to do this: my $daysinmonth =
&DaysInMonth($month) Where $month is the month num (1-12). I don't
have the ability to install modules as I'm on a shared server. And I
don't have Time::DaysInMonth installed. Thank you! Eric
sub DaysInMonth {
return (31,28,31,30,31,30,31,31,30,31,30,31)[(my $month = shift) - 1]
}
Might be more useful with a year specification ... Rich

Even better ...

sub DaysInMonth {
return (0,31,28,31,30,31,30,31,31,30,31,30,31)[shift]
}
 
J

Jürgen Exner

Eric wrote:
[...]
I don't have the ability to install modules as I'm on a shared
server.

So what? That's not a valid excuse.
Apparently you can install your own programs. Then you can install your own
modules, too. Please see the FAQ about how to do that ("How do I keep my own
module/library directory?")
And I don't have Time::DaysInMonth installed.

Well, you can still copy and paste the code from Time::DaysInMonth into your
own code, can't you (unless this module is not written in Perl, but I don't
think so).

jue
 
M

Malte Ubl

Eric said:
Hi,

Can you help me write a piece of code to do this:

my $daysinmonth = &DaysInMonth($month)

Where $month is the month num (1-12).

You really dont want to this yourself. Finding leap years, as others
suggested, is not as easy as one might think on the first look.

I don't have the ability to install modules as I'm on a shared server. And I
don't have Time::DaysInMonth installed.

If you can install a regular Perl application, you can install any other
module which is written in Perl, too.

Bye
malte
 
T

Tad McClellan

[ please include an attribution when you quote someonw ]

Wasn't trolling. What made you think I was?


You said so yourself in:

Message-ID: <[email protected]>

For the sole purpose of annoying you.


Posting only to annoy folks is pretty much the definition of trollism.
 
A

Anno Siegel

Carsten Aulbert said:
Hi,

Can you help me write a piece of code to do this:

my $daysinmonth = &DaysInMonth($month)

Where $month is the month num (1-12).

That's pretty simple, all month have (at least) 28 days ;-)

Ok, enough of the kidding:

sub DaysInMonth {
my $month = shift;
my $year = shift; # need full year, i.e. 03 is _not_ sufficient
my @month_length = (0,31,28,31,30,31,30,31,31,30,31,30,31);

# only special month is February
return $month_length[$month] unless $month == 2;

# February has always 28 days, except if the year is
# dividable by 4 (without remainder). Exceptions are
# full centuries

return 29 if $year % 4 == 0 and $year % 100 != 0;
return 29 if $year % 400 == 0;
return 28;
}

The code is fine, as far as I can tell from manual inspection, but the
comment doesn't match the code. It fails to acknowledge the exception
from the exception for years divisible by 400.

I mention this not for the pure joy of pedantry (or not purely for the
joy of pedantry), but because it is a good illustration of how fragile
comments are. I mean, a slip like that happens easily, in fact it's hard
to avoid entirely when you do a lot of commenting. It wouldn't even hurt
much in this case because the leap year algorithm is well known and easily
accessible.

But finding one comment in a program that's clearly out of sync with the
code means that you'll have to think of that possibility with every comment
in the program. That reduces their utility considerably. Old story about
one bad apple...

I guess what I'm saying is not that comments are useless, but they take
more effort, in writing *and* maintenance, than is usually acknowledged.
They should be considered potentially expensive parts of the code that
are to be used sparingly, not proliferated.

Anno
 
E

Eric

Tad McClellan said:
[ please include an attribution when you quote someonw ]

Wasn't trolling. What made you think I was?


You said so yourself in:

Message-ID: <[email protected]>

For the sole purpose of annoying you.


Posting only to annoy folks is pretty much the definition of trollism.

If you read that whole thread you'd realise you've taken me completely out
of context. I was being sarcastic.

You're probably American I guess.

Eric
 
E

Erik Tank

You can install the perl modules locally:
http://servers.digitaldaze.com/extensions/perl/modules.html#install

This way you don't need to recreate the module nor do you have to have
root privileges to intall it.

The only draw back I see with this is that you need to have the 'use
lib <path>' statement so if you move the program to a different server
you need to make sure that this is correct before you are able to use
it again.

PS: I would recommend using Date::Calc, but I think that is more of a
personal preference.
 
M

Matt Garrish

Anno Siegel said:
The code is fine, as far as I can tell from manual inspection, but the
comment doesn't match the code. It fails to acknowledge the exception
from the exception for years divisible by 400.

I mention this not for the pure joy of pedantry (or not purely for the
joy of pedantry), but because it is a good illustration of how fragile
comments are. I mean, a slip like that happens easily, in fact it's hard
to avoid entirely when you do a lot of commenting. It wouldn't even hurt
much in this case because the leap year algorithm is well known and easily
accessible.

Now that I'm a little more awake...

I don't disagree with you that commenting scripts is an art unto itself, but
I think you are being more than a little pedantic in this case. He clearly
wrote that "full centuries" are an exception. I think it would be encumbent
upon the person reading the code to figure out that "full centuries"
includes a check that they aren't divisible by 400. Instead of changing his
comment, he probably could have changed his code to:

if ($year % 4 == 0) {

return 29 if $year % 100 != 0 or $year % 400 == 0;

}

This way the century checks are more clearly delineated from the "divisible
by 4" rule. Either way, I wouldn't go so far as to say that his original
comment was misleading in any way.

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top