def test_same_code_as_readme(self): import datetime # Check if 2018-04-30 is holiday in China from chinese_calendar import is_workday, is_holiday april_last = datetime.date(2018, 4, 30) self.assertFalse(is_workday(april_last)) self.assertTrue(is_holiday(april_last)) # or check and get the holiday name import chinese_calendar as calendar # with different import style on_holiday, holiday_name = calendar.get_holiday_detail(april_last) self.assertTrue(on_holiday) self.assertEqual(calendar.Holiday.labour_day.value, holiday_name) # even check if a holiday is in lieu import chinese_calendar self.assertFalse(chinese_calendar.is_in_lieu(datetime.date(2006, 2, 1))) self.assertTrue(chinese_calendar.is_in_lieu(datetime.date(2006, 2, 2)))
def test_same_code_as_readme_english(self): import datetime # Check if 2018-04-30 is holiday in China from chinese_calendar import is_holiday, is_workday april_last = datetime.date(2018, 4, 30) assert is_workday(april_last) is False assert is_holiday(april_last) is True # or check and get the holiday name import chinese_calendar as calendar # 也可以这样 import on_holiday, holiday_name = calendar.get_holiday_detail(april_last) assert on_holiday is True assert holiday_name == calendar.Holiday.labour_day.value # even check if a holiday is in lieu import chinese_calendar assert chinese_calendar.is_in_lieu(datetime.date(2006, 2, 1)) is False assert chinese_calendar.is_in_lieu(datetime.date(2006, 2, 2)) is True
def test_same_code_as_readme_chinese(self): import datetime # 判断 2018年4月30号 是不是节假日 from chinese_calendar import is_holiday, is_workday april_last = datetime.date(2018, 4, 30) assert is_workday(april_last) is False assert is_holiday(april_last) is True # 或者在判断的同时,获取节日名 import chinese_calendar as calendar # 也可以这样 import on_holiday, holiday_name = calendar.get_holiday_detail(april_last) assert on_holiday is True assert holiday_name == calendar.Holiday.labour_day.value # 还能判断法定节假日是不是调休 import chinese_calendar assert chinese_calendar.is_in_lieu(datetime.date(2006, 2, 1)) is False assert chinese_calendar.is_in_lieu(datetime.date(2006, 2, 2)) is True
def check_trading_period(): """""" now = datetime.now() current_time = now.time() trading = False if (now.weekday() < 5 and not cnc.is_in_lieu(now) and (time(8, 59) <= current_time <= time(10, 15) or time(10, 29) <= current_time <= time(11, 30) or time(13, 29) <= current_time <= time(15, 0) or time(20, 59) <= current_time <= time(23, 0)) ): trading = True return trading
def build_date_fea_defult(df, date_col): """ 构建日期相关特征. 为一个DataFrame的日期列构造日期相关特征, 构建的特征有: ['year', 'month', 'day','dayofyear', 'dayofweek', 'daysinmonth', 'is_leap_year', 'is_in_lieu', 'is_holiday', 'is_workday']. Args: df: 输入的DataFrame. date_col: 日期列名,为一个字符串,需存在于df中,对应列可以是字符串格式或者日期格式. Returns: df: 构建完成的新的DataFrame. """ year, month, day, dayofyear, \ dayofweek, daysinmonth, is_leap_year, \ is_in_lieu, is_holiday, is_workday = create_empty_list(10) for t in df[date_col]: t = pd.to_datetime(t) year.append(t.year) month.append(t.month) day.append(t.day) dayofyear.append(t.dayofyear) dayofweek.append(t.dayofweek) daysinmonth.append(t.daysinmonth) is_leap_year.append(int(t.is_leap_year)) is_in_lieu.append(int(cc.is_in_lieu(t))) is_holiday.append(int(cc.is_holiday(t))) is_workday.append(int(cc.is_workday(t))) df['year'] = year df['month'] = month df['day'] = day df['dayofyear'] = dayofyear df['dayofweek'] = dayofweek df['daysinmonth'] = daysinmonth df['is_leap_year'] = is_leap_year df['is_in_lieu'] = is_in_lieu df['is_holiday'] = is_holiday df['is_workday'] = is_workday return df
# encoding: utf-8 """ @author : zhirui zhou @contact: [email protected] @time : 2019/11/29 10:37 """ from dtsp.dataset import example_data, Seq2SeqDataSet, walk_forward_split from dtsp.models import Seq2Seq from torch.utils.data import Subset, DataLoader from pathlib import Path import shutil import chinese_calendar as calendar import datetime as dt calendar.is_in_lieu(dt.datetime(2019, 9, 27)) def test_seq2seq(): hp = { 'path': Path('.').resolve() / 'logs', 'target_size': 20, 'rnn_type': 'LSTM', 'dropout': 0.1, 'hidden_size': 128, 'teacher_forcing_rate': 0.5, 'n_head': 2, 'use_attn': True, 'trans_hidden_size': 4, 'trans_continuous_var': None, 'trans_category_var': [(13, 2)], 'trans_bidirectional': True, 'trans_rnn_type': 'LSTM',
# [A04付费旅客] import pandas as pd from datetime import datetime from chinese_calendar import is_workday, is_holiday, is_in_lieu #是否工作日,休息日,法定节假日 datafile = "D:/2021软件服务外包比赛/result/" + 'train_20210120_date_split.csv' # 航空原始数据,第一行为属性标签 resultfile = "D:/2021软件服务外包比赛/result/" + 'train_20210120_date_split.csv' # 数据探索结果表 print(is_workday(datetime.now())) print(is_holiday(datetime.now())) print(is_in_lieu(datetime.now())) data_train = pd.read_csv(datafile) print(data_train['seg_dep_time'][0]) print(datetime.strptime(data_train['seg_dep_time'][0], '%Y/%m/%d %H:%M')) # data_train['seg_dep_time_month'] = [data_train['seg_dep_time'][i].month for i in # range(len(data_train))] # data_train['seg_dep_time_year'] = [data_train['seg_dep_time'][i].year for i in # range(len(data_train))] data_train['seg_dep_time_is_workday'] = [ is_workday( datetime.strptime(data_train['seg_dep_time'][i], '%Y/%m/%d %H:%M')) for i in range(len(data_train)) ] data_train['seg_dep_time_is_holiday'] = [ is_holiday( datetime.strptime(data_train['seg_dep_time'][i], '%Y/%m/%d %H:%M')) for i in range(len(data_train)) ] data_train['seg_dep_time_is_in_lieu'] = [ is_in_lieu( datetime.strptime(data_train['seg_dep_time'][i], '%Y/%m/%d %H:%M'))
import datetime # 判断 2018年4月30号 是不是节假日 from chinese_calendar import is_workday, is_holiday april_last = datetime.date(2018, 4, 30) print(april_last) # 或者在判断的同时,获取节日名 import chinese_calendar as calendar # 也可以这样 import on_holiday, holiday_name = calendar.get_holiday_detail(april_last) print(on_holiday) print(holiday_name) # 还能判断法定节假日是不是调休 import chinese_calendar print(chinese_calendar.is_in_lieu(datetime.date(2006, 1, 1))) print(chinese_calendar.is_in_lieu(datetime.date(2006, 1, 2))) # python -m test.test_workdays
import datetime from chinese_calendar import is_workday, is_holiday, is_in_lieu today = datetime.date.today() one_day_duration = datetime.timedelta(days=1) tomorrow = today + one_day_duration try: if tomorrow.weekday() in (5, 6) and is_workday(tomorrow): print( f'Tomorrow {tomorrow} is workday and weekend, do you hava set the alarm clock?' ) elif is_workday(today): print(f'Just work.') elif is_in_lieu(today): print(f'Today {today} is holiday but also lieu.') elif is_holiday(today): print(f'Today {today} is holiday, hava a gooo day!') except NotImplementedError: print('Please check chinese_calendar had updated and the date is invalid.')
def overtime(worktimes): data = {} for name in worktimes: for worktime in worktimes[name]: start = dateutil.parser.parse( worktime['start']) if worktime['start'] else None end = dateutil.parser.parse( worktime['end']) if worktime['end'] else None today = dateutil.parser.parse(worktime['day']) yesterday = today - datetime.timedelta(1) tommorrow = today + datetime.timedelta(1) # print(start, end ,today,yesterday,tommorrow,work_start_time(today),work_end_time(today)) workday = chinese_calendar.is_workday(start) lieu = chinese_calendar.is_in_lieu(start) holiday, holiday_name = chinese_calendar.get_holiday_detail(start) holiday_name = WEEKDAY[ start.weekday()] if not holiday_name else holiday_name # if not workday: # holiday, holiday_name = chinese_calendar.get_holiday_detail(o.start) # if holiday_name: # o.holiday_name = holiday_name if name not in data: data[name] = {'overtime': []} w = data[name]['overtime'] overtime = None if workday: # 如果首次提交时间小于上班时间,则认为是昨天的通宵加班 if start is not None and start < work_start_time(today): overtime = OverTime(yesterday, name) overtime.start = work_end_time(today) overtime.end = start # 如果最后一次提交大于下班时间,且没有到0点 if end is not None and end > work_end_time( today) and end < tommorrow: overtime = OverTime(today, name) overtime.start = work_end_time(today) overtime.end = end else: if start is not None and start > work_start_time(today): overtime = OverTime(today, name) overtime.start = work_start_time(today) if end is None: overtime.end = work_end_time(today) else: overtime.end = end if end > work_end_time( today) else work_end_time(today) if overtime is None: continue overtime.duration = math.ceil( (overtime.end - overtime.start).seconds / 3600.0) overtime.workday = workday overtime.holiday = holiday overtime.holiday_name = holiday_name overtime.lieu = lieu w.append(overtime) return data