May 2008

Number of Weeks In A Month

So I am working on a .NET web app that needs to calculate how many weeks there are in a month. I have come upon some solutions but I put one together that I like better than the rest…

public int Weeks(int year, int month, DayOfWeek firstDayOfWeek)
{

CultureInfo ciCulture = new CultureInfo(“en-US”);
System.Globalization.Calendar cal = ciCulture.Calendar;

return cal.GetWeekOfYear(DateTime(year, month, DateTime.DaysInMonth(year, month)), ciCulture.DateTimeFormat.CalendarWeekRule, firstDayOfWeek) – cal.GetWeekOfYear(DateTime(year, month, 1), ciCulture.DateTimeFormat.CalendarWeekRule, firstDayOfWeek);
}

This lets the .NET Framework do the thinking for me, and it is Globalization ready. Anywho the logic is::

Find the week number for the end of the month, subtract the week number for the start of the month… bam! Number of weeks.

The method is dependent on what day of the week a new week starts. So if your week starts on a Monday throw that it’s way.

Should this be in the framework? Probably …

PS:

using System.Globalization; //<-- ADD THAT;

MySQLdb error …

So I have been looking at the Django Project ( framework… whatever ). Installing that is pretty easy I would say. It was easier than installing SQL Server 2005 on Vista, definitely fewer warnings. It’s all fun and games until you come to the installation of MySQL and the driver for Python.

The driver can be found at :: http://www.djangoproject.com/r/python-mysql/ , or it’s respective SourceForge location. Anyway…

First off I didn’t have XCode installed so gcc did not exist. That was an easy fix I just downloaded XCode 3 and thought I’d be good to go. Problem came when I tried to run the setup.py script and found that gcc exits with an error. The problem is that XCode 3 installs gcc4 and there is a file in this driver called _mysql.c and it needs gcc3. This is due to the declaration of a variable or 2 and some pre processor directives that need to be changed/removed from that file.

The exact error was :

“/usr/include/sys/types.h:92: error: two or more data types in declaration specifiers”

So I tried to install gcc3 first, that’s a trap! You can try it but that failed for me for several reasons. So after much bangage of my head on the table I found this page that explained how to change that file::

http://www.keningle.com/?p=11

Fun stuff! You can follow the instructions there or you can download the modified package here:

mysql-python-122.zip

Note: You may want to check the actual SourceForge release of the python driver, it may be working by the time you read this. This is specific to version 1.2.2 .

Note 2: If you break your system running / doing anything you read or downloaded here I am not responsible, nor my webhost, or anyone else other than yourself.