Percentage of work days in a month

64-bit Perl, 67 68

Perl 5.10 or later, run with perl -E 'code here' or perl -M5.010 filename

map{$d++,/^S/||$w++if$_=`date -d@ARGV-$_`}1..31;say int.5+100*$w/$d

Concessions to code size:

  • locale-sensitive: it counts as work days the days whose date output don't start with a capital S. Run under LC_ALL=C if in doubt.
  • output is pure and well-formatted, but there's "garbage" on stderr on months shorter than 31. 2> /dev/null if upset.
  • for some reason, my version of date considers 2817-12 an invalid month. Who knew, GNU new apocalypse is due! Requires a 64 bit build of date for dates after 2038. (Thanks Joey)

PHP - 135

I made it in PHP because I had a similar problem to treat a few days ago.

<?php $a=array(2,3,3,3,2,1,1);$t=strtotime($argv[1]);$m=date(t,$t);echo round((20+min($m-28,$a[date(w,strtotime('28day',$t))]))/$m*100)

(Somewhat) More legibly, and without notices about constants being used as strings:

$additionalDays = array(2, 3, 3, 3, 2, 1, 1);
$timestamp = strtotime($argv[1]);
$daysInMonth = date('t', $timestamp);
$limit = $daysInMonth - 28;
$twentyNinthDayIndex = date('w', strtotime("+28 days", $timestamp));
$add = $additionalDays[$twentyNinthDayIndex];
$numberOfWorkDays = 20 + min($limit, $add);
echo round($numberOfWorkDays / $daysInMonth * 100);

This is made possible by a very simple algorithm to compute the number of work days in a month: check for the weekdayness of the 29th, 30th and 31st (if those dates exist), and add 20.

Python 152 Characters

from calendar import*
for d in range(1,r+1):
 if weekday(y,m,d)>4:c-=1
print '%.f'%(c*100./r)



Code Golf