http://qs321.pair.com?node_id=97120

ganesh_saravanan has asked for the wisdom of the Perl Monks concerning the following question: (dates and times)

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

Originally posted as a Categorized Question.

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

Replies are listed 'Best First'.
Re: how can i find number of days in a given year and month
by Celada (Monk) on Dec 05, 2005 at 18:36 UTC

    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).

Re: how can i find number of days in a given year and month
by brian_d_foy (Abbot) on Jan 03, 2005 at 23:57 UTC

    I've grown fond of Date::Simple.

    use Date::Simple qw(days_in_month); my $days = days_in_month( 2004, 2 );
Re: how can i find number of days in a given year and month
by grinder (Bishop) on Jul 16, 2001 at 23:31 UTC
    #! /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.

Re: how can i find number of days in a given year and month
by tye (Sage) on Jul 17, 2001 at 17:16 UTC
    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.

      # your 0 == $year%4 && 0 == $year%100 || 0 == $year%400; # should be 0 == $year%4 && 0 != $year%100 || 0 == $year%400;

      "Argument is futile - you will be ignorralated!"

Re: how can i find number of days in a given year and month
by davorg (Chancellor) on Jul 17, 2001 at 12:15 UTC

    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

Re: how can i find number of days in a given year and month
by Anonymous Monk on Aug 02, 2004 at 13:26 UTC
    1450

    Originally posted as a Categorized Answer.

A reply falls below the community's threshold of quality. You may see it by logging in.