Determine total days of month
Hello, I am trying to come up with a function, or formula calculation, to determine the total days of a given month, IE: April which has 30 days, Feb has 28 days, March has 31 days... etc Is there way to do this with Perl TIA, Mike(mickalo)Blezien === Thunder Rain Internet Publishing Providing Internet Solution that Work === -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Determine total days of month
There are plenty of modules out there to do this. Date::Calc is one, and a little creative use of Time::Local would also work, for example you could subtract one from the first day of the next month and then get the day. -Original Message- From: Mike Blezien [mailto:[EMAIL PROTECTED] Sent: Tuesday, April 04, 2006 11:50 AM To: Perl List Subject: Determine total days of month Hello, I am trying to come up with a function, or formula calculation, to determine the total days of a given month, IE: April which has 30 days, Feb has 28 days, March has 31 days... etc snip -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Determine total days of month
On 4/4/06, Mike Blezien [EMAIL PROTECTED] wrote: I am trying to come up with a function, or formula calculation, to determine the total days of a given month, IE: April which has 30 days, Feb has 28 Is there way to do this with Perl Nope. Perl is completely incapable of this sort of thing. :-) Have you looked for a module on CPAN yet? Many people before you have needed to work with dates and times. http://search.cpan.org Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Determine total days of month
# Sample code: use Date::Calc qw(:all); $days = Days_in_Month('2006','04'); print $days; # End Sample You'll need the Date::Calc module from cpan (http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod). On Tue, 2006-04-04 at 13:49 -0500, Mike Blezien wrote: Hello, I am trying to come up with a function, or formula calculation, to determine the total days of a given month, IE: April which has 30 days, Feb has 28 days, March has 31 days... etc Is there way to do this with Perl TIA, Mike(mickalo)Blezien === Thunder Rain Internet Publishing Providing Internet Solution that Work === -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Determine total days of month
On Tue, 4 Apr 2006, Mike Blezien wrote: I am trying to come up with a function, or formula calculation, to determine the total days of a given month, IE: April which has 30 days, Feb has 28 days, March has 31 days... etc Is there way to do this with Perl Don't calculate it. Just use a lookup table in a hash. Trivially, and not 100% accurate but maybe good enough: my %months = ( jan = 31, feb = 28, mar = 31, apr = 30, jun = 30, jul = 31, aug = 31, sep = 30, oct = 31, nov = 30, dec = 31 ); Depending on what you're doing, you may have to handle leap years. If you do, you can do something like this instead: if (leap_year($year)) { my %months = ( jan = 31, feb = 29, ... ); } else { my %months = ( jan = 31, feb = 28, ... ); } (In this example, leap_year($year) is left as an exercise. :-) For a lot of problems like this, where you know that there's a one-to-one mapping between things -- as, in this case, months and day counts -- the easiest way is just a hash-based lookup table. You can often solve these things with some kind of elaborate calculations -- say, to compute the day that a lunar-calendar based holiday (Easter, Hanukkah, Ramadan, Chinese New Year, etc) -- but a lot of the time it ends up being easier to just figure out the mappings in advance and hard-code that either in your script or in a resource data file that your script loads at run time. -- Chris Devers -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Determine total days of month
Joshua, thank you, that will do the trick. :) Mike - Original Message - From: Joshua Colson [EMAIL PROTECTED] To: beginners@perl.org Cc: Mike Blezien [EMAIL PROTECTED] Sent: Tuesday, April 04, 2006 1:58 PM Subject: Re: Determine total days of month # Sample code: use Date::Calc qw(:all); $days = Days_in_Month('2006','04'); print $days; # End Sample You'll need the Date::Calc module from cpan (http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod). On Tue, 2006-04-04 at 13:49 -0500, Mike Blezien wrote: Hello, I am trying to come up with a function, or formula calculation, to determine the total days of a given month, IE: April which has 30 days, Feb has 28 days, March has 31 days... etc Is there way to do this with Perl TIA, Mike(mickalo)Blezien -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Determine total days of month
backwards. it's Because Why? posted. top hadn't you if advice better even been have would it but , advice good is This On 4/4/06, Joshua Colson [EMAIL PROTECTED] wrote: # Sample code: use Date::Calc qw(:all); $days = Days_in_Month('2006','04'); print $days; # End Sample You'll need the Date::Calc module from cpan (http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod). If you're doing a lot of this, you're still going to want a lookup table; it's considerably faster than calulating the number of days for each month. Just grab Date::Calc where you need it: sub feb { return $year ? (Date::Calc::leap_year($year) ? 29 : 28) : '' } my %months = ( jan = 31, feb = \feb, ... ); HTH, -- jay -- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!
Re: Determine total days of month
My favorite is Date::Manip by Sullivan Beck - check www.cpan.org which does just about anything your can think of associated with date and time in just about any language use Date::Manip; $days=Date_DaysInMonth($m,$y); Mike Blezien wrote: Hello, I am trying to come up with a function, or formula calculation, to determine the total days of a given month, IE: April which has 30 days, Feb has 28 days, March has 31 days... etc Is there way to do this with Perl TIA, Mike(mickalo)Blezien === Thunder Rain Internet Publishing Providing Internet Solution that Work === -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response