আমি এই পোস্টটি পুনরুত্থিত করছি কারণ আজ আমাকে কেবল শনিবার এবং রবিবার সপ্তাহের দিনগুলিই নয়, ছুটির দিনগুলিও বাদ দেওয়ার উপায় খুঁজে পেতে হয়েছিল । আরও নির্দিষ্টভাবে, আমার সম্ভাব্য ছুটির বিভিন্ন সেট হ্যান্ডেল করা দরকার:
- দেশ-আক্রমণকারী ছুটি (অন্তত পশ্চিমা দেশগুলির জন্য - যেমন জানুয়ারী, ২০১১)
- গণনা করা ছুটি (যেমন ইস্টার এবং ইস্টার সোমবার)।
- দেশ-নির্দিষ্ট ছুটি (যেমন ইতালিয়ান মুক্তির দিন বা মার্কিন যুক্তরাষ্ট্রের আইডি 4)
- শহর-নির্দিষ্ট ছুটি (যেমন রোম সেন্ট প্যাট্রন দিবস)।
- অন্য কোনও কাস্টম তৈরি ছুটির দিন (যেমন "আগামীকাল আমাদের অফিস বন্ধ হয়ে যাবে")।
অবশেষে, আমি নিম্নলিখিত সহায়ক / এক্সটেনশন শ্রেণীর সেট নিয়ে বেরিয়ে এসেছি: যদিও তারা অস্পষ্টভাবে মার্জিত নয়, তারা অপ্রয়োজনীয় লুপগুলির ব্যাপক ব্যবহার করে, তবে তারা আমার সমস্যাগুলি ভাল করার জন্য যথেষ্ট পরিমাণে শালীন। আমি এই পোস্টে এখানে পুরো উত্স কোডটি ফেলে দিচ্ছি, আশা করি এটি অন্য কারও পক্ষেও কার্যকর হবে।
সোর্স কোড
public static class DateTimeExtensions
{
public static int Years(DateTime dt1,DateTime dt2)
{
return Months(dt1,dt2)/12;
}
public static int Years2(DateTime start, DateTime end)
{
return (end.Year - start.Year - 1) +
(((end.Month > start.Month) ||
((end.Month == start.Month) && (end.Day >= start.Day))) ? 1 : 0);
}
public static int Months(DateTime dt1,DateTime dt2)
{
if (dt2<dt1)
{
DateTime dt0=dt1;
dt1=dt2;
dt2=dt0;
}
dt2=dt2.AddDays(-(dt1.Day-1));
return (dt2.Year-dt1.Year)*12+(dt2.Month-dt1.Month);
}
public static DateTime Max(DateTime dt1,DateTime dt2)
{
return (dt2>dt1?dt2:dt1);
}
public static DateTime Min(DateTime dt1,DateTime dt2)
{
return (dt2<dt1?dt2:dt1);
}
public static DateTime AddBusinessDays(
this DateTime current,
int days,
IEnumerable<DateTime> holidays = null)
{
var sign = Math.Sign(days);
var unsignedDays = Math.Abs(days);
for (var i = 0; i < unsignedDays; i++)
{
do
{
current = current.AddDays(sign);
}
while (current.DayOfWeek == DayOfWeek.Saturday
|| current.DayOfWeek == DayOfWeek.Sunday
|| (holidays != null && holidays.Contains(current.Date))
);
}
return current;
}
public static DateTime SubtractBusinessDays(
this DateTime current,
int days,
IEnumerable<DateTime> holidays)
{
return AddBusinessDays(current, -days, holidays);
}
public static int GetBusinessDays(
this DateTime startDate,
DateTime endDate,
IEnumerable<DateTime> holidays)
{
if (startDate > endDate)
throw new NotSupportedException("ERROR: [startDate] cannot be greater than [endDate].");
int cnt = 0;
for (var current = startDate; current < endDate; current = current.AddDays(1))
{
if (current.DayOfWeek == DayOfWeek.Saturday
|| current.DayOfWeek == DayOfWeek.Sunday
|| (holidays != null && holidays.Contains(current.Date))
)
{
}
else cnt++;
}
return cnt;
}
public static DateTime GetEasterSunday(int year)
{
int day = 0;
int month = 0;
int g = year % 19;
int c = year / 100;
int h = (c - (int)(c / 4) - (int)((8 * c + 13) / 25) + 19 * g + 15) % 30;
int i = h - (int)(h / 28) * (1 - (int)(h / 28) * (int)(29 / (h + 1)) * (int)((21 - g) / 11));
day = i - ((year + (int)(year / 4) + i + 2 - c + (int)(c / 4)) % 7) + 28;
month = 3;
if (day > 31)
{
month++;
day -= 31;
}
return new DateTime(year, month, day);
}
public static IEnumerable<DateTime> GetHolidays(IEnumerable<int> years, string countryCode = null, string cityName = null)
{
var lst = new List<DateTime>();
foreach (var year in years.Distinct())
{
lst.AddRange(new[] {
new DateTime(year, 1, 1),
new DateTime(year, 1, 6),
new DateTime(year, 5, 1),
new DateTime(year, 8, 15),
new DateTime(year, 11, 1),
new DateTime(year, 12, 8),
new DateTime(year, 12, 25),
new DateTime(year, 12, 26)
});
var easterDate = GetEasterSunday(year);
lst.Add(easterDate);
lst.Add(easterDate.AddDays(1));
if (!String.IsNullOrEmpty(countryCode))
{
switch (countryCode.ToUpper())
{
case "IT":
lst.Add(new DateTime(year, 4, 25));
break;
case "US":
lst.Add(new DateTime(year, 7, 4));
break;
case default:
break;
}
}
if (!String.IsNullOrEmpty(cityName))
{
switch (cityName)
{
case "Rome":
case "Roma":
lst.Add(new DateTime(year, 6, 29));
break;
case "Milano":
case "Milan":
lst.Add(new DateTime(year, 12, 7));
break;
default:
break;
}
}
}
return lst;
}
}
ব্যবহারের তথ্য
কোডটি বেশ স্ব-বর্ণনামূলক, তবে আপনি কীভাবে এটি ব্যবহার করতে পারেন তা বোঝাতে এখানে কয়েকটি উদাহরণ দেওয়া হয়েছে।
10 টি ব্যবসায়িক দিন যুক্ত করুন (কেবল শনিবার এবং রবিবারের সপ্তাহের দিনগুলি এড়িয়ে যাওয়া)
var dtResult = DateTimeUtil.AddBusinessDays(srcDate, 10);
10 ব্যবসায়িক দিন যুক্ত করুন (শনিবার, রবিবার এবং 2019 এর জন্য সমস্ত দেশ-আক্রমণকারী ছুটির দিন বাদ দেওয়া)
var dtResult = DateTimeUtil.AddBusinessDays(srcDate, 10, GetHolidays(2019));
10 টি ব্যবসায়িক দিন যুক্ত করুন (শনিবার, রবিবার এবং 2019 এর সমস্ত ইতালীয় ছুটির দিন বাদ দেওয়া)
var dtResult = DateTimeUtil.AddBusinessDays(srcDate, 10, GetHolidays(2019, "IT"));
10 টি ব্যবসায়িক দিন যুক্ত করুন (শনিবার, রবিবার, সমস্ত ইতালীয় ছুটি এবং 2019 সালের রোম-নির্দিষ্ট ছুটিগুলি এড়িয়ে যাওয়া) যুক্ত করুন
var dtResult = DateTimeUtil.AddBusinessDays(srcDate, 10, GetHolidays(2019, "IT", "Rome"));
উপরের ফাংশন এবং কোডের উদাহরণগুলি আমার ব্লগের এই পোস্টে আরও ব্যাখ্যা করা হয়েছে ।