Tema: Re: Nedarbo dienu nustatymas
Autorius: Andrius F
Data: 2009-09-18 16:49:16
3iam sharpui.
Jokių garantijų : )
nedarbo dienų sąrašas iš day.lt   velykos skaičiuojamos pagal algoritmą iš http://www.geekpedia.com/code68_Find-Easter-Sunday-of-any-year.html
motinos diena visada sekmadienį, taip kad atskirai neskaičiuojama.
gal dar ka praleidau?


hope this helps.

/// <summary>
/// Date Extensions
/// </summary>
public static class DateTimeExtensions
{
private static List<DateTime> _offDays = new List<DateTime>();
static DateTimeExtensions()
{
_offDays.AddRange(new DateTime[] { 
new DateTime(1,01,01),
new DateTime(1,02,16),
new DateTime(1,03,11),
new DateTime(1,05,01),
new DateTime(1,05,01),
new DateTime(1,06,24),
new DateTime(1,07,06),
new DateTime(1,08,15),
new DateTime(1,11,01),
new DateTime(1,12,25),
new DateTime(1,12,26)
});
}
/// <summary>
/// Gets easter sunday of specified year
/// </summary>
/// <param name="date"></param>
/// <returns>Date of the easter in specified year</returns>
public static DateTime GetEaster(this DateTime date)
{
int YearToCheck = date.Year;
int Y = YearToCheck;
int a = Y % 19;
int b = Y / 100;
int c = Y % 100;
int d = b / 4;
int e = b % 4;
int f = (b + 8) / 25;
int g = (b - f + 1) / 3;
int h = (19 * a + b - d - g + 15) % 30;
int i = c / 4;
int k = c % 4;
int L = (32 + 2 * e + 2 * i - h - k) % 7;
int m = (a + 11 * h + 22 * L) / 451;
int Month = (h + L - 7 * m + 114) / 31;
int Day = ((h + L - 7 * m + 114) % 31) + 1;
DateTime dtEasterSunday = new DateTime(YearToCheck, Month, Day);
Debug.Assert(dtEasterSunday.DayOfWeek == DayOfWeek.Sunday);
return dtEasterSunday;
}
/// <summary>
/// Get value indicating weather specified day is a day off
/// </summary>
/// <param name="date"></param>
/// <returns>true if a day is day off, otherwise false</returns>
public static bool IsDayOff(this DateTime date)
{
return (date.DayOfWeek == DayOfWeek.Sunday ||
date.DayOfWeek == DayOfWeek.Saturday ||
(date.DayOfWeek == DayOfWeek.Monday && date.Date.Equals(date.GetEaster().AddDays(1))) || // check only mondays for second easter day.
_offDays.Contains(date.Date, DayComparer.Instance));
}
private class DayComparer : IEqualityComparer<DateTime>
{
private static DayComparer _comparer = new DayComparer();
public static DayComparer Instance
{
get { return _comparer; }
}
#region IEqualityComparer<DateTime> Members
public bool Equals(DateTime x, DateTime y)
{
return x.DayOfYear == y.DayOfYear - Convert.ToInt32(DateTime.IsLeapYear(y.Year));
}
public int GetHashCode(DateTime obj)
{
throw new NotImplementedException();
}
#endregion
}
}

-------------testai 

//Easter test
DateTime d1 = new DateTime(2000, 04, 12); 
DateTime d2 = new DateTime(2009, 04, 13);
Console.WriteLine("Date {0} is {1} th day of the year and is {2}", d1, d1.DayOfYear, d1.IsDayOff() ? "Day Off" : "Workday");
Console.WriteLine("Date {0} is {1} th day of the year and is {2}", d2, d2.DayOfYear, d2.IsDayOff() ? "Day Off" : "Workday");
d1 = new DateTime(2010, 04, 04); 
d2 = new DateTime(2010, 04, 05);
Console.WriteLine("Date {0} is {1} th day of the year and is {2}", d1, d1.DayOfYear, d1.IsDayOff() ? "Day Off" : "Workday");
Console.WriteLine("Date {0} is {1} th day of the year and is {2}", d2, d2.DayOfYear, d2.IsDayOff() ? "Day Off" : "Workday");
//leap year test
d1 = new DateTime(2009, 12, 25);
d2 = new DateTime(2000, 12, 25); //leap year
Console.WriteLine("Date {0} is {1} th day of the year and is {2}", d1, d1.DayOfYear, d1.IsDayOff() ? "Day Off" : "Workday");
Console.WriteLine("Date {0} is {1} th day of the year and is {2}", d2, d2.DayOfYear, d2.IsDayOff() ? "Day Off" : "Workday");

d1 = DateTime.Now;
d2 = DateTime.Now.AddDays(1);
Console.WriteLine("Date {0} is {1} th day of the year and is {2}", d1, d1.DayOfYear, d1.IsDayOff() ? "Day Off" : "Workday");
Console.WriteLine("Date {0} is {1} th day of the year and is {2}", d2, d2.DayOfYear, d2.IsDayOff() ? "Day Off" : "Workday");



"Hassan" <turbon_@_googlomailas.com> wrote in message news:h8vll3$2tv$1@trimpas.omnitel.net...
> Sveiki,
> Yra poreikis nustatyti ar duotoji diena yra darbo, ar ne. Nedarbo dienu aibe sudaro savaitgaliai (easy) + valstybines sventes. Valstybines sventes dvieju rusiu - vienos turi fiksuotas datas (vadinasi tiesiog reikia susidaryti aibe situ dienu), kitu datos skaiciuojasi pagal tam tikrus algoritmus. 
> Kaip ir nera kosmine uzduotis viska pasidaryt paciam, bet kadangi neabejoju, kad ne man pirma mir ne man paskutiniam kyla toks poreikis - tai gal kas jau turit pasidares biblioteka tokiem reikalam? Butu gerai .NET ar kuonors, ka supranta .NET, bet tinka ir kitos kalbos - nusiportinciau. 
> 
> Kalba eina apie nedarbo dienas lietuvoje.
>