def __get_start_time(self, generation): import pytz import math from dateutil.parser import parse as timeparse # TODO: NEED TO CHECK ALL DATABASES TO ENSURE THAT THE TIME RANGE ARE GOOD start_datetime = self.configs['study']['start_datetime'] start_timezone = self.configs['study']['timezone'] # If start_datetime is a single time, set that as start time if isinstance(start_datetime, str): start_time = pytz.timezone(start_timezone).localize( timeparse(start_datetime)) return int(start_time.timestamp()) # If start_datetime is formatted as a time step with beginning and end, choose either of these as a start time # If sequential is set then the startime will if isinstance(start_datetime, (list, tuple)): if len(start_datetime) == 2: start_time_s = int( pytz.timezone(start_timezone).localize( timeparse(start_datetime[0])).timestamp()) start_time_e = int( pytz.timezone(start_timezone).localize( timeparse(start_datetime[1])).timestamp()) # This is the sequential startime code if 'start_datetime_sequence' in self.configs['study']: if self.configs['study'][ 'start_datetime_sequence'] == 'sequential': interval = int( (start_time_e - start_time_s) / self.configs['study']['generations'] / 60) * 60 start_time = range(start_time_s, start_time_e, interval)[generation] return start_time start_time = random.choice( range(start_time_s, start_time_e, 60)) return start_time else: if 'start_datetime_sequence' in self.configs['study']: if self.configs['study'][ 'start_datetime_sequence'] == 'sequential': multiplier = math.ceil( self.configs['study']['generations'] / len(start_datetime)) start_time_readable = start_datetime * multiplier[ generation] start_time = pytz.timezone(start_timezone).localize( timeparse(start_time_readable)) return start_time start_time = pytz.timezone(start_timezone).localize( timeparse(random.choice(start_datetime))) return int(start_time.timestamp())
async def schedule_message(self, ctx, *args): args = bot_command_args(args) msg = args[1] if args[0].lower() == 'now': time = datetime.datetime.now() + datetime.timedelta(seconds=3) else: time = timeparse(args[0]) if time < datetime.datetime.now(): await ctx.send(config['error_messages']['schedule_past']) return False try: channel = try_get_channel_named(ctx.guild, args[2].lstrip('#')) except ValueError as e: await ctx.send(str(e)) return False except UserWarning as e: await ctx.send(str(e)) await store_scheduled_message(str(time), msg, ctx.guild.id, channel.id) await ctx.message.add_reaction('👍') await send_scheduled_message({ 'time': time, 'msg': msg, 'channel': channel }) return True
def meta(self): meta = yaml.safe_load(self._meta_yaml) if not meta: return {} assert isinstance(meta, dict) try: self.date = timeparse(meta['date']) except KeyError: self.date = datetime.fromtimestamp(os.path.getmtime(self.full_path)) meta['date'] = self.date.strftime(gen.config['DATETIME']) try: meta['slug'] except KeyError: rules = [ ('%title%', meta['title']), ("%path%", self.path), ("%year%", str(self.date.year)), ("%month%", str(self.date.month)), ("%day%", str(self.date.day)), ("%hour%", str(self.date.hour)), ("%minute%", str(self.date.minute)), (" ", "-") ] slug = self._slug for (expression, replacement) in rules: slug = replace(slug, expression, replacement) meta['slug'] = lower(slug) try: meta['author'] except KeyError: meta['author'] = gen.config['AUTHOR'] return meta
def makeWeekinfo(schedules,starting_time,totalweeks): info_ls = [] for i in range(0,totalweeks): restsched = [] weekscheds = [] week_ls = [] cnt = 0 cutpoint_back = starting_time+timedelta(days=7*(i+1)) cutpoint_front = starting_time+timedelta(days=7*(i)) print('cutpoint :',cutpoint_front,cutpoint_back) for sched in schedules: # 스케줄을 1주단위로 쪼개기 if sched.period_start < cutpoint_back: weekscheds.append(sched) if sched.period_end >= cutpoint_back: restsched.append(sched) else: restsched.append(sched) totalcnt = len(weekscheds) # print(totalcnt, weekscheds) while 1: # 스케줄을 row 배치순서로 넣기 row_ls = [] rests = [] latest = timeparse("1970-01-01") recog = 0 print('latest : ',latest) if totalcnt <= cnt : # 모든 스케줄을 다 소화했으면 break break for sched in weekscheds: # 1줄당 들어갈 스캐줄 비교 temp = timeparse(sched.period_start.strftime('%Y-%m-%d')) if (temp >= latest): if recog==0: recog=1 nullsize = (timeparse(sched.period_start.strftime("%Y-%m-%d"))-cutpoint_front).days else: nullsize = (timeparse(sched.period_start.strftime("%Y-%m-%d"))-latest).days row_ls.append([nullsize,sched])# 전 공백사이즈 삽입, #스케줄정보 삽입 latest = timeparse(sched.period_end.strftime('%Y-%m-%d')) + timedelta(days=1) cnt += 1 print(cnt, end="") else : rests.append(sched)# 남은 내용물 다시 배치 week_ls.append(row_ls) weekscheds = rests info_ls.append(week_ls) schedules = restsched return info_ls
def interpret(json): if type(json) != dict: raise TypeError( 'Expecting a dictionary representing a valid JSON object.') else: # let's do it form_internals = json['Form'] form_entry = json['Entry'] formId = form_internals['Id'] # formID has ID 17 for RCRs formTimeStamp = form_entry['DateSubmitted'] today = date.today() formDate = timeparse(formTimeStamp) # FD object holds a queryset of the formData objects on the date of the form, of that form id FD = FormData.objects.filter(date=formDate).filter(formID=formId) # RCR processing first if formId == '17': # RCRs will also filter by staff name #send_to_server(json) # Uncomment the next line if you want a copy of the RCR data sent to dev accounts. NOT RECOMMENDED FOR PRODUCTION #notify_devs('info', 'RCR Form data recorded. Type is: "{}" and its data is: "{}".'.format(form_internals['Name'], str(json))) fname = json['StaffName']['First'] lname = json['StaffName']['Last'] try: staff = Employee.objects.filter(user__first_name=fname).get( user__last_name=lname) except Employee.DoesNotExist: staff = Employee.objects.get( user__username='******' ) # anonymous RCR, assign it to the django admin superuser account try: FD = FD.get(staff=staff) FD.qty += 1 FD.save() except FormData.DoesNotExist: new = FormData.objects.create(formID=int(formId), formName=form_internals['Name'], date=date.today(), qty=1) new.staff = staff new.save() else: notify_devs( 'warning', 'Form data recorded: Type is: "{}" and its data is: "{}".'. format(form_internals['Name'], str(json))) try: data = FD[0] data.qty = data.qty + 1 data.save() except IndexError: new = FormData.objects.create(formID=int(formId), formName=form_internals['Name'], date=date.today(), qty=1) new.save()
def markLog(self, msg_content): """ The !markLog command Give a section of the log a name so that it can be sent and manipulated later. """ splits = msg_content.split() if len(splits) >= 3: max_time_row = self.log.execute( "SELECT MAX(date) FROM log;").fetchone() if not max_time_row: self.send("Nothing logged. Cannot mark.") return default = datetime.fromtimestamp(max_time_row[0]) try: start_time = time.mktime( timeparse(splits[1], default=default).timetuple()) except ValueError: self.send("Incorrect date format. Cannot mark.") return try: end_time = time.mktime( timeparse(splits[2], default=default).timetuple()) subject = " ".join(splits[3:]) self.set_mark(subject, start_time, end_time) except ValueError: #the end time we tried to parse was actually part of the subject end_time = max_time_row[0] subject = " ".join(splits[2:]) self.set_mark(subject, start_time, end_time) except IndexError: #the subject we tried to get doesn't actually exist self.send("No subject specified. Cannot mark.") else: #give usage help self.send("usage: !markLog start_time [end_time] subject") self.send("end time defaults to last logged message") self.send("specify time like this: DD-MM-YYYY_HH:MM:SS") self.send("date defaults to date of last logged message")
def _update_status_full(self): headers = {"X-Api-Token": self._token} url = urljoin(self._url, "table/%d" % self._table_id) r = requests.get(url, headers=headers) self._status = json.loads(r.content) self._name = self._status['Name'] self._dataset = self._status['Dataset'] self._last_ok_run = timeparse(self._status["LastOkRunEndAt"]) return self._status
def cal_week(start_date, how_many): # user = User.query.filter_by(username=username).first_or_404() # print(type(start_date)) # schedules = current_user.schedules.order_by(Schedule.period_start.desc()) if int(how_many) > 0: start_datetime = timeparse(start_date)+timedelta(days=1) how_many = int(how_many) else : start_datetime = timeparse(start_date)-timedelta(days=14) how_many = -int(how_many) starting_time = timeparse(start_datetime.strftime('%Y-%m-%d')) ending_time = (starting_time+timedelta(days=how_many*7)) schedules = current_user.schedules.filter(and_(Schedule.period_end >= starting_time, Schedule.period_start <= ending_time)).order_by(Schedule.period_start.asc()) cal_print = [[starting_time+timedelta(days=date+wk*7) for date in range(0,7)]for wk in range(0,how_many)] time_now = datetime.utcnow()+timedelta(hours=9) #한국시간 기준(UTC보다 9시간 빠름) weekschedule_info = makeWeekinfo(schedules,starting_time,how_many) return render_template('cal_schedule.html', schedules = weekschedule_info, # schedules, cal_print=cal_print, time_now = time_now)
def stored_strings_to_objects(stored): future_messages = [] for message in stored: parsed = timeparse(message['iso_time']) channel = bot.get_guild(message['guild_id']).get_channel( message['channel_id']) future_messages.append({ 'time': parsed, 'msg': message['msg'], 'channel': channel }) return future_messages
def _update_status_short(self): headers = {"X-Api-Token": self._token} url = urljoin(self._url, "table/%d/status" % self._table_id) r = requests.get(url, headers=headers) status = json.loads(r.content) if status["Error"]: raise Exception(status["Error"]) self._last_ok_run = timeparse(status["LastOkRunEndAt"]) self._status["Running"] = status["Status"] == "running" return self._status
def get_start_time(self): metadata = self.__get_metadata(self.__generation) if metadata: return metadata['start_timestamp'] import pytz from dateutil.parser import parse as timeparse tz_str = self.__config['study']['timezone'] dt_str = self.__config['study']['start_datetime'] start_time = pytz.timezone(tz_str).localize(timeparse(dt_str)) return int(start_time.timestamp())
def schedule_rawtext_parser(text): temp = text.split("\n",2) if temp[0].find('~')!=-1: due = 1 dates = temp[0].split('~') ndates = [] for date in dates: try:# parse라는 말이 다용도라서.. timeparse로 변경하여 사용 ndates.append(timeparse(date.strip()).strftime("%Y-%m-%d %H:%M")) except: ndates.append(ndates[0]) confirm_time_reverse = (timeparse(ndates[1])-timeparse(ndates[0])).days if confirm_time_reverse < 0: ndates[1] = (timeparse(ndates[1]) + timedelta(days=-confirm_time_reverse)).strftime("%Y-%m-%d %H:%M") else: due = 0 ndates = [] # 일부러 두번넣는다. ndates.append(timeparse(temp[0].strip()).strftime("%Y-%m-%d %H:%M")) ndates.append(str(ndates[0])) return ndates, temp
def markLog(self, msg_content): """ The !markLog command Give a section of the log a name so that it can be sent and manipulated later. """ splits = msg_content.split() if len(splits) >= 3: max_time_row = self.log.execute("SELECT MAX(date) FROM log;").fetchone() if not max_time_row: self.send("Nothing logged. Cannot mark.") return default = datetime.fromtimestamp(max_time_row[0]) try: start_time = time.mktime(timeparse(splits[1], default=default).timetuple()) except ValueError: self.send("Incorrect date format. Cannot mark.") return try: end_time = time.mktime(timeparse(splits[2], default=default).timetuple()) subject = " ".join(splits[3:]) self.set_mark(subject, start_time, end_time) except ValueError: #the end time we tried to parse was actually part of the subject end_time = max_time_row[0] subject = " ".join(splits[2:]) self.set_mark(subject, start_time, end_time) except IndexError: #the subject we tried to get doesn't actually exist self.send("No subject specified. Cannot mark.") else: #give usage help self.send("usage: !markLog start_time [end_time] subject") self.send("end time defaults to last logged message") self.send("specify time like this: DD-MM-YYYY_HH:MM:SS") self.send("date defaults to date of last logged message")
def req_meta(url, **kwargs): html = req_html(url, **kwargs) if not html: return html try: htmld = pq(html) meta = {} meta['url'] = url meta['title'] = htmld('.postTitle2').text() meta['content'] = htmld('#cnblogs_post_body').text() date = htmld('#post-date').text().strip() if date == '': sk = html.find('cb_entryCreatedDate=\'') if sk > -1: sk = sk + 21 ek = html.find('\'', sk) date = html[sk:ek] meta['posttime'] = timeparse(date) title = htmld('title').text().strip() if title == '': title = htmld.xhtml_to_html()('title').text().strip() meta['nick'] = title.split('-')[-2].strip() parts = url[:-5].split('/') u = parts[3] postid = parts[-1] meta['username'] = u view = req_html( 'http://www.cnblogs.com/mvc/blog/ViewCountCommentCout.aspx?postId=' + postid) meta['view'] = int(view or 0) comment = req_html('http://www.cnblogs.com/mvc/blog/GetComments.aspx?postId='+postid+\ '&blogApp='+u+\ '&pageIndex=0&anchorCommentId=0&_=1471419238926') #meta['comment'] = json.loads(comment)['commentCount'] if comment else 0 meta['comment'] = int(comment[16:comment.find(',')]) if comment else 0 return meta except Exception as e: print(e, url) return False
async def show_until(self, ctx, until): until_time = timeparse(until) now_time = datetime.datetime.now() with open(config['scheduled_message_store'], 'r') as f: stored = stored_strings_to_objects(json.load(f)['messages']) to_show = sorted(list_group(filter( lambda x: now_time < x['time'] <= until_time, stored), key=lambda x: x['time']), key=lambda x: x[0]) for time, messages in to_show: text = ['at {}\n'.format(time)] for message in messages: text.append('{0[channel].mention}: {0[msg]}\n'.format(message)) await ctx.send(''.join(text), allowed_mentions=discord.AllowedMentions.none())
def calendar(): form = ScheduleForm() time_now = timeparse((datetime.utcnow()+timedelta(hours=9)).strftime('%Y-%m-%d')) #한국시간 기준(UTC보다 9시간 빠름) monday_delta = time_now.isocalendar()[2] % 7 # 일요일 기준으로 다시 맞춤 cal_print = [[time_now + timedelta(days=date+wk*7-monday_delta) for date in range(0,7)]for wk in range(-1,3)] starting_time = time_now + timedelta(days=-1*7-monday_delta) ending_time = time_now + timedelta(days=3*7-monday_delta) schedules = current_user.schedules.filter(and_(Schedule.period_end >= starting_time, Schedule.period_start <= ending_time)).order_by(Schedule.period_start.asc()) # print(starting_time,monday_delta) # 함수화된 일정순서 전처리 함수 weekschedule_info = makeWeekinfo(schedules,starting_time,4) return render_template('calendar.html', title="bright`s home", schedules = weekschedule_info,#schedules, weekschedule=weekschedule_info, cal_print=cal_print, time_now=time_now , form=form)
def _parse_feature(gml: Any) -> Dict[str, Any]: coords = _gml_find(gml, "gml:pos") lat, lon = coords.strip().split(" ") prop = _gml_find(gml, "BsWfs:ParameterName") value = _gml_find(gml, "BsWfs:ParameterValue") time_prop = _gml_find(gml, "BsWfs:Time") unix_timestamp = int(timeparse(time_prop).timestamp()) return { "property": prop, "value": value, "timestamp": unix_timestamp, "lat": lat, "lon": lon, }
def timestr_to_timestamp(time_string: str, timezone: str): timestamp = pytz.timezone(timezone).localize(timeparse(time_string)) return int(timestamp.timestamp())