Wednesday, January 04, 2006

Time and date functions in c#

I have been used to work with the COleDateTime objects in Visual Studio 98. There is a similar solution in C#?. Yes. The DateTime and TimeSpan Object. There are very similar to the useful COleDateTime and COleDateTimeSpan objects.

Here is a sample program that I have made to test the zeller´s congruence algorithm to determine the day of week from the Date. I have tested it from now until the 2100.


/* Zeller's Congruence to determine day of week */
int get_dayofweek(int date)
{
int d = date % 100;
int m = (date % 10000) / 100;
int y = date / 10000;
if (m < 3)
{
m = m + 12 ;
y = y - 1;
}
return ((2 + d + (13*m-2)/5 + y + y/4 - y/100 + y/400) % 7);
}


private void TestDayOfWeekAlgo()
{
DateTime t=new DateTime(2005,1,1,12,0,0);
TimeSpan i=new TimeSpan(1,0,0,0);
do
{
t+=i;
if ((int)t.DayOfWeek!=get_dayofweek(t.Year*10000+t.Month*100+t.Day))
{
MessageBox.Show("The day of week algorithm is not working correctly","Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
} while(t.Year<2100);

}

If you have time, you can read the ISO 8601 document about standard dates, and also some interesting things as about how to number the differents weeks of the year.

Monday, January 02, 2006

How to share files between multiple files without making a class library

As I continue programming in c#, I have some classes that are shared between various projects. And I know that it is not a very good Idea to have exactly the same files placed in the different projects.

I have been looking for a way of doing this, and the only solution seemed to create a class library, compile it to a DLL file and add a reference to this class library in all the projects. I don´t like this solution because I prefer that a program fits only in one EXE file.

After looking a little more, I have found a easy solution. When you add the .cs file that you want to share, (Menu project, add existing file), select in the the right bottom button of the window the option "Link".

Sharing the files in this way seems to cause small problems in the control version software that we use (P4Q), but it seems very easy to work in this way.