using System; using System.Collections.Generic; using System.Linq; using System.Text; public class IsoDate1 { public DateTime date { get; private set; } public int Year { get; private set; } public int Week { get; private set; } public IsoDate1(int _year, int _week) { DateTime newYear; int NewYearWeek; int WeeksBetween; int dayOfWeek; Year = _year; Week = _week; if (Week == 1) { DateTime newYearsDay = new DateTime(Year, 1, 1); if (newYearsDay.DayOfWeek == DayOfWeek.Monday) date = newYearsDay; else if (newYearsDay.DayOfWeek == DayOfWeek.Friday || newYearsDay.DayOfWeek == DayOfWeek.Saturday || newYearsDay.DayOfWeek == DayOfWeek.Sunday) { dayOfWeek = (int)newYearsDay.DayOfWeek - 1; if (dayOfWeek == -1) dayOfWeek = 6; date = newYearsDay.AddDays(7 - dayOfWeek); } else { newYear = new DateTime(Year - 1, 12, 31); dayOfWeek = (int)newYear.DayOfWeek - 1; if (dayOfWeek == -1) dayOfWeek = 6; date = newYear.AddDays(-dayOfWeek); } } else { newYear = new DateTime(Year, 12, 31); NewYearWeek = GetNewYearWeek(newYear); if (NewYearWeek == 1) { IsoDate1 lateDecember = new IsoDate1(newYear.AddDays(-7)); WeeksBetween = lateDecember.Week - Week; date = lateDecember.date.AddDays(-7 * WeeksBetween); dayOfWeek = (int)lateDecember.date.DayOfWeek - 1; if (dayOfWeek == -1) dayOfWeek = 6; date = date.AddDays(-dayOfWeek); } else { WeeksBetween = NewYearWeek - Week; date = newYear.AddDays(-7 * WeeksBetween); dayOfWeek = (int)newYear.DayOfWeek - 1; if (dayOfWeek == -1) dayOfWeek = 6; date = date.AddDays(-dayOfWeek); } } } private int GetNewYearWeek(DateTime newYear) { int NewYearWeek; if (DateTime.IsLeapYear(newYear.Year)) { if (newYear.DayOfWeek == DayOfWeek.Saturday || newYear.DayOfWeek == DayOfWeek.Sunday) NewYearWeek = 52; else if (newYear.DayOfWeek == DayOfWeek.Friday || newYear.DayOfWeek == DayOfWeek.Thursday) NewYearWeek = 53; else NewYearWeek = 1; } else { if (newYear.DayOfWeek == DayOfWeek.Friday || newYear.DayOfWeek == DayOfWeek.Saturday || newYear.DayOfWeek == DayOfWeek.Sunday) NewYearWeek = 52; else if (newYear.DayOfWeek == DayOfWeek.Thursday) NewYearWeek = 53; else NewYearWeek = 1; } return NewYearWeek; } public IsoDate1(DateTime _date) { date = _date; DateTime newYear = new DateTime(date.Year, 12, 31); int NewYearWeek; DateTime lastMonday; int lastMondayWeek; int daysBetween; int weeksBetween; int dayOfWeek; NewYearWeek = GetNewYearWeek(newYear); if (date == newYear) { Week = NewYearWeek; if (Week == 1) Year = newYear.Year + 1; else Year = newYear.Year; return; } if (NewYearWeek == 1) { dayOfWeek = (int)newYear.DayOfWeek - 1; if (dayOfWeek == -1) dayOfWeek = 6; lastMonday = newYear.AddDays(-7 - dayOfWeek); lastMondayWeek = 52; } else { dayOfWeek = (int)newYear.DayOfWeek - 1; if (dayOfWeek == -1) dayOfWeek = 6; lastMonday = newYear.AddDays(-dayOfWeek); lastMondayWeek = NewYearWeek; } if (date <= lastMonday) { daysBetween = (lastMonday - date).Days; if (daysBetween == 0) { Week = lastMondayWeek; Year = lastMonday.Year; } else { weeksBetween = (daysBetween - 1) / 7; Week = lastMondayWeek - weeksBetween - 1; if (Week > 0) Year = date.Year; else { IsoDate1 lastNewYear = new IsoDate1(new DateTime(date.Year - 1, 12, 31)); Week = lastNewYear.Week; Year = lastNewYear.Year; } } } else { daysBetween = (date - lastMonday).Days; if (daysBetween < 7) { Week = lastMondayWeek; Year = lastMonday.Year; } else { Week = NewYearWeek; if (Week == 1) Year = newYear.Year + 1; else Year = newYear.Year; } } } }
Saturday, November 10, 2012
Friday, November 9, 2012
ISO 8601 week numbers
ISO 8601 week numbers
To find week number from a given date we can start with finding the week number of the last day of the year and calculate backwards. The last day of the year can be in week 52, 53 or week 1 of the following year. We need 6 rules to calculate week number of a given 31. of december:
If not a leap year, the rules are:
If 31/12 is friday, saturday or sunday then 31/12 is in week 52
If 31/12 is thursday then 31/12 is in week 53
If 31/12 is monday, tuesday or wednesday then 31/12 is in week 1
If leap year, the rules are:
If 31/12 is saturday or sunday then 31/12 is in week 52
If 31/12 is thursday or friday then 31/12 is in week 53
If 31/12 is monday, tuesday or wednesday then 31/12 is in week 1
To find week number from a given date we can start with finding the week number of the last day of the year and calculate backwards. The last day of the year can be in week 52, 53 or week 1 of the following year. We need 6 rules to calculate week number of a given 31. of december:
If not a leap year, the rules are:
If 31/12 is friday, saturday or sunday then 31/12 is in week 52
If 31/12 is thursday then 31/12 is in week 53
If 31/12 is monday, tuesday or wednesday then 31/12 is in week 1
If leap year, the rules are:
If 31/12 is saturday or sunday then 31/12 is in week 52
If 31/12 is thursday or friday then 31/12 is in week 53
If 31/12 is monday, tuesday or wednesday then 31/12 is in week 1
Here are 6 year end examples:
2001 WK Mo Tu We Th Fr Sa Su 48 . . . . . 1 2 49 3 4 5 6 7 8 9 50 10 11 12 13 14 15 16 51 17 18 19 20 21 22 23 52 24 25 26 27 28 29 30 1 31 . . . . . . 2009 WK Mo Tu We Th Fr Sa Su 49 . 1 2 3 4 5 6 50 7 8 9 10 11 12 13 51 14 15 16 17 18 19 20 52 21 22 23 24 25 26 27 53 28 29 30 31 . . . 2010 WK Mo Tu We Th Fr Sa Su 48 . . 1 2 3 4 5 49 6 7 8 9 10 11 12 50 13 14 15 16 17 18 19 51 20 21 22 23 24 25 26 52 27 28 29 30 31 . . 2008 WK Mo Tu We Th Fr Sa Su 49 1 2 3 4 5 6 7 50 8 9 10 11 12 13 14 51 15 16 17 18 19 20 21 52 22 23 24 25 26 27 28 1 29 30 31 . . . . 2004 WK Mo Tu We Th Fr Sa Su 49 . . 1 2 3 4 5 50 6 7 8 9 10 11 12 51 13 14 15 16 17 18 19 52 20 21 22 23 24 25 26 53 27 28 29 30 31 . . 2000 WK Mo Tu We Th Fr Sa Su 48 . . . . 1 2 3 49 4 5 6 7 8 9 10 50 11 12 13 14 15 16 17 51 18 19 20 21 22 23 24 52 25 26 27 28 29 30 31To calculate week number for a given date we need to find the date and week number of the last monday:
If 31/12 is in week 52 or 53 then last monday is 31/12 minus week day number of 31/12 (starting with monday as day 0)
If 31/12 is in week 1 then last monday is 31/12 minus one week minus day number of 31/12. Last monday is always in week 52 in this case.
To calculate week number for a given date:
Find number of days between last monday and given date
Find number of weeks between last monday and given date
Subtract number of weeks from week of last monday
If we end up with week 0, week number is equal to week number of 31/12 for previous year
Code for calculating week number:
public class IsoDate1 { public DateTime date; public int Year; public int Week; public void isoWeek() { DateTime newYear = new DateTime(date.Year, 12, 31); int NewYearWeek; DateTime lastMonday; int lastMondayWeek; int daysBetween; int weeksBetween; int dayOfWeek; if (DateTime.IsLeapYear(date.Year)) { if (newYear.DayOfWeek == DayOfWeek.Saturday || newYear.DayOfWeek == DayOfWeek.Sunday) NewYearWeek = 52; else if (newYear.DayOfWeek == DayOfWeek.Friday || newYear.DayOfWeek == DayOfWeek.Thursday) NewYearWeek = 53; else NewYearWeek = 1; } else { if (newYear.DayOfWeek == DayOfWeek.Friday || newYear.DayOfWeek == DayOfWeek.Saturday || newYear.DayOfWeek == DayOfWeek.Sunday) NewYearWeek = 52; else if (newYear.DayOfWeek == DayOfWeek.Thursday) NewYearWeek = 53; else NewYearWeek = 1; } if (date == newYear) { Week = NewYearWeek; if (Week == 1) Year = newYear.Year + 1; else Year = newYear.Year; return; } if (NewYearWeek == 1) { dayOfWeek = (int)newYear.DayOfWeek - 1; if (dayOfWeek == -1) dayOfWeek = 6; lastMonday = newYear.AddDays(-7 - dayOfWeek); lastMondayWeek = 52; } else { dayOfWeek = (int)newYear.DayOfWeek - 1; if (dayOfWeek == -1) dayOfWeek = 6; lastMonday = newYear.AddDays(-dayOfWeek); lastMondayWeek = NewYearWeek; } if (date <= lastMonday) { daysBetween = (lastMonday - date).Days; if (daysBetween == 0) { Week = lastMondayWeek; Year = lastMonday.Year; } else { weeksBetween = (daysBetween - 1) / 7; Week = lastMondayWeek - weeksBetween - 1; if (Week > 0) Year = date.Year; else { IsoDate1 lastNewYear = new IsoDate1(); lastNewYear.date = new DateTime(date.Year - 1, 12, 31); lastNewYear.isoWeek(); Week = lastNewYear.Week; Year = lastNewYear.Year; } } } else { daysBetween = (date - lastMonday).Days; if (daysBetween < 7) { Week = lastMondayWeek; Year = lastMonday.Year; } else { Week = NewYearWeek; if (Week == 1) Year = newYear.Year + 1; else Year = newYear.Year; } } } }
Subscribe to:
Posts (Atom)