def isoformat(date): if date: if date.tzinfo: date = date.astimezone(datetime.timezone.utc) date = date.replace(tzinfo=None) date = date.replace(microsecond=0) return date.isoformat('T')
def get_month_day_range(date): """ For a date, returns the start and end date of the month specified """ first_day = date.replace(day=1) last_day = date.replace(day=calendar.monthrange(date.year, date.month)[1]) return first_day, last_day
def MonthDelta(date, months): month = int(date.strftime("%m")) - months while month <= 0: month += 12 year = int(date.strftime("%Y")) - 1 date = date.replace(year=year) return date.replace(month=month)
def isoformat(date): if date: if isinstance(date, datetime.date) and not isinstance(date, datetime.datetime): return date.isoformat() if date.tzinfo: date = date.astimezone(datetime.timezone.utc) date = date.replace(tzinfo=None) date = date.replace(microsecond=0) return date.isoformat("T")
def applyTimeHint(date, hint): if not hint: return date if hint == TIME_HINT_BEGIN: return date.replace(hour=0, minute=0, second=0) elif hint == TIME_HINT_END: return date.replace(hour=23, minute=59, second=59) else: raise Exception("Unknown hint %s" % hint)
def findMonthlySummaryByAppAndMonth(appname, date, orderStr): date = date.replace(day=1) date = date.replace(hour=0,minute=0,second=0,microsecond=0) allowedOrderBy = ['callcount', 'totalduration', 'avgduration', 'minduration', 'maxduration', 'stdev'] order = 'callcount' if any(orderStr in s for s in allowedOrderBy): order = orderStr records = MonthlySummary.query.filter_by(app=appname,formattedcreated=date).order_by(order + ' desc').all() return records
def addmonths(date,months = 0): targetmonth=months+date.month try: if 0 == targetmonth%12: return date.replace(year=date.year+int(targetmonth/12) - 1,month=12) else: return date.replace(year=date.year + int(targetmonth / 12), month=(targetmonth % 12)) except Exception as e: # There is an exception if the day of the month we're in does not exist in the target month # Go to the FIRST of the month AFTER, then go back one day. date.replace(year=date.year+int((targetmonth+1)/12),month=((targetmonth+1)%12),day=1) date+=datetime.timedelta(days=-1) return date
def get_fiscal_date(self, date): if self.prorata == 'fiscal_year': last_day = self.company_id.fiscalyear_last_day last_month = self.company_id.fiscalyear_last_month if date: date = datetime.strptime(date, DF).date() else: date = datetime.now() year = date.year fiscal_date = date.replace(month=last_month, day=last_day, year=year) if fiscal_date < date: fiscal_date = date.replace(month=last_month, day=last_day, year=year + 1) return datetime.strftime(fiscal_date, '%Y-%m-%d')
def shift_month_behind(date): """ Returns date one month behind of <date> arguement with day set to 1. """ _verify_start_of_month(date) if date.month > 1: date = date.replace(month=date.month - 1) else: date = date.replace(month=12) date = date.replace(year=date.year - 1) return date
def shift_month_ahead(date): """ Returns date one month ahead of <date> argument with day set to 1. """ _verify_start_of_month(date) if date.month < 12: date = date.replace(month=date.month + 1) else: date = date.replace(month=1) date = date.replace(year=date.year + 1) return date
def finddate(row): date = re.search(r'201\d{1}(\.|\s|年)+\d{1,2}(\.|\s|月)*\d{1,2}', row).group(0) date = date.replace("年", ".") date = date.replace("月", ".") if " " in date: date = re.sub(" ", "", date) if ".." in date: date = re.sub("..", ".", date) try: return (datetime.strptime(date, "%Y.%m.%d").date()) except Exception: return date
def import_well_seasonaltimeseries(filename): '''Imports a seasonal timeseries well dataset, which is in turn produced by a MATLAB script I also wrote. (see well_data_MATLAB or similar such folder).''' csvData = pd.read_csv(filename,sep=',',skipinitialspace=True) headings = list(csvData.columns.values) dates=headings[6:] # dates start in the 6th column dates2 = [date.replace('Fall','Sep') for date in dates] dates2 = [date.replace('Spring','May') for date in dates2] dates= [dt.strptime(date, '%b%Y').date() for date in dates2] dates = date2num(dates) return csvData, dates
def get_month_day_range_move(self, date, is_format=False, format_str='%Y-%m-%d'): if date: first_day = date.replace(day=1) last_day = date.replace( day=calendar.monthrange(date.year, date.month)[1]) if not is_format: return first_day, last_day else: return first_day.strftime(format_str), last_day.strftime( format_str) return ()
def get_date_from_string(date_str): delimiters = ['.', '/'] assigned = False for delim in delimiters: if delim in date_str: assigned = True dates = date_str.split(delim) if not assigned or len(dates) > 3: raise TypeError(date_str) year = datetime.now().year date = datetime(year, int(dates[1]), int(dates[0])) if date < datetime.now(): date.replace(year=year + 1) return date
def getLast_day_of_month(date): ''' Parameters ---------- date Returns ------- ''' if date.month == 12: return date.replace(day=31) return date.replace(month=date.month + 1, day=1) - timedelta(days=1)
def from_date(cls, date, period=None): """ Create a day long daterange from for the given date. >>> daterange.from_date(date(2000, 1, 1)) daterange([datetime.date(2000, 1, 1),datetime.date(2000, 1, 2))) :param date: A date to convert. :param period: The period to normalize date to. A period may be one of: ``day`` (default), ``week``, ``american_week``, ``month``, ``quarter`` or ``year``. :return: A new range that contains the given date. .. seealso:: There are convenience methods for most period types: :meth:`~spans.types.daterange.from_week`, :meth:`~spans.types.daterange.from_month`, :meth:`~spans.types.daterange.from_quarter` and :meth:`~spans.types.daterange.from_year`. :class:`~spans.types.PeriodRange` has the same interface but is period aware. This means it is possible to get things like next week or month. .. versionchanged:: 0.4.0 Added the period parameter. """ if period is None or period == "day": return cls(date, date, upper_inc=True) elif period == "week": start = date - timedelta(date.weekday()) return cls(start, start + timedelta(7)) elif period == "american_week": start = date - timedelta((date.weekday() + 1) % 7) return cls(start, start + timedelta(7)) elif period == "month": start = date.replace(day=1) return cls(start, (start + timedelta(31)).replace(day=1)) elif period == "quarter": start = date.replace(month=(date.month - 1) // 3 * 3 + 1, day=1) return cls(start, (start + timedelta(93)).replace(day=1)) elif period == "year": start = date.replace(month=1, day=1) return cls(start, (start + timedelta(366)).replace(day=1)) else: raise ValueError("Unexpected period, got {!r}".format(period))
def convert_date(self,date): if "_" in date: date = date.replace("_", "-") if " " in date: date = date.replace(" ", "-") if date.replace("-", "").isdigit() == False: d = date.split("-") if len(d[1]) == 3: month = datetime.strptime(d[1],"%b").month else: month = datetime.strptime(d[1],"%B").month date = d[0] + "-" + str(month).zfill(2) + "-" + d[2] return date
def save_to_influx(result_json): influxdb_host = config.get("INFLUXDB", "host") influxdb_port = config.get("INFLUXDB", "port") influxdb_db = config.get("INFLUXDB", "db") influxdb_client = InfluxDBClient(host=influxdb_host, port=influxdb_port, database=influxdb_db) measurements = [] for hour in result_json: date = hour['datetime'] if "24:00" in date: date = datetime.datetime.strptime( date.replace("24:00", "00:00"), '%d/%m/%Y %H:%M') + datetime.timedelta(days=1) else: date = datetime.datetime.strptime(date, '%d/%m/%Y %H:%M') date = date - datetime.timedelta(hours=1) row = dict() row['measurement'] = "electricity_consumption" row['time'] = date row['fields'] = {"kWh": hour['consumo']} measurements.append(row) influxdb_client.write_points(measurements)
def wind_data(): wind_data_read_2017 = pd.read_csv(r'met_sum_insitu_1_obop_hour_2017.txt', header=None) wind_data_read_2018 = pd.read_csv(r'met_sum_insitu_1_obop_hour_2018.txt', header=None) wind_data_read_2019 = pd.read_csv(r'met_sum_insitu_1_obop_hour_2019.txt', header=None) wind_data_read = pd.concat( [wind_data_read_2017, wind_data_read_2018, wind_data_read_2019]) date_index = [] ws = [] wd = [] for index, row in wind_data_read.iterrows(): string = str(row.values) ws.append(float(string[26:30])) wd.append(float(string[21:24])) date = string[6:19] date = date.replace(' ', '-') date = datetime.strptime(date, '%Y-%m-%d-%H') #.strftime('%Y-%m-%d::%H') date_index.append(date) wind_data = pd.DataFrame(list(zip(wd, ws)), columns=['wd', 'ws'], index=date_index) wind_data = wind_data[wind_data.wd != 999.0] return wind_data
def rapport(request): if 'date' in request.GET: date = request.GET['date'] date = datetime.strptime(date, "%Y-%m-%d") else: date = datetime.today() lendemain = date + timedelta(days=1) # on met les deux dates a minuit date = date.replace(hour=0, minute=0, second=0) lendemain = lendemain.replace(hour=0, minute=0, second=0) ajoutes = Exemplaire.objects.all().filter(date_creation__gt=date, date_creation__lt=lendemain) factures = Facture.objects.all().filter(date_creation__gt=date, date_creation__lt=lendemain) nb_ajoutes = ajoutes.count() nb_factures = factures.count() nb_vendus = sum([f.nb_livres() for f in factures]) prix_total_vendu = sum([f.prix_total() for f in factures]) context = { 'nb_ajoutes': nb_ajoutes, 'nb_factures': nb_factures, 'date': date.date(), 'nb_vendus': nb_vendus, 'prix_total_vendu': prix_total_vendu, } return render_to_response('encefal/rapport.html', context)
def _checkParams(self): RHDisplayCategoryBase._checkParams(self) self.detail = request.args.get("detail", "event") if self.detail not in ("event", "session", "contribution"): raise BadRequest("Invalid detail argument") self.period = request.args.get("period", "day") if self.period not in ("day", "month", "week"): raise BadRequest("Invalid period argument") if "date" in request.args: try: date = datetime.strptime(request.args["date"], "%Y-%m-%d") except ValueError: raise BadRequest("Invalid date argument") else: date = datetime.now() date = self.category.display_tzinfo.localize(date) date = date.replace(hour=0, minute=0, second=0, microsecond=0) if self.period == "day": self.start_dt = date self.end_dt = self.start_dt + relativedelta(days=1) elif self.period == "week": self.start_dt = date - relativedelta(days=date.weekday()) self.end_dt = self.start_dt + relativedelta(days=7) elif self.period == "month": self.start_dt = date + relativedelta(day=1) self.end_dt = self.start_dt + relativedelta(months=1)
def searchDate(channelDate=""): xbmcplugin.setContent(pluginhandle, 'movies') channel = "" date = "" params = channelDate.split('|') channel = params[0] if len(params) > 1: date = params[1] result = [] if date == "": dialog = xbmcgui.Dialog() date = dialog.numeric(1, translation(30007)) date = re.sub('[^0-9|^\/]', '0', date) date = date.replace('/', '.') if (channel != "") and (len(date) == 10): data = getData() for entry in data: cEntry = entry if (entry[0] == channel) and (entry[3] == date): cEntry[1] = cEntry[2] + ': ' + cEntry[1] result.append(cEntry) result.sort(key=lambda entry: entry[1].lower()) for entry in result: addVideo(entry) xbmcplugin.endOfDirectory(pluginhandle)
def parse_einfor(self, response): item = NaverFcItem() item['url'] = response.url item['title'] = str( response.xpath("//div[@class='article-head-title']/text()").get()) date = response.xpath( "//ul[@class='no-bullet auto-marbtm-0 line-height-6']/li[2]/text()" ).get() item['name'] = '연합인포맥스' item['date'] = date.replace('승인', '').strip() content = str( response.xpath("//div[@itemprop='articleBody']/text()").getall()) content = re.sub( ' +', ' ', str( re.sub(re.compile('<.*?>'), ' ', content.replace('\\t', '')).replace('\\r', '').replace( '\t', '').replace('\\xa01', '').replace( '\\xa0', '').replace('"', '').replace("'", '').replace( '\r\n', '').replace('\n', '').replace( '\t', '').replace('\u200b', '').replace( '\\r\\n', '').replace('\\n', '').strip())) item['content'] = content if item['content'] != None: yield item
def date_video(self): # XML形式から取得した投稿日付を日付型に変換 (xml[0][4].text: %Y-%m-%dT%H:%M:%S+09:00 -> %Y-%m-%d %H:%M:%S) date = self.xml[0][4].text date = date[:-6] date = date.replace('T', ' ') date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S') return date
def zero_day(date, to_func=None, days=0, default=datetime.now): if not hasattr(date, "hour"): date = default() date = date.replace(hour=0, minute=0, second=0, microsecond=0) if days: date += days * DURATION_DAY return convert_time(date, to_func)
def rapport(request): if 'date' in request.GET: date = request.GET['date'] date = datetime.strptime(date,"%Y-%m-%d") else: date = datetime.today() lendemain = date + timedelta(days=1) # on met les deux dates a minuit date = date.replace(hour=0, minute=0, second=0) lendemain = lendemain.replace(hour=0, minute=0, second=0) ajoutes = Exemplaire.objects.all().filter(date_creation__gt=date, date_creation__lt=lendemain) factures = Facture.objects.all().filter(date_creation__gt=date, date_creation__lt=lendemain) nb_ajoutes = ajoutes.count() nb_factures = factures.count() nb_vendus = sum([f.nb_livres() for f in factures]) prix_total_vendu = sum([f.prix_total() for f in factures]) context = { 'nb_ajoutes':nb_ajoutes, 'nb_factures':nb_factures, 'date':date.date(), 'nb_vendus':nb_vendus, 'prix_total_vendu':prix_total_vendu, } return render_to_response('encefal/rapport.html', context)
def searchDate(channelDate = ""): xbmcplugin.setContent(pluginhandle, 'movies'); channel = "" date = "" params = channelDate.split('|') channel = params[0] if len(params) > 1: date = params[1] result = [] if date == "": dialog = xbmcgui.Dialog() date = dialog.numeric(1, translation(30007)) date = re.sub('[^0-9|^\/]','0',date) date = date.replace('/','.') if (channel != "") and (len(date) == 10): data = getData() for entry in data: cEntry = entry if (entry[0] == channel) and (entry[3] == date): cEntry[1] = cEntry[2]+': '+cEntry[1] result.append(cEntry) result.sort(key=lambda entry: entry[1].lower()) for entry in result: addVideo(entry) xbmcplugin.endOfDirectory(pluginhandle)
def parse_date(self, date): """ Parses string dates in format YYYY-MM-DD to datetime objects and adjusts them based on frequency. """ """ TODO: - Account for holidays """ if not isinstance(date, dt.date): date = dt.datetime.strptime(date, '%Y-%m-%d').date() if self.frequency == 'daily': day = date.weekday() if day >= 5: print('The day you chose is not a weekday.') date -= timedelta(days=day - day%4) elif self.frequency == 'weekly': day = date.weekday() if day > 0: print('Weeks start on a Monday') date -= timedelta(days=day) elif self.frequency == 'monthly': print('Month starts on the 1st') date = date.replace(day=1) return date
def getNews(url,kind): try: res = requests.get(url) soup = BeautifulSoup(res.text,"lxml") #print "URL:"+url #title = soup.title.string if kind == u'娛樂' or kind == u'日韓' or kind == u'華流': title = soup.find(id="newsTitle").text.strip() date = soup.find("div","time").text.strip() content = soup.find("div","Content2").text.strip() keywords = soup.find(attrs={"name":"keywords"})['content'].strip() else: title = soup.find("h1","news-title-3").text.strip() date = soup.find("time","page-date").text.strip() content = soup.find("div",id="Content1").text.strip() keywords = soup.find(attrs={"name":"Keywords"})['content'].strip() date=date.replace("/","-") date = datetime.strptime(date,"%Y-%m-%d %H:%M:%S") #print date kind_number = kind_dict.get(kind,0) #print kind_number while keywords.endswith(",") or keywords.endswith(" "): keywords = keywords[:-1] keywords = keywords.split(",") f = writeFile(kind_number,keywords) jieba.load_userdict(f) words = pseg.cut(content) keyword_string="" for word in words: if word.flag == 'n'and keyword_string.find(word.word)==-1: keyword_string+=word.word+"," #print word.word, word.flag keyword_string=keyword_string[:-1] source = u"三立" try: db = MySQLdb.connect(host="localhost",user="******", passwd="wkps1015",db="Crawl", charset="utf8") cursor = db.cursor() sql = "INSERT INTO news(Link, Title, Content, Kinds, Post_time, Keywords, Source) VALUES (%s,%s,%s,%s,%s,%s,%s)" cursor.execute(sql,(url, title, content , kind_number, date, keyword_string, source)) db.commit() db.close() except MySQLdb.Error as err: if 1062 in err: writeLog('E',"[Repeat] %s url=%s"%(err,url)) else: writeLog('E',"[DatabaseError] %s url=%s"%(err,url)) except AttributeError as error: writeLog('E',"[AttributeError] %s url=%s"%(error,url)) except requests.ConnectionError as error: writeLog('E',"[ConnectError] %s url=%s"%(error,url)) except requests.exceptions.ConnectionError as error: writeLog('E',"[Request_Connect] %s url=%s"%(error,url)) except Exception as error: writeLog('E',"[ERROR] %s url=%s"%(error,url))
def _get_maintenance_activity_group_by_date(date, rank): week = date - timedelta(date.weekday()) groups = Maintenance_Activity_Group.get_maintenance_activity_groups(week) if groups: if len(rank) == 5: # fixed maintenance. rank = 'HH:MM' try: dt = date.replace(hour = int(rank[0:2]), minute = int(rank[3:5])) except ValueError: # badly formatted time, should be "HH:MM" print "_get_maintenance_activity_group_by_date(", date, ",", rank, "); in resource_cal.py:" print "ValueError: badly formatted time, should be 'HH:MM'" return None # cant make anything of this, return None. def comp_mag(mag): return mag.get_start(tzname = 'ET') == dt elif len(rank) == 1 and rank.isalpha(): # floating maintenance: rank = 'A', 'B', etc. def comp_mag(mag): return TimeAgent.truncateDt(mag.get_start(tzname = 'ET')) == date and mag.rank.upper() == rank.upper() else: # whoops! rank should be either '[A-Z]' or 'HH:MM' print "_get_maintenance_activity_group_by_date(", date, ",", rank, "); in resource_cal.py:" print "poorly formated rank; must be either '[A-Z]' or 'HH:MM'." return None groups = filter(comp_mag, groups) return groups[0] if groups else None return None
async def _create_quick(self, ctx: Context, date: EventDateTime, terrain: str, faction: str, zeus: Member = None, time: EventTime = None, sideop=False, platoon_size: str = None, quiet=False): if time is not None: date = date.replace(hour=time.hour, minute=time.minute) event = await self._create_event(ctx, date, sideop=sideop, platoon_size=platoon_size, force=(time is not None), batch=True, silent=True) await self._set_quick(ctx, event, terrain, faction, zeus, quiet=True) if not quiet: await ctx.send("Created event {}".format(event)) return event
def dayLength(date, latitude): # Formulas from: http://www.gandraxa.com/length_of_day.xml # I don't really understand them, and the numbers don't quite match # what I get from calendar sites. Perhaps this is measuring the exact # moment the center of the sun goes down, as opposed to civil twilight? # But I think it's close enough to get the idea across. # Tilt of earth's axis relative to its orbital plane ("obliquity of ecliptic") axis = math.radians(23.439) # Date of winter solstice in this year. Not quite right, but good # enough for our purposes. solstice = date.replace(month=12, day=21) # If a year is a full circle, this is the angle between the solstice # and this date, in radians. May be negative if we haven't reached the # solstice yet. dateAngle = (date - solstice).days * 2 * math.pi / 365.25 latitude = math.radians(latitude) m = 1 - math.tan(latitude) * math.tan(axis * math.cos(dateAngle)) # If m is less than zero, the sun never rises; if greater than two, it never sets. m = min(2, max(0, m)) return math.acos(1 - m) / math.pi
def show_tasks(self, date=None): if date in ['today', 'tomorrow', 'yesterday']: day_date_param = self.__convert_to_date(date) task_cells = list( filter( lambda x: datetime.strptime( self.__num_suffix(x['Next Check-In']), '%d %B %Y'). date() == day_date_param, self.sheet)) if task_cells: self.__perform_send_action(task_cells) else: return { 'text': 'No task assigned to be checked in {}, try another date'. format(date) } else: date_param = date.replace('-', ' ') task_cells = list( filter(lambda x: x['Next Check-In'] == date_param, self.sheet)) if task_cells: self.__perform_send_action(task_cells) else: return { 'text': 'No task assigned to be checked in on this date, try another date' }
def dashboard(): """Display user's dashboard.""" # Retrieve form data. user_id = session["user_id"] date = datetime.now() date = date.replace(hour=0, minute=0, second=0, microsecond=0) minutes_asleep = int(request.form.get("hours_sleep")) * 60 insomnia = convert_to_boolean(request.form.get("insomnia")) insom_type = request.form.get("insom_type") insom_severity = int(request.form.get("insom_severity")) alcohol = convert_to_boolean(request.form.get("alcohol")) caffeine = convert_to_boolean(request.form.get("caffeine")) menstruation = convert_to_boolean(request.form.get("menstruation")) bedtime = datetime.strptime((request.form.get("bedtime")), '%H:%M') stress_level = int(request.form.get("stress_level")) activity_level = int(request.form.get("activity_level")) # Create new record in db if no existing record with user_id and date; # otherwise, update current record. create_or_update_record(user_id, date, minutes_asleep, insomnia, insom_type, insom_severity, alcohol, caffeine, menstruation, bedtime, stress_level, activity_level) # Pass calculated data to template return render_template("dashboard.html")
def plan(request): try: cursor = connection.cursor() cursor.execute(""" SELECT DISTINCT date FROM weight_clampliftplan ORDER BY date DESC""") query = cursor.fetchall() qlen = len(query) if len(query) == 1: qstr = str(query)[16:][:-5] else: qstr = str(query)[16:][:-4] qsplt = qstr.split('),), (datetime.date(') datelist = list() for date in qsplt: datefrm = date.replace(", ","-") datelist.append(datefrm) if 'opdate' in request.GET and request.GET['opdate']: opdate = request.GET['opdate'] else: opdate = "" except: pass return render_to_response('plan.html', locals())
def _checkParams(self): RHDisplayCategoryBase._checkParams(self) self.detail = request.args.get('detail', 'event') if self.detail not in ('event', 'session', 'contribution'): raise BadRequest('Invalid detail argument') self.period = request.args.get('period', 'day') if self.period not in ('day', 'month', 'week'): raise BadRequest('Invalid period argument') if 'date' in request.args: try: date = datetime.strptime(request.args['date'], '%Y-%m-%d') except ValueError: raise BadRequest('Invalid date argument') else: date = datetime.now() date = self.category.display_tzinfo.localize(date) date = date.replace(hour=0, minute=0, second=0, microsecond=0) if self.period == 'day': self.start_dt = date self.end_dt = self.start_dt + relativedelta(days=1) elif self.period == 'week': self.start_dt = date - relativedelta(days=date.weekday()) self.end_dt = self.start_dt + relativedelta(days=7) elif self.period == 'month': self.start_dt = date + relativedelta(day=1) self.end_dt = self.start_dt + relativedelta(months=1)
def monthdelta(date, delta): m, y = (date.month + delta) % 12, date.year + ( (date.month) + delta - 1) // 12 if not m: m = 12 d = min(date.day, calendar.monthrange(y, m)[1]) return date.replace(day=d, month=m, year=y)
def do_scrape_improved(crypto): today = dateToday() URL = 'https://coinmarketcap.com/currencies/'+str(crypto)+'/historical-data/?start=20130429&end='+str(today) req = Request(URL+str(crypto)) webpage = urlopen(req).read() soup = BeautifulSoup(webpage, 'html5lib') tbody = soup.find('tbody') rows = tbody.find_all('tr') Prices = [] for row in rows: dataPoint = {} cols = row.find_all('td') cols = [ele.text.strip() for ele in cols] date = cols[0] date = date.replace(',', '') opn = cols[1] opn = opn.replace(',','') high = cols[2] high = high.replace(',','') low = cols[3] low = low.replace(',','') close = cols[4] close = close.replace(',','') dataPoint['Open'] = float(opn) dataPoint['High'] = float(high) dataPoint['Low'] = float(low) dataPoint['Close'] = float(close) dataPoint['Date'] = date Prices.append(dataPoint) return Prices
def monthdelta(date, delta): m, y = (date.month+delta) % 12, date.year + (date.month+(delta-1)) // 12 if not m: m = 12 d = min( date.day, [31, 29 if y % 4 == 0 and not y % 400 == 0 else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][m-1]) return date.replace(day=d, month=m, year=y) - timedelta(days=1)
def _process_args(self): RHDisplayCategoryBase._process_args(self) self.detail = request.args.get('detail', 'event') if self.detail not in ('event', 'session', 'contribution'): raise BadRequest('Invalid detail argument') self.period = request.args.get('period', 'day') if self.period not in ('day', 'month', 'week'): raise BadRequest('Invalid period argument') if 'date' in request.args: try: date = datetime.strptime(request.args['date'], '%Y-%m-%d') except ValueError: raise BadRequest('Invalid date argument') else: date = datetime.now() date = self.category.display_tzinfo.localize(date) date = date.replace(hour=0, minute=0, second=0, microsecond=0) if self.period == 'day': self.start_dt = date self.end_dt = self.start_dt + relativedelta(days=1) elif self.period == 'week': self.start_dt = date - relativedelta(days=date.weekday()) self.end_dt = self.start_dt + relativedelta(days=7) elif self.period == 'month': self.start_dt = date + relativedelta(day=1) self.end_dt = self.start_dt + relativedelta(months=1)
async def convert(self, ctx: Context, arg: str) -> datetime: try: date = datetime.strptime(arg, '%Y-%m-%d') except ValueError: raise BadArgument( "Invalid date format {}. Has to be YYYY-MM-DD".format(arg)) return date.replace(hour=18, minute=30)
def courseweeklyreport(request,courseid): args=iitbxsessiondata(request) report=[] #currenttime = datetime.now().strftime("%d-%m-%Y_%H:%M:%S")\ args['refreshdate']=refreshdate=Lookup.updatedate() date=str(refreshdate) ddate= date.replace("'","") redate=ddate.replace(",","") coursename =edxcourses.objects.get(courseid=courseid).coursename args['coursename']= coursename report_name="Weekly Report of "+"_"+str(courseid)+" _ "+redate sumtotal= 0 sumstudents= 0 sumenrolled= 0 sumunenrolled= 0 sumactive= 0 sumstudents= 0 sumstaff= 0 args['courseid']=courseid sqlq='''SELECT "1" id, @sum:=@sum+query.Total as Id,week,Enrolled,Unenrolled,Total,students,staff FROM (select count(distinct idd) "Total",DATE_ADD(created, INTERVAL(7-DAYOFWEEK(created)) DAY) "week", sum(enrolled) "Enrolled", sum(unenrolled) "Unenrolled",sum(students) "students",sum(staff) "staff" FROM ( select distinct s.user_id idd, if(s.is_active=0,1,0) unenrolled, if(s.is_active=1,1,0) enrolled, date(s.created) created , if(c.role is null and is_active=1,1,0) students, if(c.role is not null,1,0)staff,s.id from student_courseenrollment s LEFT OUTER JOIN student_courseaccessrole c ON c.user_id=s.user_id and s.course_id=c.course_id where s.course_id = %s ) X group by week(created) asc ) as query CROSS JOIN (SELECT @sum :=0) as dummy ''' %('"'+courseid+'"') course_report=AuthUser.objects.raw(sqlq) for k in course_report: sumtotal+=k.Total sumenrolled+=k.Enrolled sumunenrolled+=k.Unenrolled sumstudents+=k.students sumstaff+=k.staff date = datetime.datetime.strptime(str(k.week), "%Y-%m-%d").strftime("%d-%m-%Y") report.append([str(date),str(k.Enrolled),str(k.Unenrolled),str(k.Total),int(k.Id),k.students,k.staff]) args["report"]=report.reverse() args['institutename']=request.session['institutename'] args['rcid']= request.session['rcid'] args['email']=request.user args["report"]=report args["sumtotal"]=sumtotal args["sumenrolled"]=sumenrolled args["sumunenrolled"]=sumunenrolled args["sumstudents"]=sumstudents args["sumstaff"]=sumstaff args["report_name"]=report_name return render_to_response('managerapp/courseweeklyreport.html',args)
def move_ucr_data_into_aggregation_tables(date=None, intervals=2): date = date or datetime.utcnow().date() monthly_dates = [] first_day_of_month = date.replace(day=1) for interval in range(intervals - 1, 0, -1): # calculate the last day of the previous months to send to the aggregation script first_day_next_month = first_day_of_month - relativedelta( months=interval - 1) monthly_dates.append(first_day_next_month - relativedelta(days=1)) monthly_dates.append(date) if hasattr(settings, "ICDS_UCR_DATABASE_ALIAS") and settings.ICDS_UCR_DATABASE_ALIAS: with connections[settings.ICDS_UCR_DATABASE_ALIAS].cursor() as cursor: _create_aggregate_functions(cursor) _update_aggregate_locations_tables(cursor) aggregation_tasks = [] for monthly_date in monthly_dates: calculation_date = monthly_date.strftime('%Y-%m-%d') aggregation_tasks.append( UCRAggregationTask('monthly', calculation_date)) aggregation_tasks.append( UCRAggregationTask('daily', date.strftime('%Y-%m-%d'))) aggregate_tables.delay(aggregation_tasks[0], aggregation_tasks[1:])
def emploi(): name = inspect.stack()[0][3] last_item = get_last_element(name) link = "" doc.add(h2("emploi maroc")) base_url = "https://www.emploi.ma" url = base_url + "/recherche-jobs-maroc/?f%5B0%5D=im_field_offre_region%3A64" list = ul() soup = html_code(url) job_elems = soup.select(".job-description-wrapper") link = "" for elem in job_elems: date = elem.select_one(".job-recruiter").text date = date.split(" ")[0] date = date.replace(".", "/") if (date == today or date == yesterday): elem = elem.select_one("h5 a") title = elem.text link = elem["href"] if (link == last_item): list += li("---------------") list += li(a(title, href=base_url + link), __pretty=False) last_item = base_url + job_elems[0].select_one("h5 a")["href"] set_last_element(last_item, name) doc.add(list)
def nice_date(date): if date is not None: for month in month_list: if month in date: return date.replace(month, month_list_french[month_list.index(month)]) return date
def simulate(): ''' Simulate observations. ''' dates = find_dates_to_observe() s = scheduler.Scheduler() f = open(params_simulator.params['simulate_dates_file'], 'wb') for date in dates: print print 'START NIGHT:', date f.write(date.replace('-', '') + '\n') # skip if already exists (was computed during the previous simulations) #~ if os.path.isdir("/home/el")): #~ continue # Thin clouds. Observe only bright stars --> magnitude limit. mag_limit = True s.observing_plan(date=date, remove_twilight=True, bright_time=True) # Seeing #~ seeing=random.gauss(2.0, 1.0) # Maybe gauss is not the best distribution f.close()
def map(date): api_date = date.replace('-', '') validate_date(api_date) return render_template("map.html", date=date)
def _convert_to_utc(date, mask='%Y-%m-%d %H:%M:%S'): utc = pytz.utc eastern = pytz.timezone('US/Eastern') date_ = datetime.datetime.strptime(date.replace(" 0:", " 12:"), mask) date_eastern = eastern.localize(date_, is_dst=None) date_utc = date_eastern.astimezone(utc) return date_utc.strftime('%Y-%m-%d %H:%M:%S %Z%z')
def render_news(request, date, index): date = date.replace("/", "-") item = Newsitem.objects.get(date=date) revision = item.get_latest() args = {"revision": revision, "item": item} return render_to_response("full_news.html", args, context_instance=RequestContext(request))
def show_tasks(self, date=None): if date in ['today', 'tomorrow', 'yesterday']: day_date_param = self._convert_to_date(date) task_cells = list( filter( lambda x: datetime.strptime( self._num_suffix(x['Next Check-In']), '%d %B %Y').date( ) == day_date_param, self.sheet)) if task_cells: self._perform_send_action(task_cells) else: return { 'text': 'No task assigned to be checked in {}, try another date'. format(date) } # below elif statement to be used to check if passed in param matches the desired format {dth-month-yyyy} # elif re.match('desired_format{dth-month-yyyy}', date): else: date_param = date.replace('-', ' ') task_cells = list( filter(lambda x: x['Next Check-In'] == date_param, self.sheet)) if task_cells: self._perform_send_action(task_cells) else: return { 'text': 'No task assigned to be checked in on this date, try another date' }
def get_month_day_range(date): """ For a date 'date' returns the start and end date for the month of 'date'. Month with 31 days: >>> date = datetime.date(2011, 7, 27) >>> get_month_day_range(date) (datetime.date(2011, 7, 1), datetime.date(2011, 7, 31)) Month with 28 days: >>> date = datetime.date(2011, 2, 15) >>> get_month_day_range(date) (datetime.date(2011, 2, 1), datetime.date(2011, 2, 28)) """ first_day = date.replace(day = 1) last_day = date.replace(day = calendar.monthrange(date.year, date.month)[1]) return first_day, last_day
def render_news(request, date, index): date = date.replace('/', '-') item = Newsitem.objects.get(date=date) revision = item.get_latest() args = {'revision': revision, 'item': item} return render_to_response('full_news.html', args, context_instance=RequestContext(request))
def inc_month(date, count): y, m, d = date.year, date.month, date.day m += count y += (m - 1) // 12 m = ((m - 1) % 12) + 1 days_in_month = monthrange(y, m)[1] d = min(d, days_in_month) return date.replace(year=y, month=m, day=d)
def timezone_now(self, tzinfo=settings.CONVERT_TIME_ZONE, midnight=False): ''' Gives the current IST time ''' to_zone = tz.gettz(tzinfo) date = dt.now(to_zone) if midnight and date: date = date.replace(hour=0, minute=0, second=0, microsecond=0) return date
def ru_months(date, simple=None): try: return [ date.replace(month[0], (simple and month[1] or month[2])) for month in months if date.find(month[0]) != -1 ][0] except IndexError: return ''
def subtract_date(date, year=0, month=0): year, month = divmod(year*12 + month, 12) if date.month <= month: year = date.year - year - 1 month = date.month - month + 12 else: year = date.year - year month = date.month - month return date.replace(year = year, month = month)
def write_pagetitles_xml(period, date, data): date = date.replace(',','-') filename = 'pagetitles-' + period + '-' + date pagetitles_xml_file = open(filename + '.xml','w') pagetitles_xml_file.write(data) pagetitles_xml_file.close() print 'The data already stored in %s' % filename return filename
def get_list(self): for tr in self.document.getroot().xpath('//tr[@class="patFuncEntry"]'): id = tr.xpath('td/input')[0].attrib["value"] book = Book(id) bigtitle = tr.xpath('td[@class="patFuncTitle"]/label/a')[0].text book.name = bigtitle.split('/')[0] book.author = bigtitle.split('/')[1] date = tr.xpath('td[@class="patFuncStatus"]')[0].text book.date = txt2date(date.replace('RETOUR', '')) yield book