Beispiel #1
0
 def is_trade_day(cls, date, holidays=None):
     """
     判断给定的这个时间是否是交易日(以日记)
     :param date: 需要判断的时间
     :return:
     """
     try:
         holidays = cls.holidays if holidays is None else holidays
         cal = Calendar(workdays=cls.workdays, holidays=holidays)
         flag = cal.isbusday(dt.datetime(date.year, date.month, date.day))
         return flag
     except Exception as e:
         ExceptionInfo(e)
         return False
Beispiel #2
0
def project_stat():
    db = fenxi_mysql.dbmysql()
    objectid = ''
    ####今天
    date = datetime.datetime.now().strftime("%Y-%m-%d")
    ##昨天
    date1 = datetime.datetime.now() + datetime.timedelta(days=-1)
    date1 = date1.strftime("%Y-%m-%d")
    ##前天
    date2 = datetime.datetime.now() + datetime.timedelta(days=-2)
    date2 = date2.strftime("%Y-%m-%d")
    ##今天结束日
    date00 = datetime.datetime.now() + datetime.timedelta(days=1)
    date00 = date00.strftime("%Y-%m-%d")
    ##昨天结束日
    date11 = datetime.datetime.now().strftime("%Y-%m-%d")
    ##前天结束日
    date22 = datetime.datetime.now() + datetime.timedelta(days=-1)
    date22 = date22.strftime("%Y-%m-%d")
    #判断是否为工作日,工作日返回true,非工作日返回false
    cal = Calendar()
    if cal.isbusday(date) == True:
        if cal.isbusday(date1) == False or cal.isbusday(date2) == False:
            d1 = 0
            d2 = 0
            while cal.isbusday(date1) == False:
                d1 = d1 + 1
                days1 = -1 - d1
                date1 = datetime.datetime.now() + datetime.timedelta(
                    days=days1)
                date11 = date1 + datetime.timedelta(days=1)
                date1 = date1.strftime("%Y-%m-%d")
                date11 = date11.strftime("%Y-%m-%d")
            while cal.isbusday(date2) == False:
                d2 = d2 + 1
                days2 = -2 - d2
                date2 = datetime.datetime.now() + datetime.timedelta(
                    days=days2)
                date22 = date2 + datetime.timedelta(days=1)
                date2 = date2.strftime("%Y-%m-%d")
                date22 = date22.strftime("%Y-%m-%d")
                if date1 == date2:
                    d2 = d2 + 1
                    days2 = -2 - d2
                    date2 = datetime.datetime.now() + datetime.timedelta(
                        days=days2)
                    date22 = date2 + datetime.timedelta(days=1)
                    date2 = date2.strftime("%Y-%m-%d")
                    date22 = date22.strftime("%Y-%m-%d")

        print date, date1, date2
        print date00, date11, date22

        ##############################################################################
        objectid = db.object_id_sel()
        objectid_cn = len(objectid)
        i = 1
        #        status=1
        for i in range(objectid_cn):
            obid = objectid[i][0]
            #今天bug数
            td = int(db.bug_objectid_sel(obid, date, date00)[0])
            #昨天bug数
            zt = int(db.bug_objectid_sel(obid, date1, date11)[0])
            #前天bug数
            qt = int(db.bug_objectid_sel(obid, date2, date22)[0])
            pro_stat = db.project_stat_sel(obid, '0')
            status = int(pro_stat[0])
            stardate = pro_stat[1]
            ##今天有bug
            if td > 0:
                print obid
                print td, zt, qt
                #项目进行中
                if status == 1:
                    if zt == 0 and qt == 0:
                        total = int(
                            db.bug_objectid_sel(obid, stardate, date2)[0])
                        jk_total = int(
                            db.jenkins_source_sel(obid, stardate, date2)[0])
                        db.project_stat_update(total, jk_total, date2, '1',
                                               obid, '0')
                        print "如果项目进行中,今天无bug,更改项目状态为:结束"

                #项目结束
                if status == 0:
                    bugcn = td - zt
                    if bugcn >= 1:
                        obname = db.object_name_sel(obid)
                        proname = obname[0] + str(date)
                        db.project_stat_save(proname, obid, date, '0')
            #            print obname[0],bugcn
            #今天无bug
            else:
                #项目进行中
                if status == 1:
                    if zt == 0:
                        total = int(
                            db.bug_objectid_sel(obid, stardate, date1)[0])
                        jk_total = int(
                            db.jenkins_source_sel(obid, stardate, date1)[0])
                        db.project_stat_update(total, jk_total, date2, '1',
                                               obid, '0')
                        print "如果项目进行中,今天无bug,更改项目状态为:结束"
                print obid
                print td, zt, qt
Beispiel #3
0
#    return 1
# else:
#    return 0
#
#
#print workDay()
#print (datetime.datetime.now()+datetime.timedelta(days=3)).weekday()

date = datetime.datetime.now().strftime("%Y-%m-%d")
date1 = datetime.datetime.now() + datetime.timedelta(days=-1)
date1 = date1.strftime("%Y-%m-%d")
date2 = datetime.datetime.now() + datetime.timedelta(days=-2)
date2 = date2.strftime("%Y-%m-%d")
#判断是否为工作日,工作日返回true,非工作日返回false
cal = Calendar()
if cal.isbusday(date) == True:
    if cal.isbusday(date2) == False:
        d1 = 0
        d2 = 0
        while cal.isbusday(date1) == False:
            d1 = d1 + 1
            days1 = -1 - d1
            date1 = datetime.datetime.now() + datetime.timedelta(days=days1)
            date1 = date1.strftime("%Y-%m-%d")
        while cal.isbusday(date2) == False:
            d2 = d2 + 1
            days2 = -2 - d2
            date2 = datetime.datetime.now() + datetime.timedelta(days=days2)
            date2 = date2.strftime("%Y-%m-%d")
            if date1 == date2:
                d2 = d2 + 1
Beispiel #4
0
def preprocessor(input_file, t, output):
    # (tollgate_id,direction)五个方向 0-(1,0) 1-(1,1) 2-(2,0) 3-(3,0) 4-(3,1)
    # 三维特征  vehicle_mode{0-7}, has_etc{0,1}, vehicle_type{0,1,unknown}
    corr_pair = [[1, 0], [1, 1], [2, 0], [3, 0], [3, 1]]
    time_point = dict()  # 三个维度的各类别
    volume_per_slot = dict()  # 车流量
    working_day = dict()  # 是否工作日
    sample_cnt = 0
    time_interval = t
    output_file = output
    time_slot_cnt = 0
    with open(input_file, 'r') as f:
        f.readline()  # ignore the title
        # 输入前先将数据集递增排序
        for line in f:
            sample = line.split(',')
            time_stamp = datetime.strptime(sample[0], '%d/%m/%Y %H:%M')
            if sample_cnt == 0:
                mins = time_stamp.minute % 10
                if mins == 0:
                    begin_time = time_stamp
                else:
                    begin_time = time_stamp - timedelta(minutes=mins)
            minutes = (time_stamp - begin_time).seconds / 60
            if minutes >= time_interval or sample_cnt == 0:
                # 每隔time_interval 初始化一次
                mins = time_stamp.minute % 10
                if mins == 0:
                    begin_time = time_stamp
                else:
                    begin_time = time_stamp - timedelta(minutes=mins)
                index = datetime.strftime(begin_time, '%d/%m/%Y %H:%M')
                volume_per_slot[index] = [0 for _ in range(5)]
                time_point[index] = [dict() for _ in range(5)]
                for i in range(5):
                    time_point[index][i]['mode'] = [0 for _ in range(8)]
                    time_point[index][i]['etc'] = [0 for _ in range(2)]
                    time_point[index][i]['type'] = [0 for _ in range(3)]
                time_slot_cnt += 1
            for i in range(5):
                if str(corr_pair[i][0]) in sample[1] and str(corr_pair[i][1]) in sample[2]:
                    volume_per_slot[index][i] += 1
                    time_point[index][i]['mode'][int(sample[3])] += 1
                    time_point[index][i]['etc'][int(sample[4])] += 1
                    if '1' in sample[5] or '0' in sample[5]:
                        time_point[index][i]['type'][int(sample[5])] += 1
                    else:
                        time_point[index][i]['type'][2] += 1
                    break
            sample_cnt += 1

    with open(str(time_interval)+u'分钟'+output_file, 'w') as f:
        title_str = '"time_window","gate_dir",'
        for i in range(8):
            title_str += 'vehicle_model[' + str(i) + '],'
        for i in range(2):
            title_str += 'has_etc[' + str(i) + '],'
        for i in range(2):
            title_str += 'vehicle_type[' + str(i) + '],'
        title_str += 'no_type,total_volume,working_day'
        f.write(title_str + '\n')
        cal = Calendar()
        for k in volume_per_slot.keys():
            for j in range(5):
                time1 = datetime.strptime(k, '%d/%m/%Y %H:%M')
                time_formalize1 = datetime.strftime(time1, "%Y-%m-%d %H:%M:%S")
                time2 = time1 + timedelta(minutes=time_interval)
                time_formalize2 = datetime.strftime(time2, "%Y-%m-%d %H:%M:%S")
                f.write('"[%s,%s)",' % (time_formalize1, time_formalize2))
                # gate_dir
                f.write('"%d",' % j)
                for m in range(8):
                    f.write('"%d",' % time_point[k][j]['mode'][m])
                for e in range(2):
                    f.write('"%d",' % time_point[k][j]['etc'][e])
                for t in range(2):
                    f.write('"%d",' % time_point[k][j]['type'][t])
                f.write('"%d",' % time_point[k][j]['type'][2])
                f.write('"%d",' % volume_per_slot[k][j])
                if cal.isbusday(time1):
                    working_day[k] = 1
                else:
                    working_day[k] = 0
                f.write('"%d"\n' % working_day[k])
from business_calendar import Calendar, MO, TU, WE, TH, FR
import datetime
import calendar

year = 2018
month = 12
businessDayCount = 0
holidayDayCount = 0

monthRange = calendar.monthrange(year,month)
cal = Calendar()

for i in range(1,monthRange[1]+1):
	date1 = datetime.datetime(year,month,i) 
	if cal.isbusday(date1):
		# print('%s   : %s' % (i, "Business Day"))
		businessDayCount +=1
	else:
		# print('%s   : %s' % (i, "Holiday Day"))
		holidayDayCount +=1

print('Year        : %s' % year)
print('Month       : %s' % month)
print('Business Day: %s days' % (businessDayCount))
print('Holiday Day : %s days' % (holidayDayCount))
print('Total Day   : %s days' % (monthRange[1]))
 def test_holidays_has_priority(self):
     date = datetime.date(2018, 4, 28)
     busdays = [date]
     holidays = [date]
     cal = Calendar(holidays=holidays, busdays=busdays)
     assert not cal.isbusday(date)
Beispiel #7
0
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