本文整理匯總了Python中datetime.date.weekday方法的典型用法代碼示例。如果您正苦於以下問題:Python date.weekday方法的具體用法?Python date.weekday怎麽用?Python date.weekday使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類datetime.date
的用法示例。
在下文中一共展示了date.weekday方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: format_week
# 需要導入模塊: from datetime import date [as 別名]
# 或者: from datetime.date import weekday [as 別名]
def format_week(self, char, num):
if char.islower(): # week of year
day_of_year = self.get_day_of_year()
week = self.get_week_number(day_of_year)
if week == 0:
date = self.value - timedelta(days=day_of_year)
week = self.get_week_number(self.get_day_of_year(date),
date.weekday())
return self.format(week, num)
else: # week of month
week = self.get_week_number(self.value.day)
if week == 0:
date = self.value - timedelta(days=self.value.day)
week = self.get_week_number(date.day, date.weekday())
pass
return '%d' % week
示例2: format_weekday
# 需要導入模塊: from datetime import date [as 別名]
# 或者: from datetime.date import weekday [as 別名]
def format_weekday(self, char, num):
if num < 3:
if char.islower():
value = 7 - self.locale.first_week_day + self.value.weekday()
return self.format(value % 7 + 1, num)
num = 3
weekday = self.value.weekday()
width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num]
context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num]
return get_day_names(width, context, self.locale)[weekday]
示例3: get_week_number
# 需要導入模塊: from datetime import date [as 別名]
# 或者: from datetime.date import weekday [as 別名]
def get_week_number(self, day_of_period, day_of_week=None):
"""Return the number of the week of a day within a period. This may be
the week number in a year or the week number in a month.
Usually this will return a value equal to or greater than 1, but if the
first week of the period is so short that it actually counts as the last
week of the previous period, this function will return 0.
>>> format = DateTimeFormat(date(2006, 1, 8), Locale.parse('de_DE'))
>>> format.get_week_number(6)
1
>>> format = DateTimeFormat(date(2006, 1, 8), Locale.parse('en_US'))
>>> format.get_week_number(6)
2
:param day_of_period: the number of the day in the period (usually
either the day of month or the day of year)
:param day_of_week: the week day; if ommitted, the week day of the
current date is assumed
"""
if day_of_week is None:
day_of_week = self.value.weekday()
first_day = (day_of_week - self.locale.first_week_day -
day_of_period + 1) % 7
if first_day < 0:
first_day += 7
week_number = (day_of_period + first_day - 1) // 7
if 7 - first_day >= self.locale.min_week_days:
week_number += 1
return week_number
示例4: format_week
# 需要導入模塊: from datetime import date [as 別名]
# 或者: from datetime.date import weekday [as 別名]
def format_week(self, char, num):
if char.islower(): # week of year
day_of_year = self.get_day_of_year()
week = self.get_week_number(day_of_year)
if week == 0:
date = self.value - timedelta(days=day_of_year)
week = self.get_week_number(self.get_day_of_year(date),
date.weekday())
return self.format(week, num)
else: # week of month
week = self.get_week_number(self.value.day)
if week == 0:
date = self.value - timedelta(days=self.value.day)
week = self.get_week_number(date.day, date.weekday())
return '%d' % week
示例5: format_weekday
# 需要導入模塊: from datetime import date [as 別名]
# 或者: from datetime.date import weekday [as 別名]
def format_weekday(self, char='E', num=4):
"""
Return weekday from parsed datetime according to format pattern.
>>> format = DateTimeFormat(date(2016, 2, 28), Locale.parse('en_US'))
>>> format.format_weekday()
u'Sunday'
'E': Day of week - Use one through three letters for the abbreviated day name, four for the full (wide) name,
five for the narrow name, or six for the short name.
>>> format.format_weekday('E',2)
u'Sun'
'e': Local day of week. Same as E except adds a numeric value that will depend on the local starting day of the
week, using one or two letters. For this example, Monday is the first day of the week.
>>> format.format_weekday('e',2)
'01'
'c': Stand-Alone local day of week - Use one letter for the local numeric value (same as 'e'), three for the
abbreviated day name, four for the full (wide) name, five for the narrow name, or six for the short name.
>>> format.format_weekday('c',1)
'1'
:param char: pattern format character ('e','E','c')
:param num: count of format character
"""
if num < 3:
if char.islower():
value = 7 - self.locale.first_week_day + self.value.weekday()
return self.format(value % 7 + 1, num)
num = 3
weekday = self.value.weekday()
width = {3: 'abbreviated', 4: 'wide', 5: 'narrow', 6: 'short'}[num]
if char == 'c':
context = 'stand-alone'
else:
context = 'format'
return get_day_names(width, context, self.locale)[weekday]
示例6: update_date
# 需要導入模塊: from datetime import date [as 別名]
# 或者: from datetime.date import weekday [as 別名]
def update_date(self, date=None, save=False):
"""
Calculates the date to the next occurence and optionally saves it.
It uses the usual_month_day if set for setting the correct day in a monthly recurrence
"""
delta = None
if not date:
date = self.date
if self.interval == self.MONTHLY:
delta = relativedelta(months=1)
elif self.interval == self.QUARTERLY:
delta = relativedelta(months=3)
elif self.interval == self.BIANNUALLY:
delta = relativedelta(months=6)
elif self.interval == self.ANNUALLY:
delta = relativedelta(years=1)
elif self.interval == self.WEEKLY:
delta = relativedelta(weeks=1)
elif self.interval == self.DAILY:
delta = relativedelta(days=1)
else:
return
delta *= self.multiplier
while True:
date += delta
if self.usual_month_day > 0 and self.interval not in [self.WEEKLY, self.DAILY]:
day = self.usual_month_day
while True:
try:
date = date.replace(day=day)
break
except ValueError:
day -= 1
pass
if date.weekday() > 4 and self.interval not in [self.WEEKLY, self.DAILY]:
if self.weekend_handling == self.SKIP:
continue
elif self.weekend_handling == self.NEXT_WEEKDAY:
date += relativedelta(days=7 - date.weekday())
elif self.weekend_handling == self.PREVIOUS_WEEKDAY:
date -= relativedelta(days=date.weekday() - 4)
if save:
self.date = date
self.save()
return date
示例7: check_soc
# 需要導入模塊: from datetime import date [as 別名]
# 或者: from datetime.date import weekday [as 別名]
def check_soc():
"""Retrieve vehicle status and stop or start charging if
current charging level matches or exceeds specified max level and
the vehicle is currently charging.
"""
threading.Timer(60.0, check_soc).start() # Called every minute
p = v.get_position()
position = (p['position']['latitude'], p['position']['longitude'])
d = int(1000*distance(home,position))
if (d > 100):
logger.info("car is "+str(d)+"m from home")
return
t = datetime.datetime.now()
# offpeak: M-F (0-4) 0:00- 7:00, 23:00-23:59
# S-S (5-6) 0:00-15:00, 19:00-23:59
today = date.weekday(t)
offpeak = ( (t.hour < peak[today][0] or t.hour >= peak[today][1]))
# getting health status forces a status update
healthstatus = v.get_health_status()
status = { d['key'] : d['value'] for d in v.get_status()['vehicleStatus'] }
current_soc = int(status['EV_STATE_OF_CHARGE'])
charging_status = status['EV_CHARGING_STATUS']
logger.info("current SoC is "+str(current_soc)+"%"+", offpeak is "+str(offpeak))
if status['EV_CHARGING_METHOD'] == "WIRED":
logger.info("car is plugged in")
logger.info("charging status is "+charging_status)
if offpeak:
# allow for SoC to drop a little to avoid restarting too often
if current_soc < max_soc-1 and charging_status == "PAUSED":
logger.info("sending start charging request")
v.charging_start()
elif current_soc >= max_soc and charging_status == "CHARGING":
# Stop charging if max SoC is reached
logger.info("sending stop charging request")
v.charging_stop()
elif charging_status == "CHARGING":
# Stop charging if we are charging during peak
logger.info("sending stop charging request")
v.charging_stop()
else:
logger.info("car is not plugged in")