What week is it?

I noticed many people are using the weeks, as a way of describing time. I have never used weeks, so I never know where to look up what date, a week is.

I have always thought that it would be easy to make a program in .NET that do the work for me, and it really was.

You can see the result below.

If you want to know what week a specific date is, click the calendar, and the week will be shown in textbox below.

If you want to know what date a specific week starts with, select a date in the calendar, in the year you want to get the week from. Type in the week in the textbox, and click the what date button.

***Example removed***

Oh, and if you want to use the code, it is just these few lines:

int GetWeekOfYear(DateTime date)
{
 
GregorianCalendar cal = new GregorianCalendar();
 
return cal.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}

private DateTime GetDateByWeek(int year, int week)
{
  DateTime date = new DateTime(year, 1, 1);
 
return date.AddDays(7 * week – (int)date.DayOfWeek-6);
}

New job

Long time no see. It’s been a long time since my last post. That is partly due to my new job. I just changed workplace from one of the biggest software companies in Scandinavia, to a little software consulting company with 18 employees.

My short-sighted plan with this blog, was actually to write about my points of view on software architecture, and what I thought tomorrows killer applications would look like.

Realizing that I don’t have time for that, with a newborn child, a new job, and some other small project on the sideline, this will blog will instead serve as a place where I post code tips, and “strange” behaviours in .NET, that I come across, and how to solve them.

As I get some projects up and running, this web server will also serve as a test bed, and a place to download sample aps, and sample code.

Well, that’s all for now folks.
Over and out!

Impersonating administrator

Developing to sharepoint 2003, you often need to access data, that only an administrator has access to. To do that in code, you need to impersonate an administrator. The classic solution looks like this. 

 

Add the following code to your project: 

private WindowsImpersonationContext ctx = null;
 public void UseAppPoolIdentity(){
  try{
   if (!WindowsIdentity.GetCurrent().IsSystem){
    ctx = WindowsIdentity.Impersonate(System.IntPtr.Zero);
   }
  }

  catch{}
}
public void ReturnToImpersonatingCurrentUser(){
 try{
  if(ctx != null)
   ctx.Undo();
  }
  catch{}
 }
 

It is used from somewhere else in the code, like this:

UseAppPoolIdentity();
…Do something that only admins can do…
ReturnToImpersonatingCurrentUser(); 

But for some reason, this doesn’t always work, so you have to update the function: ReturnToImpersonatingCurrentUser(), so it looks like this: 

public void UseAppPoolIdentity(){
 try{
  if (!WindowsIdentity.GetCurrent().IsSystem){
   ctx = WindowsIdentity.Impersonate(System.IntPtr.Zero);
   WindowsIdentity.Impersonate(WindowsIdentity.GetCurrent().Token);
  }
 }
 catch{}
}

Why this work, I don’t know. I guess it’s just one of sharepoints many special features 🙂