def _thai_strftime(datetime, fmt_c): text = "" if fmt_c == "a": # abbreviated weekday text = _TH_ABBR_WEEKDAYS[datetime.weekday()] elif fmt_c == "A": # full weekday text = _TH_FULL_WEEKDAYS[datetime.weekday()] elif fmt_c == "b": # abbreviated month text = _TH_ABBR_MONTHS[datetime.month - 1] elif fmt_c == "B": # full month text = _TH_FULL_MONTHS[datetime.month - 1] elif fmt_c == "y": # # year without century text = str(datetime.year + 543)[2:4] elif fmt_c == "Y": # year with century text = str(datetime.year + 543) elif fmt_c == "c": # Wed 6 Oct 01:40:00 1976 # พ 6 ต.ค. 01:40:00 2519 <-- left-aligned weekday, right-aligned day text = "{:<2} {:>2} {} {} {}".format( _TH_ABBR_WEEKDAYS[datetime.weekday()], datetime.day, _TH_ABBR_MONTHS[datetime.month - 1], datetime.strftime("%H:%M:%S"), datetime.year + 543, ) elif fmt_c == "v": # undocumented format: ' 6-Oct-1976' text = "{:>2}-{}-{}".format( datetime.day, _TH_ABBR_MONTHS[datetime.month - 1], datetime.year + 543 ) else: # matched with nothing text = datetime.strftime("%{}".format(fmt_c)) return text
def judge_business_day(datetime, data): not_weekend = datetime.weekday() is not 5 and datetime.weekday() is not 6 not_holiday = str(datetime.date()) not in data["holidays"] if not_weekend and not_holiday: return True else: return False
def CheckDatetime(datetime): if datetime.weekday() == 6: if datetime.hour >= 9: return True else: return False elif datetime.weekday() == 4: if datetime.hour < 21: return True else: return False elif datetime.weekday() < 4: return True elif datetime.weekday() == 5: return False
def _isdst(self, dt): tt = (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.weekday(), 0, 0) stamp = _time.mktime(tt) tt = _time.localtime(stamp) return tt.tm_isdst > 0
def day_of_week_xxx(datetime) -> str: """ :param datetime.datetime datetime: :rtype: str """ days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] return days[datetime.weekday()]
def FridaysofMonth(year: int, month: int) -> List[int]: resDates = [] c = calendar.Calendar(firstweekday=calendar.SUNDAY) for dt in c.itermonthdates(year, month): if (dt.weekday() == 4 and dt.month == month): resDates.append(dt.day) return resDates
def createTimeFeatures(data,features_to_create=["EPOCH","START_OF_DAY","MONTH","WEEKDAY","HOLIDAY","DAY_OFF"], delete = True): """ A function to create different time features. The parameter features_to_create enables to select only the features we want to add. :param data: a dataframe with a DATE field. :param features_to_create: an array of strings like default ["EPOCH","START_OF_DAY","MONTH","WEEKDAY"] :type data: pandas.core.frame.DataFrame. :return: data: the modified dataframe. """ data.insert(0, "DATE_TIME", data["DATE"].apply(toDatetime)); calHoliday = createHolidayCalendar(PATH_HOLIDAY); calDayOff = createHolidayCalendar(PATH_DAYOFF); # aF stands for applyFunction # c stands for categorical variable features = { "EPOCH":{'aF' : datetimeToEpoch, 'type':'int'}, "START_OF_DAY":{'aF' : datetimeToStartOfDay, 'type':'int'}, "MONTH": {'aF' : lambda datetime: datetime.month, 'type':'category'}, "WEEKDAY": {'aF' : lambda datetime: datetime.weekday(), 'type':'category'}, "HOLIDAY": {'aF' : lambda datetime: isInHoliday(calHoliday,datetime), 'type':'category'}, "DAY_OFF": {'aF' : lambda datetime: isInHoliday(calDayOff,datetime), 'type':'category'} } i = 1 # Just for locating features for key, aF, type in [(i,features[i]['aF'],features[i]['type']) for i in features_to_create]: data.insert(i, key, data["DATE_TIME"].apply(aF)); if(type): data[key]= data[key].astype(type) i += 1 if(delete): del data["DATE"] del data["DATE_TIME"]# We no longer need that initial feature return data
def service_periods(self, datetime): datetimestr = datetime.strftime( "%Y%m%d" ) #datetime to string like "20081225" datetimeint = int(datetimestr) #int like 20081225. These ints have the same ordering as regular dates, so comparison operators work # Get the gtfs date range. If the datetime is out of the range, no service periods are in effect start_date, end_date = self.date_range() if datetime < start_date or datetime > end_date: return [] # Use the day-of-week name to query for all service periods that run on that day dow_name = self.DOW_INDEX[datetime.weekday()] service_periods = list( self.execute( "SELECT service_id, start_date, end_date FROM calendar WHERE %s=1"%dow_name ) ) # Exclude service periods whose range does not include this datetime service_periods = [x for x in service_periods if (int(x[1]) <= datetimeint and int(x[2]) >= datetimeint)] # Cut service periods down to service IDs sids = set( [x[0] for x in service_periods] ) # For each exception on the given datetime, add or remove service_id to the accumulating list for exception_sid, exception_type in self.execute( "select service_id, exception_type from calendar_dates WHERE date = ?", (datetimestr,) ): if exception_type == 1: sids.add( exception_sid ) elif exception_type == 2: if exception_sid in sids: sids.remove( exception_sid ) return list(sids)
def get_order_for(self, datetime): # Is there an override ? override = self.get_override_for(datetime) if override: return override schedule = self.get_schedule(datetime.weekday()) return schedule.get_order_for(datetime.time())
def date_converter(datetime): week_day = datetime.weekday() # monday is 0, sunday is 6 elapsed_time = math.ceil(datetime.time().microsecond / 1000) +\ datetime.time().second * (10 ** 3) +\ datetime.time().minute * (60 * (10 ** 6)) +\ datetime.time().hour * (60 * 60 * (10 ** 6)) return elapsed_time
def add_weekdays(df): '''adds weekday numbers 0-6 to dataframe''' weekdays = [] for index, row in df.iterrows(): datetime = row['datetime'] day = datetime.weekday() weekdays.append(day) df['weekdays'] = weekdays
def datedim_values(self, dt): # Calculate number of minute into the day (eg. 1016) num_minutes = dt.hour * 60. + dt.minute # Time of the start of the bin time_bin = math.floor(num_minutes / self.minutes_per_bin) # eg. 1005 hour_bin = math.floor(num_minutes / 60.) # eg. 16 min_bin = (time_bin * self.minutes_per_bin) % 60 # eg. 45 # Get a floating point representation of the center of the time bin time_num = (hour_bin * 60. + min_bin + self.minutes_per_bin / 2.0) / ( 60. * 24.) # eg. 0.7065972222222222 time_cos = math.cos(time_num * 2 * math.pi) time_sin = math.sin(time_num * 2 * math.pi) #get time_cat time_cat = "%02d:%02d" % (hour_bin, min_bin) # eg. "16:45" day_of_week = dt.weekday() day_of_year = dt.timetuple().tm_yday day_cat = self.day_to_str[day_of_week] day_num = (day_of_week + time_num) / 7. day_cos = math.cos(day_num * 2. * math.pi) day_sin = math.sin(day_num * 2. * math.pi) year = dt.year month = dt.month day = dt.day weekend = 0 if day_of_week in [5, 6]: weekend = 1 holid = 0 if dt in self.holidays: holid = 1 return { "year": year, "month": month, "day": day, "day_of_week": day_of_week, "day_of_year": day_of_year, "weekend": weekend, "holiday": holid, "day_cat": day_cat, "day_num": day_num, "day_cos": day_cos, "day_sin": day_sin, "time_cat": time_cat, "time_bin": time_bin, "hour_bin": hour_bin, "min_bin": min_bin, "time_num": time_num, "time_cos": time_cos, "time_sin": time_sin, }
def doorcode4(self, fecha): # Calculate de Door Code... need a date in String format "%Y-%m-%d" compan = self.env.user.company_id d = datetime.strptime(fecha, DEFAULT_SERVER_DATE_FORMAT) dia_semana = datetime.weekday(d) # Dias a restar y ponerlo en lunes d = d - timedelta(days=dia_semana) dtxt = d.strftime('%s.%%06d') % d.microsecond dtxt = compan.precode + dtxt[4:8] + compan.postcode return dtxt
class ReasearchStand(models.Model): date = models.TimeField(auto_now_add=True) route = models.ForeignKey(Route) weekday = models.IntegerField(default=datetime.weekday(datetime.now())) month = models.IntegerField(default=datetime.now().month) def __str__(self): return str(self.date)
def is_holiday(dt): ''' Function to check if a data is a holiday args: dt(datetime.date): Given data except: None returns: True/False(boolean): True if holiday else False ''' if dt.weekday() == 5 or dt.weekday() == 6 or dt in holidays: return True else: return False
def check_code(self): entrada = datetime.strptime(self.date_start, DEFAULT_SERVER_DATE_FORMAT) if datetime.weekday(entrada) == 0: entrada = entrada + timedelta(days=1) salida = datetime.strptime(self.date_end, DEFAULT_SERVER_DATE_FORMAT) if datetime.weekday(salida) == 0: salida = salida - timedelta(days=1) codes = (u'Código de entrada: ' + '<strong><span style="font-size: 2em;">' + self.doorcode4(self.date_start) + '</span></strong>') while entrada <= salida: if datetime.weekday(entrada) == 0: codes += ("<br>" + u'Cambiará el Lunes ' + datetime.strftime(entrada, "%d-%m-%Y") + ' a: <strong><span style="font-size: 2em;">' + self.doorcode4(datetime.strftime( entrada, "%Y-%m-%d")) + '</span></strong>') entrada = entrada + timedelta(days=1) return self.write({'door_code': codes})
def returnWeekday(datetime): weekdays = { 0: "Monday", 1: "Tuesday", 2: "Wednesday", 3: "Thursday", 4: "Friday", 5: "Saturday", 6: "Sunday" } # takes a datetime object and returns a string of the weekday return weekdays[datetime.weekday()]
class Seasonality: type_to_period = { "weekday": 7, "weekofyear": 53, "month": 12, "weekofmonth": 5, "hour": 24, } type_datetime_to_int_func = { "weekday": lambda dt: dt.weekday(), "weekofyear": lambda dt: dt.isocalendar()[1] - 1, "month": lambda dt: dt.month - 1, "weekofmonth": lambda dt: dt.day // 7, "hour": lambda dt: dt.hour } def __init__(self, seasonality_type): assert seasonality_type in self.type_to_period, \ "Invalid seasonality! Needs to be in %s!" % self.type_to_period.keys() self.seasonality_type = seasonality_type self.seasonal_period = self.type_to_period[self.seasonality_type] self._datetime_to_int_func = self.type_datetime_to_int_func[ self.seasonality_type] def datetime_to_array(self, arr): return np.vectorize(self._datetime_to_int_func)(arr) def _get_next_x(self, last_date, increment, n_next): new_dates = last_date + increment * np.arange(1, n_next + 1) return self.datetime_to_array(new_dates) def apply_decoder(self, apply_to): return apply_to / self._decoder def apply_encoder(self, apply_to): return apply_to * self._encoder def get_model_inputs(self): return self._inputs def plot_seasonality(self): import matplotlib.pyplot as plt weights = self.get_weights() if len(weights.shape) > 1 and weights.shape[1] == self.seasonal_period: weights = weights.T plt.figure() plt.plot(weights) plt.title(self.seasonality_type, fontsize=15)
def getMatsuba(self): self.dateList = [] self.res2 = [] self.res3 = [] dt = self.startDate while dt <= self.endDate: if os.path.exists(filePath + dt.strftime("%Y_%m_%d")): # folder exists? fileList = os.listdir(filePath + dt.strftime("%Y_%m_%d")) # get all files for name in fileList: if name[0:len(self.name)] == self.name[ 0:len(self.name)] and len(name) < len( self.name) + 21 and len(name) > len( self.name) + 5: # get the specific file writeLog("Read: " + dt.strftime("%Y_%m_%d") + '\\' + name) print "Read: " + dt.strftime("%Y_%m_%d") + '\\' + name book = xlrd.open_workbook(filePath + dt.strftime("%Y_%m_%d") + '\\' + name) sheet = book.sheet_by_index(0) i = 0 while sheet.cell_type(i, 0) != 0: i += 1 date = sheet.cell(i - 1, 0).value DATE = datetime(*xlrd.xldate_as_tuple(date, 0)) if datetime.weekday(DATE) == 4: DATE = DATE + timedelta(2) DATE = pd.to_datetime(DATE, format='%Y-%m-%d') if DATE in self.dateList: pos = self.dateList.index(DATE) if self.res2[pos][0] == DATE: self.res2[pos] = (DATE, sheet.cell(i, 19).value, sheet.cell(i, 22).value) if self.res3[pos][0] == DATE: self.res3[pos] = (DATE, sheet.cell(i, 20).value, sheet.cell(i, 23).value) else: self.dateList.append(DATE) self.res2.append((DATE, sheet.cell(i, 19).value, sheet.cell(i, 22).value)) self.res3.append((DATE, sheet.cell(i, 20).value, sheet.cell(i, 23).value)) dt += timedelta(1)
def change_rf_band_list(self, rfbandList): import datetime datetime = datetime.datetime.today() # note Monday is 0, Tuesday is 1, Sunday is 6 dayInteger = int(datetime.weekday()) listIndex = dayInteger % (len(rfbandList)) singleBandUnderTest = rfbandList[listIndex] singleBandList = [singleBandUnderTest] return singleBandList
def date_converter(Startdate): week_day = {0: "Monday",1: "Tuesday",2: "Wednesday",3: "Thursday",4: "Friday",5: "Saturday",6: "Sunday"} if Startdate is 'NaN': pass else: datee, time = Startdate.split(' ') month, day, year = datee.split('/') hour, minute = time.split(':') w_date=datetime(int(year),int(month),int(day),int(hour),int(minute)) w1=datetime.weekday(datetime(int(year),int(month),int(day),int(hour),int(minute))) weekday=week_day[w1] with open("Washington_new1.csv", 'a+') as wfile1: wfile1.write(month + "\n") with open("Washington_new2.csv", 'a+') as wfile2: wfile2.write(year + "\n") with open("Washington_new3.csv", 'a+') as wfile3: wfile3.write(weekday + "\n")
def service_periods(self, datetime): # Get the gtfs date range. If the datetime is out of the range, no service periods are in effect start_date, end_date = self.date_range() if datetime < start_date or datetime > end_date: return [] # Use the day-of-week name to query for all service periods that run on that day dow_name = self.DOW_INDEX[datetime.weekday()] sids = set( [x[0] for x in self.execute( "SELECT * FROM calendar WHERE %s=1"%dow_name )] ) # For each exception on the given datetime, add or remove service_id to the accumulating list datetimestr = datetime.strftime( "%Y%m%d" ) for exception_sid, exception_type in self.execute( "select service_id, exception_type from calendar_dates WHERE date = ?", (datetimestr,) ): if exception_type == 1: sids.add( exception_sid ) elif exception_type == 2: if exception_sid in sids: sids.remove( exception_sid ) return list(sids)
def service_periods(self, datetime): datetimestr = datetime.strftime( "%Y%m%d") #datetime to string like "20081225" datetimeint = int( datetimestr ) #int like 20081225. These ints have the same ordering as regular dates, so comparison operators work # Get the gtfs date range. If the datetime is out of the range, no service periods are in effect start_date, end_date = self.date_range() if datetime < start_date or datetime > end_date: return [] # Use the day-of-week name to query for all service periods that run on that day dow_name = self.DOW_INDEX[datetime.weekday()] service_periods = list( self.execute( "SELECT service_id, start_date, end_date FROM calendar WHERE %s=1" % dow_name)) # Exclude service periods whose range does not include this datetime service_periods = [ x for x in service_periods if (int(x[1]) <= datetimeint and int(x[2]) >= datetimeint) ] # Cut service periods down to service IDs sids = set([x[0] for x in service_periods]) # For each exception on the given datetime, add or remove service_id to the accumulating list for exception_sid, exception_type in self.execute( "select service_id, exception_type from calendar_dates WHERE date = ?", (datetimestr, )): if exception_type == 1: sids.add(exception_sid) elif exception_type == 2: if exception_sid in sids: sids.remove(exception_sid) return list(sids)
def digest(self): # 找出这周的起始和结束日 year, week = datetime.datetime.now().isocalendar()[0:2] date = datetime.date(year, 1, 1) if (date.weekday() > 3): date = date + datetime.timedelta(days=7 - datetime.weekday()) else: date = date - datetime.timedelta(days=date.weekday()) delta = datetime.timedelta(days=(week - 1) * 7) start, end = date + delta, date + delta + datetime.timedelta(days=6) posts = Post.query.filter( Post.publish_date >= start, Post.publish_date <= end ).all() if (len(posts) == 0): return send( reminder.emial, '', 'digest', {'posts': posts} )
class Manager: datetime = datetime.datetime.today() weekday = datetime.weekday() dayname = Day(weekday).name year = datetime.year month = datetime.month monthDays = monthrange(year, month)[1] pattern = Pattern() weekCost = float("0.0") businessDays = 0 def __init__(self): self.pattern.updateCosts() self.calculateBusinessDays() def calculateBusinessDays(self): i = 1 while i <= self.monthDays: wday = datetime.datetime(self.year, self.month, i).weekday() if Manager.isBusinessDay(wday): self.businessDays += 1 i += 1 def isBusinessDay(value): if value == 0 or value == 1 or value == 2 or value == 3 or value == 4: return True else: return False def calculateMonth(self): weekCost = float("0.0") for var in self.pattern.dictionary: weekCost = weekCost + float(self.pattern.dictionary[var]) self.weekCost = weekCost
def _thai_strftime(datetime: datetime.datetime, fmt_char: str) -> str: """Conversion support for thai_strftime().""" str_ = "" if fmt_char == "A": # National representation of the full weekday name str_ = thai_full_weekdays[datetime.weekday()] elif fmt_char == "a": # National representation of the abbreviated weekday str_ = thai_abbr_weekdays[datetime.weekday()] elif fmt_char == "B": # National representation of the full month name str_ = thai_full_months[datetime.month - 1] elif fmt_char == "b": # National representation of the abbreviated month name str_ = thai_abbr_months[datetime.month - 1] elif fmt_char == "C": # Thai Buddhist century (AD+543)/100 + 1 as decimal number; str_ = str(int((datetime.year + _BE_AD_DIFFERENCE) / 100) + 1) elif fmt_char == "c": # Locale’s appropriate date and time representation # Wed 6 Oct 01:40:00 1976 # พ 6 ต.ค. 01:40:00 2519 <-- left-aligned weekday, right-aligned day str_ = "{:<2} {:>2} {} {} {}".format( thai_abbr_weekdays[datetime.weekday()], datetime.day, thai_abbr_months[datetime.month - 1], datetime.strftime("%H:%M:%S"), datetime.year + _BE_AD_DIFFERENCE, ) elif fmt_char == "D": # Equivalent to ``%m/%d/%y'' str_ = "{}/{}".format( datetime.strftime("%m/%d"), str(datetime.year + _BE_AD_DIFFERENCE)[-2:], ) elif fmt_char == "F": # Equivalent to ``%Y-%m-%d'' str_ = "{}-{}".format(str(datetime.year + _BE_AD_DIFFERENCE), datetime.strftime("%m-%d")) elif fmt_char == "G": # ISO 8601 year with century representing the year that contains the # greater part of the ISO week (%V). Monday as the first day of the week. str_ = str(int(datetime.strftime("%G")) + _BE_AD_DIFFERENCE) elif fmt_char == "g": # Same year as in ``%G'', but as a decimal number without century (00-99). str_ = str(int(datetime.strftime("%G")) + _BE_AD_DIFFERENCE)[-2:] elif fmt_char == "v": # BSD extension, ' 6-Oct-1976' str_ = "{:>2}-{}-{}".format( datetime.day, thai_abbr_months[datetime.month - 1], datetime.year + _BE_AD_DIFFERENCE, ) elif fmt_char == "X": # Locale’s appropriate time representation. str_ = datetime.strftime("%H:%M:%S") elif fmt_char == "x": # Locale’s appropriate date representation. str_ = "{}/{}/{}".format( _padding(datetime.day), _padding(datetime.month), datetime.year + _BE_AD_DIFFERENCE, ) elif fmt_char == "Y": # Year with century str_ = str(datetime.year + _BE_AD_DIFFERENCE) elif fmt_char == "y": # Year without century str_ = str(datetime.year + _BE_AD_DIFFERENCE)[2:4] elif fmt_char == "+": # National representation of the date and time # (the format is similar to that produced by date(1)) # Wed 6 Oct 1976 01:40:00 str_ = "{:<2} {:>2} {} {} {}".format( thai_abbr_weekdays[datetime.weekday()], datetime.day, thai_abbr_months[datetime.month - 1], datetime.year + _BE_AD_DIFFERENCE, datetime.strftime("%H:%M:%S"), ) else: # No known localization available, use Python's default str_ = datetime.strftime(f"%{fmt_char}") return str_
supername = ws.cell(row=menurow, column=1).value if supername == None: continue supermenu.append(supername) supermenu = set(supermenu) supermenuli = list(supermenu) for superi in supermenuli: #supermenudict[superi]={} for rowi in range(2, ws.max_row + 1): if ws.cell(row=rowi, column=1).value == superi: prodname = ws.cell(row=rowi, column=2).value supermenudict.setdefault(superi, []).append(prodname) #供应商菜单字典 superlist, shopsupdict, shopwasdict = shopyesdaysale() shopprodict = shopsupprosale() tomorrow = datetime.today() + timedelta(days=1) weekday = datetime.weekday(tomorrow) weekday = int(weekday) + 1 #weekday=3 if weekday in [2, 5]: #供应商每天预定量 for shopkey in shopsupdict: for supk in shopsupdict[shopkey]: if shopkey in shopwasdict: if supk in shopwasdict[shopkey]: #if shopsupdict[shopkey][supk]>6: # shopsupdict[shopkey][supk]=shopsupdict[shopkey][supk]-3 # if shopsupdict[shopkey][supk]>4: #shopsupdict[shopkey][supk]=shopsupdict[shopkey][supk]-2 #else: shopsupdict[shopkey][ supk] = shopsupdict[shopkey][supk] - 1 else:
def is_tercer_viernes(dt): return dt.weekday() == 4 and 15 <= dt.day <= 21
def first_sunday_on_or_after(dt): days_to_go = 6 - dt.weekday() if days_to_go: dt += timedelta(days_to_go) return dt
print("Desglose de la fecha", fecha2, ":") tupla_mensajes = ("Año", "Núm. semana", "Núm. día de semana") tupla_valores = datetime.isocalendar(fecha2) tupla_diassem = ("Lun", "Mar", "Mié", "Jue", "Vie", "Sáb", "Dom") for mensaje, valor in zip(tupla_mensajes, tupla_valores): print(mensaje, "-->", valor) print("Día de semana-->", tupla_diassem[tupla_valores[2] - 1]) print(input(" continuar?")) print("Obtener día de la semana por su número ") print( "La función weekday() devuelve el número de día de la semana a que corresponda la fecha indicada, según los siguientes valores por día: 0-Lunes, 1-Martes, 2-Miércoles, 3-Jueves, 4-Viernes , 5-Sábado y 6-Domingo" ) dia_semana = datetime.weekday(fecha1) print(fecha1, "->", dia_semana, "->", tupla_diassem[dia_semana]) print("Obtener y contar los días que sean martes entre dos fechas") print(input(" continuar?")) limpiar() print("#########################################################") from datetime import datetime, timedelta formato = "%d/%m/%Y" contador = 0 fechadesde = input('Fecha desde (dd/mm/aaaa): ') fechahasta = input('Fecha hasta (dd/mm/aaaa): ') if fechadesde == '' or fechahasta == '': exit()
def _thai_strftime(datetime: datetime.datetime, fmt_char: str) -> str: """ Conversion support for thai_strftime() """ str_ = "" if fmt_char == "A": # National representation of the full weekday name str_ = thai_full_weekdays[datetime.weekday()] elif fmt_char == "a": # National representation of the abbreviated weekday str_ = thai_abbr_weekdays[datetime.weekday()] elif fmt_char == "B": # National representation of the full month name str_ = thai_full_months[datetime.month - 1] elif fmt_char == "b": # National representation of the abbreviated month name str_ = thai_abbr_months[datetime.month - 1] elif fmt_char == "C": # Thai Buddhist century (AD+543)/100 + 1 as decimal number; str_ = str(int((datetime.year + 543) / 100) + 1) elif fmt_char == "c": # Locale’s appropriate date and time representation # Wed 6 Oct 01:40:00 1976 # พ 6 ต.ค. 01:40:00 2519 <-- left-aligned weekday, right-aligned day str_ = "{:<2} {:>2} {} {} {}".format( thai_abbr_weekdays[datetime.weekday()], datetime.day, thai_abbr_months[datetime.month - 1], datetime.strftime("%H:%M:%S"), datetime.year + 543, ) elif fmt_char == "D": # Equivalent to ``%m/%d/%y'' str_ = "{}/{}".format(datetime.strftime("%m/%d"), str(datetime.year + 543)[-2:]) elif fmt_char == "F": # Equivalent to ``%Y-%m-%d'' str_ = "{}-{}".format(str(datetime.year + 543), datetime.strftime("%m-%d")) elif fmt_char == "G": # ISO 8601 year with century representing the year that contains the greater part of the ISO week (%V). Monday as the first day of the week. str_ = str(int(datetime.strftime("%G")) + 543) elif fmt_char == "g": # Same year as in ``%G'', but as a decimal number without century (00-99). str_ = str(int(datetime.strftime("%G")) + 543)[-2:] elif fmt_char == "v": # BSD extension, ' 6-Oct-1976' str_ = "{:>2}-{}-{}".format( datetime.day, thai_abbr_months[datetime.month - 1], datetime.year + 543 ) elif fmt_char == "X": # Locale’s appropriate time representation. str_ = datetime.strftime("%H:%M:%S") elif fmt_char == "x": # Locale’s appropriate date representation. str_ = "{}/{}/{}".format( _padding(datetime.day), _padding(datetime.month), datetime.year + 543 ) elif fmt_char == "Y": # Year with century str_ = str(datetime.year + 543) elif fmt_char == "y": # Year without century str_ = str(datetime.year + 543)[2:4] elif fmt_char == "+": # National representation of the date and time (the format is similar to that produced by date(1)) # Wed 6 Oct 1976 01:40:00 str_ = "{:<2} {:>2} {} {} {}".format( thai_abbr_weekdays[datetime.weekday()], datetime.day, thai_abbr_months[datetime.month - 1], datetime.year + 543, datetime.strftime("%H:%M:%S"), ) else: # No known localization available, use Python's default str_ = datetime.strftime(f"%{fmt_char}") return str_
def is_holiday(dt): if dt.weekday() == 5 or dt.weekday() == 6 or dt in holidays: return True else: return False
def get_weekday(datetime=None): if not datetime: datetime = now_datetime() weekdays = get_weekdays() return weekdays[datetime.weekday()]
def parse_xml_to_flat_dict(request, datetime, lat, lon, a_dt, dist): response_dict = xmltodict.parse(request.content) parsed_result = {} parsed_list = ['location_lat', 'location_long', 'temperature', 'dew_point', 'wind_speed', 'wind_direction', 'cloud_cover_amount', 'snow_amount', 'humidity'] for para in parsed_list: parsed_result.setdefault(para, 0) parsed_result['day'] = datetime.weekday() parsed_result['day_of_month'] = datetime.day parsed_result['dep_time'] = datetime.strftime('%H%M') parsed_result['arr_time'] = a_dt.strftime('%H%M') parsed_result['elapsed_time'] = 60 parsed_result['distance'] = dist parsed_result['location_lat'] = lat parsed_result['location_long'] = lon try: response_data = response_dict['dwml']['data'] response_params = response_data['parameters'] except KeyError: print 'Exception {}'.format(parsed_result) return parsed_result def check_if_value_exists(dict): return 'value' in dict try: if 'temperature' in response_params: t = response_params['temperature'] if t[0] and check_if_value_exists(t[0]): parsed_result['temperature'] = response_params['temperature'][0]['value'] if t[1] and check_if_value_exists(t[1]): parsed_result['dew_point'] = response_params['temperature'][1]['value'] except: pass if 'wind-speed' in response_params and check_if_value_exists(response_params['wind-speed']): parsed_result['wind_speed'] = response_params['wind-speed']['value'] if 'direction' in response_params and check_if_value_exists(response_params['direction']): parsed_result['wind_direction'] = response_params['direction']['value'] if 'cloud-amount' in response_params and check_if_value_exists(response_params['cloud-amount']): parsed_result['cloud_cover_amount'] = response_params['cloud-amount']['value'] if 'precipitation' in response_params and check_if_value_exists(response_params['precipitation']): parsed_result['snow_amount'] = response_params['precipitation']['value'] # if 'convective-hazard' in response_params: # ch = response_params['convective-hazard'] # if ch[0] and ch[0]['severe-component'] and check_if_value_exists(ch[0]['severe-component']): # parsed_result['probability_severe_thunderstorm'] = response_params['convective-hazard'][0]['severe-component']['value'] # if ch[1] and ch[1]['severe-component'] and check_if_value_exists(ch[1]['severe-component']): # parsed_result['probability_extreme_severe_thunderstorm'] = response_params['convective-hazard'][1]['severe-component'][ # 'value'] if 'humidity' in response_params and check_if_value_exists(response_params['humidity']): parsed_result['humidity'] = response_params['humidity']['value'] print parsed_result return parsed_result
def find_day(timestamp): # print(timestamp) datetime = pd.to_datetime(timestamp) # print(datetime.weekday()) return datetime.weekday()
def getDayIdByDateTime(self, datetime): # monday is 0, but in database is 1 so plus 1 return datetime.weekday() + 1
def generate_data(tsv_file, remains=True, save_as_file=True): city_id = int(tsv_file.split(sep='.')[0]) origin_dataset_file_path = os.path.join(config.origin_dataset_dir, tsv_file) holidays = config.holidays end_of_holidays = config.end_of_holidays weekend_weekdays = config.weekend_weekdays # 预处理小时粒度的数据 df = pd.read_csv(origin_dataset_file_path, names=["city_id", "datetime", "count"], sep="\t") df['datetime'] = df.datetime.apply( lambda datetime: pd.to_datetime(datetime[0:13])) df = df[['datetime', 'count']].groupby('datetime').sum() df['datetime'] = df.index df['hour'] = df.datetime.apply(lambda datetime: datetime.hour) hour_grained_dataframe = df[["datetime", "hour", "count"]] # hour_grained_data = np.array(hour_grained_dataframe.values) # np.save(config.dataset_dir_path + str(city_id) + "_hour_grained_data.npy", hour_grained_data) # 预处理日粒度的数据 df = pd.read_csv(origin_dataset_file_path, names=["city_id", "datetime", "count"], sep="\t") df['datetime'] = df.datetime.apply( lambda datetime: pd.to_datetime(datetime[0:10])) df = df[['datetime', 'count']].groupby('datetime').sum() df['datetime'] = df.index df['day_of_week'] = df.datetime.apply(lambda datetime: datetime.weekday()) df['holidays_distance'] = 8 df['end_of_holidays_distance'] = 8 df['is_weekend_weekday'] = 0 for i in range(df.shape[0]): for holiday in holidays: tmp = (pd.to_datetime(holiday) - df.iloc[i, 1]).days if abs(tmp) < abs(df.iloc[i, 3]): df.iloc[i, 3] = tmp for end_of_holiday in end_of_holidays: tmp = (pd.to_datetime(end_of_holiday) - df.iloc[i, 1]).days if tmp < df.iloc[i, 4] and tmp >= 0: df.iloc[i, 4] = tmp for weekend_weekday in weekend_weekdays: if pd.to_datetime(weekend_weekday) == df.iloc[i, 1]: df.iloc[i, 5] = 1 df.holidays_distance = df.holidays_distance + 7 day_grained_dataframe = df[[ 'datetime', 'day_of_week', 'holidays_distance', 'end_of_holidays_distance', 'is_weekend_weekday', 'count' ]] # day_grained_data = np.array(day_grained_dataframe.values) # np.save(config.dataset_dir_path + str(city_id) + "_day_grained_data.npy", day_grained_data) # 将数据处理为可输入模型的格式 may_be_predict_date = pd.to_datetime( config.dateset_start_date) + datetime.timedelta(days=7) data = [] for i in range(len(hour_grained_dataframe)): if hour_grained_dataframe.iloc[i].datetime < may_be_predict_date: continue else: day_grained_8_days_data = [] for delta_days in range(7, 0, -1): day_grained_8_days_data.append(day_grained_dataframe.loc[( hour_grained_dataframe.iloc[i].datetime - datetime.timedelta(days=delta_days) ).strftime('%Y-%m-%d')].values[1:]) day_grained_8_days_data.append(day_grained_dataframe.loc[ hour_grained_dataframe.iloc[i].datetime.strftime( '%Y-%m-%d')].values[1:]) goal = day_grained_8_days_data[-1][-1] if remains: day_grained_8_days_data[-1][-1] = day_grained_8_days_data[-1][ -1] - hour_grained_dataframe.iloc[i - (i % 24):i]['count'].sum() day_grained_8_days_data = np.array(day_grained_8_days_data, dtype='float64') day_growth_rate_vector = ( np.array(day_grained_8_days_data[:, 4], dtype='float64') / float(day_grained_8_days_data[0, 4]) - 1).reshape(-1, 1) day_grained_8_days_data = np.concatenate( [day_grained_8_days_data, day_growth_rate_vector], 1) hour_grained_data = np.array( hour_grained_dataframe.iloc[i - 25:i].values[:, 1:], dtype='float64') hour_growth_rate_vector = ( hour_grained_data[:, 1] / hour_grained_data[0, 1] - 1).reshape( -1, 1) is_today_vector = np.concatenate( [np.zeros(1 + (24 - i % 24)), np.ones(i % 24)]).reshape(-1, 1) data.append([ hour_grained_dataframe.iloc[i].values[0], day_grained_8_days_data[:-1], day_grained_8_days_data[-1, :-1], np.concatenate([ hour_grained_data, hour_growth_rate_vector, is_today_vector ], 1)[1:], day_grained_8_days_data[-1, -1], np.array([ day_grained_8_days_data[0, 4], hour_grained_dataframe.iloc[i - (i % 24):i]['count'].sum(), hour_grained_dataframe.iloc[i:i + (24 - i % 24)]['count'].sum(), goal ], dtype='float64') ]) data = np.array(data) if save_as_file: if remains: np.save(os.path.join(config.dataset_dir, "%d_data.npy" % city_id), data) else: np.save( os.path.join(config.dataset_dir, "%d_data_predict_full_day.npy" % city_id), data) return data