class TestCalendarWesternWeekWithHolidays(BaseCalendarTest): @classmethod def setup_class(cls): print('\n\nTesting regular week, Mo-Fr, WITH holidays, 2010-2013') warnings.filterwarnings('ignore', module='business_calendar') def __init__(self): BaseCalendarTest.__init__(self) self.holidays = [parse(x) for x in global_holidays.split('\n')] self.cal = Calendar(holidays=self.holidays) self.cal.warn_on_holiday_exhaustion = False rr = rruleset() rr.rrule(rrule(DAILY, byweekday=(MO,TU,WE,TH,FR), dtstart=datetime.datetime(2010,1,1))) for h in self.holidays: rr.exdate(h) self.rr = rr self.dates = rr.between(datetime.datetime(2010,1,1), datetime.datetime(2013,12,31), inc=True) def test_workdaycount_with_non_workday_dates(self): print('test_workdaycount_with_non_workday_dates') """ Jan 1st is a holiday, Jan 30th and 31st fall on a weekend """ daycount = 20; for date2 in ['Jan 30, 2010','Jan 31, 2010','Feb 1, 2010']: count = len(list(self.cal.range('2010-01-01', date2))); assert count == daycount for date2 in ['Jan 29, 2010','Jan 30, 2010','Jan 31, 2010']: count = self.cal.workdaycount('Dec 31, 2009', date2); assert count == daycount+1 for date2 in ['Jan 29, 2010','Jan 30, 2010','Jan 31, 2010']: count = self.cal.busdaycount('Dec 31, 2009', date2); assert count == daycount
def test_multiple_business_days(self): business_days = [ datetime.date(2018, 4, 28), datetime.date(2018, 4, 29) ] holidays = [datetime.date(2018, 4, 23)] cal = Calendar(busdays=business_days, holidays=holidays) assert cal.busdaycount( datetime.date(2018, 4, 27), datetime.date(2018, 4, 29), ) == 2 assert cal.busdaycount( datetime.date(2018, 4, 22), datetime.date(2018, 4, 29), ) == 6
def trade_days(cls, start, end): """ 给定两个时间,计算这个时间段内有多少个交易日 :param start: :param end: :return: """ try: cal = Calendar(workdays=cls.workdays, holidays=cls.holidays) days = cal.busdaycount(start, end) return days except Exception as e: ExceptionInfo(e) return 0
def test_busdaycount(self): busdays = [datetime.date(2018, 4, 28)] cal = Calendar(busdays=busdays) nbusdays = cal.busdaycount( datetime.date(2018, 4, 27), datetime.date(2018, 4, 30), ) assert 2 == nbusdays nbusdays = cal.busdaycount( datetime.date(2018, 4, 27), datetime.date(2018, 4, 28), ) assert 1 == nbusdays nbusdays = cal.busdaycount( datetime.date(2018, 4, 30), datetime.date(2018, 4, 27), ) assert -2 == nbusdays # time interval after business day nbusdays = cal.busdaycount( datetime.date(2018, 4, 30), datetime.date(2018, 5, 4), ) assert 4 == nbusdays nbusdays = cal.busdaycount( datetime.date(2018, 5, 4), datetime.date(2018, 4, 30), ) assert -4 == nbusdays # time interval before business day nbusdays = cal.busdaycount( datetime.date(2018, 4, 2), datetime.date(2018, 4, 6), ) assert 4 == nbusdays nbusdays = cal.busdaycount( datetime.date(2018, 4, 6), datetime.date(2018, 4, 2), ) assert -4 == nbusdays
def export_report(request): """ Exporting the monthly attendance report has a PDF format. """ try: if request.method == "GET": cal = Calendar() now = datetime.now() this_year = now.year this_month = now.month date1 = datetime(this_year, this_month, 1) date2 = cal.addbusdays(date1, monthrange(this_year, this_month)[1]) nodw = cal.busdaycount(date1, date2) all_days = range(1, nodw + 1) dict = {} for u in User.objects.all(): c = Attendance.objects.filter(month=now.strftime("%b"), username=u, approved_or_not=True) l = 0 for i in c: l = l + calculate_days(i.from_date, i.to_date) print l dict[u] = nodw - l context = { 'dict': dict, 'this_month': now.strftime("%B"), 'this_year': this_year, 'nodw': nodw, } pdf_filename = '{0}.pdf'.format(now.strftime("%B")) temp_path = os.path.join(settings.PDF_ROOT, pdf_filename) write_pdf('Reports/report.html', context, temp_path) with open(temp_path, 'rb') as fh: response = HttpResponse(fh.read(), content_type="application/pdf") response[ 'Content-Disposition'] = 'inline; filename=' + os.path.basename( temp_path) return response except Exception, e: return HttpResponseRedirect('/error/')
def tglxqsfgz(): print('\n\n\n 停工留薪期实发工资计算器\n\n') # 调用business_calendar模块计算除去周末的工作日天数 year = int(input('请输入停工留薪期开始年份(数字):\n')) month = int(input('请输入停工留薪期开始月份(数字):\n')) day1 = int(input('请输入停工留薪期开始日期(数字):\n')) day2 = int(input('请输入停工留薪期结束日期(数字):\n')) date1 = datetime.datetime(year, month, day1) date2 = datetime.datetime(year, month, day2) cal = Calendar() print('在{date1}至{date2}期间,除去周末的工作日天数为{busdaycount}天\n'.format( date1=date1, date2=date2, busdaycount=cal.busdaycount(date1, date2) + 1)) # 计算实发工资 tinggongliuxinqitianshu = int( input('请根据以上天数,核实在此期间是否有法定节日,然后输入停工留薪期天数(数字):\n')) # 其实可以直接用business_calendar模块排除法定节假日和周末,但是需要事先录入法定节假日 gongzhi = float(input('请输入该月实发工资(数字):\n')) gongzuorigongzhi = tinggongliuxinqitianshu * (gongzhi / 21.75) print('\n停工留薪期实发工资(该月工作日部分):{jisuanjieguo}元'.format( jisuanjieguo=float('%.2f' % gongzuorigongzhi)))
date2 = datetime.datetime(year2, month2, day2) cal0 = Calendar() cal1 = Calendar(workdays=[SA, SU]) cal2 = Calendar(workdays=[MO, TU, WE, TH, FR], holidays=["2010-01-01", "2020-01-01"] + Holiday + Timeoff_list) cal3 = Calendar(workdays=[MO, TU, WE, TH, FR, SA, SU], holidays=["2010-01-01", "2020-01-01"] + Holiday) cal4 = Calendar(workdays=[MO, TU, WE, TH, FR, SA, SU], holidays=["2010-01-01", "2020-01-01"] + Timeoff_list) bsday0 = int(str(date2 - date1).split(" ")[0]) - 1 # 打卡间隔天数 date2 = date2 + datetime.timedelta(days=-1) # 减去一天 bsday1 = cal1.busdaycount(date1, date2) # 打卡间隔天数的周六日天数 bsday2 = cal2.busdaycount(date1, date2) # 打卡间隔存在的休假日天数 bsday3 = bsday0 - cal3.busdaycount(date1, date2) # 打卡间隔存在的节假日天数 bsday4 = bsday0 - cal4.busdaycount(date1, date2) # 打卡间隔存在的请假日天数 _a = bsday0 - bsday1 _b = bsday1 - bsday2 Holiday_Count0.append(bsday0) Holiday_Count1.append(bsday1) Holiday_Count2.append(bsday2) Holiday_Count3.append(bsday3) Holiday_Count4.append(bsday4) a.append(_a) b.append(_b)
from business_calendar import Calendar, MO, TU, WE, TH, FR import datetime date1 = datetime.datetime(2013,1,10) # normal calendar, no holidays cal = Calendar() date2 = cal.addbusdays(date1, 25) print('%s days between %s and %s' % \ (cal.busdaycount(date1, date2), date1, date2)) # don't work on Fridays? no problem! cal = Calendar(workdays=[MO,TU,WE,TH]) date2 = cal.addbusdays(date1, 25) print('%s days between %s and %s' % \ (cal.busdaycount(date1, date2), date1, date2)) # holiday? no problem! cal = Calendar(workdays=[MO,TU,WE,TH], holidays=['2013-01-17']) date2 = cal.addbusdays(date1, 25) print('%s days between %s and %s' % \ (cal.busdaycount(date1, date2), date1, date2))
class TestCalendarWithSpecialWorkdays: @classmethod def setup_class(cls): print('\n\nTesting Calendar with special workdays') def __init__(self): self.holidays = dict( hworkday=datetime.date(2018, 1, 1), hworkday_two=datetime.date(2018, 1, 2), hweekend_day=datetime.date(2018, 1, 7), ) self.specdays = dict( sholiday=datetime.date(2018, 1, 2), sworkday=datetime.date(2018, 1, 5), ssaturday=datetime.date(2018, 1, 13), ) self.busdays = [datetime.date(2018, 1, 14)] self.cal = Calendar( specdays=self.specdays.values(), holidays=self.holidays.values(), busdays=self.busdays, ) self.cal.warn_on_holiday_exhaustion = False self.workdays = [ datetime.date(2018, 1, i) for i in range(1, 15) if self.cal.isworkday(datetime.date(2018, 1, i)) and not self.cal.isholiday(datetime.date(2018, 1, i)) ] def test_isspecial(self): print('test_special') specdays = self.specdays.values() for sday in specdays: assert self.cal.isspecday(sday) for hday in self.holidays.values(): if hday not in specdays: assert not self.cal.isspecday(hday) def test_special_days_are_also_busdays(self): print('test_special_days_are_also_busdays') for name, day in self.specdays.items(): if not 'workday' in name: assert self.cal.isbusday(day) def test_busdaycount(self): print('test_busdaycount') nbusdays = self.cal.busdaycount( datetime.date(2018, 1, 1), datetime.date(2018, 1, 15), ) assert nbusdays == 12 def test_range(self): print('test_range') busdays = list( self.cal.range( datetime.date(2018, 1, 1), datetime.date(2018, 1, 15), )) check_busdays = [ self.specdays['sholiday'], self.specdays['ssaturday'], self.busdays[0] ] check_busdays.extend(self.workdays) assert sorted(check_busdays) == busdays