Microsoft doesn't know what week it is. Or at least they can't calculate it correctly. There's a nice standard, ISO 8601, for how to represent date and time. The Calendar and GregorianCalendar classes implement GetWeekOfYear incorectly, and return the incorrect week for some dates. For example, I realized that they claim that the week number for the date 2007-12-31 is 53, when in fact it is 1. After googling this for a while I found a great post from Shawn Steele that explains how to get around the behavior. Quote:
"A simple workaround to consistently get the ISO 8601 week is to realize that consecutive days Monday through Sunday in ISO 8601 weeks all have the same week #. So Monday has the same week # as Thursday. Since Thursday is the critical day for determining when the week starts each year my solution is to add 3 days if the day is Monday, Tuesday or Wednesday. The adjusted days are still in the same week, and use values that GetWeekOfYear and ISO 8601 agree on."
He also posted a great code sample for this.
I ran into this while creating some date-related SQL CLR functions for use in SQL Server 2005. I'll post more about this is a while.
Comments