Monday, January 30, 2012

Finding the first day of a week–ISO 8601

[See on how to get the correct week number from a date]

I’m working on an application where we work with week numbers and of course there was an error in getting everything to work correct with week numbers. For a specific week I want to get the Monday of that week. With the ISO 8601 standard a week starts on a Monday and ends on a Sunday, and I’m dealing with a calendar which states that week 1 = the first week with 4 full days.

Our current code was not working, and checking on StackOverflow I found a couple of samples in order to fix this:

The flaw in these solutions is that they all base the week calculation on the first Monday of the year. Reading up on ISO 8601 at Wikipedia the solution is simple:
The week number can be described by counting the Thursdays: week 12 contains the 12th Thursday of the year.
Instead of basing the code on a Monday, use Thursday and all boundary conditions with week 1, 52 and 53 are resolved. Then subtract 3 days at the end to get the Monday.

The following code works for all boundary conditions which the previous code samples fail at:

public static DateTime FirstDateOfWeek(int year, int weekOfYear)
    DateTime jan1 = new DateTime(year, 1, 1);
    int daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;

    DateTime firstThursday = jan1.AddDays(daysOffset);
    var cal = CultureInfo.CurrentCulture.Calendar;
    int firstWeek = cal.GetWeekOfYear(firstThursday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

    var weekNum = weekOfYear;
    if (firstWeek <= 1)
        weekNum -= 1;
    var result = firstThursday.AddDays(weekNum * 7);
    return result.AddDays(-3);

Friday, January 27, 2012

How to do Visual Best Bets for built-in SharePoint Search

Marcus Johansson did a post about the use of Best Bets this morning which inspired me to write this post. Read his post for more thoughts around the use of best bets.

Visual Best Bets is a feature of FAST Search for SharePoint which lets you point to a file with html content to be displayed above your search results. For example an image, silverlight or flash content can be used to graphically enhance what is linked to the keyword term. The Visual Web Part uses an iframe to accomplish this and loads up your content inside the iframe. This is useful as you can easily edit the html file at will.

But why go the extra mile for a separate file, or opt in for FS4SP for this feature? The Best Bet web part support the showing of keywords and keyword definitions. Keyword definitions are formatted as html. And a definition with html formatting is in effect a Visual Best Bet. (If you have more than one Visual Best Bet you want to assign to the keyword you would have to add them all to the same html for this to work.)