Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
There's more than one way to do things
 
PerlMonks  

how can i find number of days in a given year and month

( #97120=categorized question: print w/ replies, xml ) Need Help??
Contributed by ganesh_saravanan on Jul 16, 2001 at 22:25 UTC
Q&A  > dates and times


Answer: how can i find number of days in a given year and month
contributed by Celada

The mktime has a nice feature which you can exploit for this purpose: you can supply values that are out of range. What happens if you give as input to mktime the zeroth day of this month? Why, you get the last day of last month, of course! mktime is part of your standard C library, so you should get results consistent with everything else on the system, too (that's why I'm not so fond of some of the solutions proposed so far which reimplement the logic).

So the solution is to ask for the zeroth day of the month following the one you are interested in, and see what you get. Also you don't have to worry about what happens in December and you ask for the following month (the 13th month) because mktime deals with that too.

#!/usr/bin/perl use POSIX; ($month, $year) = (12, 2005); $lastday = POSIX::mktime(0,0,0,0,$month-1+1,$year-1900,0,0,-1); @_ = localtime($lastday); print $_[3], "\n";

Note: silly -1+1 used to clarify that we are putting the month into the 0 .. 11 range expected by mktime (-1) and asking for the following month (+1).

Answer: how can i find number of days in a given year and month
contributed by brian_d_foy

I've grown fond of Date::Simple.

use Date::Simple qw(days_in_month); my $days = days_in_month( 2004, 2 );
Answer: how can i find number of days in a given year and month
contributed by grinder

#! /usr/bin/perl -w use strict; use Date::Calc qw/Days_in_Month/; while( <DATA> ) { chomp; my( $year, $month ) = split; my $days = eval { Days_in_Month( $year, $month) }; $days = 'who knows?' if $@; print "$year/$month => $days\n"; } __DATA__ 1996 1 1996 2 1998 2 2000 2 1900 2 1600 2 2001 2 2001 7 2001 12 2001 13 2001 0

hint: if you know your data are clean then you can omit the eval and error check.

Answer: how can i find number of days in a given year and month
contributed by tye

my @monthDays= qw( 31 28 31 30 31 30 31 31 30 31 30 31 ); sub MonthDays { my $month= shift(@_); my $year= @_ ? shift(@_) : 1900+(localtime())[5]; if( $year <= 1752 ) { # Note: Although September 1752 only had 19 days, # they were numbered 1,2,14..30! return 19 if 1752 == $year && 9 == $month; return 29 if 2 == $month && 0 == $year % 4; } else { return 29 if 2 == $month and 0 == $year%4 && 0 == $year%100 || 0 == $year%400; } return $monthDays[$month-1]; }

This covers Julian and Gregorian dates and uses Julian month lengths even prior to the Julian calendar.

Answer: how can i find number of days in a given year and month
contributed by davorg

You can achive this without using external modules:

#!/usr/bin/perl -w use strict; use Time::Local; while( <DATA> ) { chomp; my ($year, $month) = split; do { warn "Invalid month: $month\n"; next } if $month > 12 or $month < 1; my $next_year = ($month == 12) ? $year + 1 : $year; my $next_month = timelocal(0, 0, 0, 1, $month % 12, $next_year); my $days = (localtime($next_month - 86_400))[3]; print "$year/$month => $days\n"; } __DATA__ 1996 1 1996 2 1998 2 2000 2 1900 2 1600 2 2001 2 2001 7 2001 12 2001 13 2001 0

Update: Small fixes to code as recommended by tye

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (11)
    As of 2014-04-23 22:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (556 votes), past polls