def get_timespace_of_trade_day(date, product='M'): """ 根据指定的日期date ,返回此交易日的行情数据时间段(开始-结束) date的前一个交易日的夜盘开始 , 需要搜索前一个交易日的夜盘开始到本date交易日的下午收盘结束 20:59(-1) - 15:30 之前请务必清洗掉非正常行情交易tick :return [start,end) (2019-5-10 21:00 , 2019-5-11 15:30) """ result = () if isinstance(date, str): date = parse(date) date = date.date() days = product_trade_days.get(product, {}) sorted_days = days.keys() idx = sorted_days.index(date) if idx == -1: # 无效的交易日 getLogger().error('date: {} is not defined in trade_days.txt'.format( str(date))) return () start = date - TimeDelta(days=1) # 默认前一天为前一个交易日 if idx != 0: # 如果不是第一条日期记录则找前一条 idx -= 1 start = sorted_days[idx] # 规定日线从前一个交易日的夜盘开始,到交易日(date)的下午收盘时间为止 # start = DateTime.combine(start,Time(20,59,0,0)) start = DateTime.combine(start, Time(21, 0, 0, 0)) date = DateTime.combine(date, Time(15, 30, 0, 0)) # start.replace(hour=20,minute=59,second=0,microsecond=0) # date.replace(hour=15,minute=30,second=0,microsecond=0) # return (start, date)
def add_elements(self, elements): from datetime import time as Time for element in elements: time_start = element["time_start"] type = element["type"] planning_element = PlanningElement(planning=self, type='single', time_start=Time( time_start["hour"], time_start["minute"]), day=element["day"], random=False) planning_element.source_id = element["audiosource_id"] if type in ["continuous", "jingle"]: time_end = element["time_end"] if time_end["hour"] == 24: planning_element.time_end = Time(23, 59) else: planning_element.time_end = Time(time_end["hour"], time_end["minute"]) planning_element.random = True planning_element.type = type planning_element.save()
def get_datetime_from_str(items): start_time = Time( *map(int, items[1].split(':'))).strftime("%I:%M") + items[2].upper() end_time = Time( *map(int, items[4].split(':'))).strftime("%I:%M") + items[5].upper() start_time = datetime.strptime(start_time, '%I:%M%p') end_time = datetime.strptime(end_time, '%I:%M%p') return start_time, end_time
def test_class_from_table(): """Tests if classes are correctly extracted from <table> tags.""" with open('tests/assets/class_table.html') as f: table_html = f.read() table = bs4.BeautifulSoup(table_html, "html.parser") expected_classes = [ Class( class_id="04208A", offered_vacancies=24, available_vacancies=3, orders_without_vacancy=0, special_students=0, teachers=["Ricardo Pereira e Silva"], schedule=[ ScheduleTime( weekday=Weekday.TUESDAY, time=Time(13, 30), duration=3, room="CTC-CTC108", ), ScheduleTime( weekday=Weekday.WEDNESDAY, time=Time(8, 20), duration=2, room="CTC-CTC107", ), ], ), Class( class_id="04208B", offered_vacancies=22, available_vacancies=0, orders_without_vacancy=0, special_students=0, teachers=["Patricia Vilain"], schedule=[ ScheduleTime( weekday=Weekday.TUESDAY, time=Time(13, 30), duration=3, room="AUX-ALOCAR", ), ScheduleTime( weekday=Weekday.WEDNESDAY, time=Time(8, 20), duration=2, room="AUX-ALOCAR", ), ], ), ] assert _table_to_classlist(table) == expected_classes
def popular_hour(df, city, trip_count, run_time_list): '''Prints the most popular start hour, and its count, from df. Appends 'run time info' to a list for future printing. Args: df: Pandas DataFrame. run_time_list: (list). city: (str) name of city for df. trip_count: (int) number of rows in df. Returns: run_time_list: (list) collection of run-time info. ''' start_time = time.time() df_time = df.dt.hour popular_hour = df_time.mode().loc[0] convert_hour = Time(hour=popular_hour) popular_hour_count = df_time.value_counts().loc[popular_hour] # Save run-time info and print stats run_time_list.append( "Calculating the most popular start hour took %s seconds." % round( (time.time() - start_time), 4)) print( "Most popular hour to start a trip: {} (Trips: {:,})".format( convert_hour, popular_hour_count)) return run_time_list
def getTime(self): if self.hour.get().isdigit() and self.minute.get().isdigit and \ self.second.get().isdigit(): return Time(int(self.hour.get()), int(self.minute.get()), \ int(self.second.get())) else: return False
def import_attendances( source: Path, threshold: int = 15, db: Path = DEFAULT_DB, ): '''Imports a time block as a Sympla attendance XLSX file into database.''' database = load_db_or_create(db) sheet = Sheet.load(source) attendances = attendance_block_from_sheet(sheet) attendances = AttendanceBlock( block=block_for_timespan( sheet.date, sheet.start, sheet.end, database.blocks, threshold=Time(0, threshold, 0), ), attenders=attendances.attenders, ) print(f'Fit into {attendances.block}', end='') database.add_attendances(attendances) database.save() print('Done.')
def duration2str(seconds): t = Time(hour=seconds // 3600, minute=(seconds // 60) % 60, second=seconds % 60) if t.hour: return t.strftime('%H:%M:%S') else: return t.strftime('%M:%S')
def afterHours(now=None): # checks if the market is open(only used in autonomous trading bot) tz = timezone('Asia/Kolkata') us_holidays = holidaysUS() if not now: now = datetime.now(tz) openTime = Time(hour=9, minute=30, second=0) closeTime = Time(hour=16, minute=0, second=0) # If a holiday if now.strftime('%Y-%m-%d') in us_holidays: return True # If before 0930 or after 1600 if (now.time() < openTime) or (now.time() > closeTime): return True # If it's a weekend if now.date().weekday() > 4: return True return False
def b_is_stock_market_open() -> bool: """ checks if the stock market is open """ # Get current US time now = datetime.now(timezone("US/Eastern")) # Check if it is a weekend if now.date().weekday() > 4: return False # Check if it is a holiday if now.strftime("%Y-%m-%d") in holidaysUS(): return False # Check if it hasn't open already if now.time() < Time(hour=9, minute=30, second=0): return False # Check if it has already closed if now.time() > Time(hour=16, minute=0, second=0): return False # Otherwise, Stock Market is open! return True
def PgTime(hour, minute, second): """Construct an object holding a time value. This function is part of the `DBAPI 2.0 specification <http://www.python.org/dev/peps/pep-0249/>`_. :rtype: :class:`datetime.time` """ return Time(hour, minute, second)
def TimeFromTicks(ticks): """Construct an objet holding a time value from the given ticks value (number of seconds since the epoch). This function is part of the `DBAPI 2.0 specification <http://www.python.org/dev/peps/pep-0249/>`_. :rtype: :class:`datetime.time` """ return Time(*localtime(ticks)[3:6])
def get_time_from_str(time_str: str) -> Time: """ Convers an string with the format "HH:MM" to a time instance :param time_str: The time string :return: The time instance """ number_strings: List[str] = time_str.split(":") hour: int = int(number_strings[0]) minute: int = int(number_strings[1]) return Time(hour=hour, minute=minute)
def _parse_time(time: str) -> ScheduleTime: time, room = time.split(" / ") weekday, time = time.split(".") time, duration = time.split("-") hour, minute = time[:2], time[2:] return ScheduleTime( weekday=Weekday(int(weekday)), time=Time(int(hour), int(minute)), duration=int(duration), room=room, )
def test_py_value_fail(con, mocker): # Ensure that if types.py_value throws an exception, the original # exception is raised (PG8000TestException), and the connection is # still usable after the error. mocker.patch.object(con, "py_types") con.py_types = {Time: raise_exception} with pytest.raises(PG8000TestException): con.run("SELECT CAST(:v AS TIME)", v=Time(10, 30)) # ensure that the connection is still usable for a new query res = con.run("VALUES ('hw3'::text)") assert res[0][0] == "hw3"
def prepare_kline_calc_minutes(self): """当天载入当日计算时间分钟点和跨日分钟点 凌晨2:40停止程序运行 """ now = DateTime.now() if now.time() < Time(3, 0): # 如果在凌晨启动的话,去前一天的计算分钟点 now = now - TimeDelta(days=1) for symbol in self.kline_symbols: self.all_calc_mins[symbol] = {} for k in (1, 5, 15, 30, 60): mins = kline.get_day_trade_calc_minutes_new(symbol, k, now) self.all_calc_mins[symbol][k] = mins
def test_time_from_str(): """Tests if a class's schedules are correctly extracted from default CAGR time strings.""" expected_times = { "3.1330-3 / CTC-CTC108": ScheduleTime( weekday=Weekday.TUESDAY, time=Time(13, 30), duration=3, room="CTC-CTC108", ), "4.0820-2 / CTC-CTC107": ScheduleTime( weekday=Weekday.WEDNESDAY, time=Time(8, 20), duration=2, room="CTC-CTC107", ), } assert all( _parse_time(text) == expected for text, expected in expected_times.items())
def parse_time(cls, data): """ Parses a time from the data stream e.g. 101039 """ if len(data) != 6: raise ValueError("Timestamp must have 6 characters") hour = int(data[0:2]) minute = int(data[2:4]) second = int(data[4:6]) return Time(hour, minute, second)
def post(self, request): credentials = {} credentials = request.META['HTTP_AUTHORIZATION'] date = request.META['HTTP_CDATE'] time = request.META['HTTP_TIME'] #changed #timestamp = request.META['HTTP_TIMESTAMP'] data = str(credentials) data = data.replace('Basic ', '') data = base64.b64decode(data) reg = re.compile('(\w+)[:=] ?"?(\w+)"?') data = dict(reg.findall(data)) for key in data: username = key password = data[username] try: officer = AdminOfficer.objects.get(username=username) password = hashlib.sha224(password).hexdigest() if str(officer.password) == password: token = AdminOfficerToken() token.officer = officer token.active = True token.logindate = date token.logintime = time comptime = datetime.strptime(str(time), "%H:%M:%S").time() if (comptime >= Time(12, 0, 0)): testdate = datetime.strptime(str(date), '%Y-%m-%d').date() testdate = testdate + timedelta(1, 0, 0, 0, 0, 0, 0) token.logoutdate = testdate.strftime('%Y-%m-%d') testtime = datetime.strptime(str(time), '%H:%M:%S') testtime = testtime + timedelta(0, 0, 0, 0, 0, 12, 0) token.logouttime = testtime.strftime('%H:%M:%S') else: token.logoutdate = date testtime = datetime.strptime(str(time), '%H:%M:%S') testtime = testtime + timedelta(0, 0, 0, 0, 0, 12, 0) token.logouttime = testtime.strftime('%H:%M:%S') #changed #token.login_timestamp = timestamp #logouttime = datetime.strptime(str(timestamp),'%Y-%m-%d %H:%M:%S') #logouttime = logouttime + timedelta(0,0,0,0,0,12,0) #token.logout_timestamp = logouttime.strftime('%Y-%m-%d %H:%M:%S') # default timeout of 12 hours token.save() session_id = token.token return JSONResponse({'done': 'ok'}, session_id) else: return JSONResponse({'error': 'Password'}, '') except AdminOfficer.DoesNotExist: return JSONResponse({'error': 'Username'}, '')
def block_for_timespan( date: Date, start: Time, end: Time, blocks: List[TimeBlock], threshold: Time = Time(0, 15, 0), ) -> TimeBlock: for block in blocks: s, e = block.start, block.end if (block.date.weekday() == date.weekday() and start >= rewind_time(s, threshold) and end <= advance_time(e, threshold)): return block raise NoFittingBlock( f'No block between {start} and {end} found for day {date}.')
def popular_hour(data): '''Finds the most popular hour in data['Start Time'] and returns the hour and its count. Args: list of dictionaries with a key of 'Start Time'. Returns: (str) most popular hour in 12 hr format , (int) count. ''' # Create list of all hours found in data[key] as (int) hour_list = [int(element['Start Time'][11:13]) for element in data] # Determine the most popular hour and count using most_pop_count my_hour, count = most_pop_count(hour_list) # Convert hour to 12hr time my_hour = Time(hour=my_hour) my_hour = Time.strftime(my_hour, '%#I %p') return my_hour, count
def time_work_right_short(spaces,t): """ 考虑 开市前的1分钟集合竞价,每个交易尾端延时1分钟缓冲 '*', 跨日'-'时段不考虑尾端延长 """ t = t.time() for space in spaces: if space[1] == Time(0,0) and t >=space[0]: return space e = (DateTime.combine(DateTime.now().date(), space[1]) + TimeDelta(minutes=1)).time() if t >= space[0] and t < e: # 包含右侧结束时间(保持1分钟数据接收缓冲) return space if len(space) == 3 and space[-1].count('*'): # 时间段开始包括 1分钟的集合竞价时间 s = (DateTime.combine( DateTime.now().date(),space[0]) - TimeDelta(minutes=1)).time() if t >= s and t < e: return space return ()
def __init__(self, locked=False): init_dict = self.__init_dict__ for key in init_dict: value = init_dict[key] date, mo, da, ye, time, ho, mi, se = [None] * 8 if isinstance(value, str): date, da, mo, ye, time, ho, mi, se = reDateTime.match( value).groups() if mo and int(mo) > 12: mo, da = da, mo if type(init_dict[key]) == dict: setattr(self, key, makeFMData( init_dict[key], locked=False)) # lock all substructures?? elif type(init_dict[key]) == list: l = [] for d in init_dict[key]: if type(d) == dict: l.append(makeFMData(d)) # lock ?? else: l.append(d) setattr(self, key, l) elif date and time: setattr( self, key, DateTime(int(ye), int(mo), int(da), int(ho), int(mi), int(se))) elif date: setattr(self, key, Date(int(ye), int(mo), int(da))) elif time: setattr(self, key, Time(int(ho), int(mi), int(se))) else: setattr(self, key, init_dict[key]) if locked: self.__modified__.add('__locked__')
def time_from_string(*hh_col_mms): ret_val = [] for hh_col_mm in hh_col_mms: hh_part, mm_part = hh_col_mm.split(":") ret_val.append(Time(int(hh_part), int(mm_part))) return ret_val[0] if len(ret_val) == 1 else ret_val
def test_time_in(): actual = pg8000.converters.time_in("12:57:18.000396") assert actual == Time(12, 57, 18, 396)
def test_time_roundtrip(con): retval = con.run("SELECT cast(:t as time) as f1", t=Time(4, 5, 6)) assert retval[0][0] == Time(4, 5, 6)
def testTimeRoundtrip(cursor): cursor.execute("SELECT ? as f1", (Time(4, 5, 6),)) retval = cursor.fetchall() assert retval[0][0] == '04:05:06'
def main(): #dict for records which may be repeated #list for records which are constant all_venues = {'': None} all_teachers = {'': None} all_timeslots = {} all_courses = {'': None} slots = [] all_sections = {'': None} tt = load_workbook('timetable.xlsx') sheet = tt.worksheets[0] #counts to keep track of new entries and IDs of records venue_count = 0 teacher_count = 0 timeslot_count = 0 course_count = 0 section_count = 0 for sheetnum, sheet in zip(range(len(tt.worksheets)), tt.worksheets): #initializing local venues and timeslots which will be overridden for each sheet(day) venues = [] timeslots = [] # read timeslots for cell in sheet[4][2:]: if (cell.value == None): break else: time = cell.value.split('-') start = time[0].replace(' ', '').split(':') end = time[1].replace(' ', '').split(':') timeslots.append( TimeSlot( Time(int(start[0].strip()), int(start[1].strip())), Time(int(end[0].strip()), int(end[1].strip())))) # read venues for col in sheet.iter_cols(2, 2): empty = False for cell in col[4:]: if (cell.value is not None): venues.append(Venue(cell.value.strip().upper())) elif (empty): break else: empty = True #saving newly found venues and timeslots in global dictionaries for venue in venues: if venue not in all_venues: venue_count += 1 all_venues[venue] = venue_count for timeslot in timeslots: if timeslot not in all_timeslots: timeslot_count += 1 all_timeslots[timeslot] = timeslot_count #iterating over each slot of timetable for i, venue in zip(range(len(venues)), venues): for j, timeslot in zip(range(len(timeslots)), timeslots): if (sheet.cell(i + 5, 2).value is None ): #if no venue, probably filler therefore continue continue slot = sheet.cell(i + 5, j + 3).value if (slot is None): #if slotvalue empty, it's an unoccupied slot slots.append( Slot(sheetnum + 1, all_venues[venue], all_timeslots[timeslot], None, None, None)) elif ( len(slot.split('\n')) != 3 ): #if slotvalue cannot be divided into 3 parts, it's invalid therefore skip slots.append( Slot(sheetnum + 1, all_venues[venue], all_timeslots[timeslot], None, None, None)) else: slotvalues = slot.split('\n') #getting the course if (slotvalues[0].strip().upper() is not None): courseObj = Course(slotvalues[0].strip().upper()) if courseObj not in all_courses: course_count += 1 all_courses[courseObj] = course_count else: courseObj = '' #getting the teacher if (slotvalues[1].strip().upper() is not None): teacherObj = Teacher(slotvalues[1].strip().upper()) if teacherObj not in all_teachers: teacher_count += 1 all_teachers[teacherObj] = teacher_count else: teacherObj = '' #getting the section(also atomically dividing the attributes) sectionValues = slotvalues[2].replace('[', '').replace( ']', '').replace('/', '') if (sectionValues[0] in '12345678'): sectionObj = Section(int(sectionValues[0]), (sectionValues[1:])) if sectionObj not in all_sections: section_count += 1 all_sections[sectionObj] = section_count else: sectionObj = '' slots.append( Slot(sheetnum + 1, all_venues[venue], all_timeslots[timeslot], all_courses[courseObj], all_teachers[teacherObj], all_sections[sectionObj])) try: dbconn = psycopg2.connect(config.DATABASE_URL) courseQuery = ("INSERT INTO courses" "(coursecode) " "values (%s)") sectionQuery = ("INSERT INTO sections" "(semester,section) " "values (%s,%s)") teacherQuery = ("INSERT INTO teachers" "(teachername) " "values (%s)") timeslotQuery = ("INSERT INTO timeslots" "(starttime, endtime) " "values (%s,%s)") venueQuery = ("INSERT INTO venues" "(venuename) " "values (%s)") slotQuery = ("INSERT INTO slots " "values (%s,%s,%s,%s,%s,%s)") cursor = dbconn.cursor() for course in all_courses: if course != '': print("Inserting " + course.__repr__()) cursor.execute(courseQuery, (course.courseCode, )) for section in all_sections: if section != '': print("Inserting " + section.__repr__()) cursor.execute(sectionQuery, (section.semester, section.section)) for teacher in all_teachers: if teacher != '': print("Inserting " + teacher.__repr__()) cursor.execute(teacherQuery, (teacher.teacherName, )) for timeslot in all_timeslots: if timeslot != '': print("Inserting " + timeslot.__repr__()) cursor.execute(timeslotQuery, (timeslot.startTime, timeslot.endTime)) for venue in all_venues: if venue != '': print("Inserting " + venue.__repr__()) cursor.execute(venueQuery, (venue.venueName, )) for slot in slots: if slot != '': print("Inserting " + slot.__repr__()) cursor.execute( slotQuery, (slot.dayNum, slot.venueNum, slot.timeslotNum, slot.courseNum, slot.teacherNum, slot.sectionNum)) dbconn.commit() except psycopg2.Error as error: print("Fail {}".format(error)) finally: cursor.close() dbconn.close()
def test_literal_Time(): assert literal(Time(22, 13, 2)) == "'22:13:02'"
from datetime import time as Time from flasgger import swag_from from flask import jsonify, Response, request from flask_jwt_extended import get_jwt_identity, jwt_required from app.doc.apply.extension import EXTENSION_GET, EXTENSION_POST, EXTENSION_DELETE, EXTENSION_MAP_GET from app.exception import NoContentException, ApplyTimeException from app.model import ExtensionApplyModel from app.util.json_schema import json_type_validate, EXTENSION_POST_JSON from app.view.base_resource import ApplyResource extension_apply_start = {11: Time(17, 30), 12: Time(17, 30)} extension_apply_end = {11: Time(20, 30), 12: Time(23, 00)} class Extension(ApplyResource): @swag_from(EXTENSION_GET) @jwt_required() def get(self, time: int): id: str = get_jwt_identity() extension_apply = ExtensionApplyModel.get_extension_apply_status( id, time) if extension_apply is None: raise NoContentException() return jsonify(extension_apply) @json_type_validate(EXTENSION_POST_JSON) @swag_from(EXTENSION_POST)