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
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment