def meetup_day(m_Year, m_Month, weekday, SPD):
dayStart = dayNames.index(weekday)
chanceDate = [
weekItem[dayStart]
for weekItem in calendar.monthcalendar(m_Year, m_Month)
if weekItem[dayStart]]
if SPD == 'teenth':
for dayItem in chanceDate:
if 13 <= dayItem <= 19:
return date(m_Year, m_Month, dayItem)
elif SPD == 'last':
dayStart = -1
elif SPD == 'first':
dayStart = 0
else:
dayStart = int(SPD[0]) - 1
return date(m_Year, m_Month, chanceDate[dayStart])
python类monthcalendar()的实例源码
def __init__(self, month, year, indent_level, indent_style):
'x.__init__(...) initializes x'
calendar.setfirstweekday(calendar.SUNDAY)
matrix = calendar.monthcalendar(year, month)
self.__table = HTML_Table(len(matrix) + 1, 7, indent_level, indent_style)
for column, text in enumerate(calendar.day_name[-1:] + calendar.day_name[:-1]):
self.__table.mutate(0, column, '<b>%s</b>' % text)
for row, week in enumerate(matrix):
for column, day in enumerate(week):
if day:
self.__table.mutate(row + 1, column, '<b>%02d</b>\n<hr>\n' % day)
self.__weekday, self.__alldays = calendar.monthrange(year, month)
self.__weekday = ((self.__weekday + 1) % 7) + 6
def check_weeks(self, year, month, weeks):
cal = calendar.monthcalendar(year, month)
self.assertEqual(len(cal), len(weeks))
for i in range(len(weeks)):
self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
def check_weeks(self, year, month, weeks):
cal = calendar.monthcalendar(year, month)
self.assertEqual(len(cal), len(weeks))
for i in xrange(len(weeks)):
self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
def check_weeks(self, year, month, weeks):
cal = calendar.monthcalendar(year, month)
self.assertEqual(len(cal), len(weeks))
for i in xrange(len(weeks)):
self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
def _first_of_month(self, day_of_week):
"""
Modify to the first occurrence of a given day of the week
in the current month. If no day_of_week is provided,
modify to the first day of the month. Use the supplied consts
to indicate the desired day_of_week, ex. pendulum.MONDAY.
:type day_of_week: int
:rtype: Date
"""
dt = self
if day_of_week is None:
return dt.day_(1)
month = calendar.monthcalendar(dt.year, dt.month)
calendar_day = (day_of_week - 1) % 7
if month[0][calendar_day] > 0:
day_of_month = month[0][calendar_day]
else:
day_of_month = month[1][calendar_day]
return dt.day_(day_of_month)
def _last_of_month(self, day_of_week=None):
"""
Modify to the last occurrence of a given day of the week
in the current month. If no day_of_week is provided,
modify to the last day of the month. Use the supplied consts
to indicate the desired day_of_week, ex. pendulum.MONDAY.
:type day_of_week: int or None
:rtype: Date
"""
dt = self
if day_of_week is None:
return dt.day_(self.days_in_month)
month = calendar.monthcalendar(dt.year, dt.month)
calendar_day = (day_of_week - 1) % 7
if month[-1][calendar_day] > 0:
day_of_month = month[-1][calendar_day]
else:
day_of_month = month[-2][calendar_day]
return dt.day_(day_of_month)
def _first_of_month(self, day_of_week):
"""
Modify to the first occurrence of a given day of the week
in the current month. If no day_of_week is provided,
modify to the first day of the month. Use the supplied consts
to indicate the desired day_of_week, ex. Pendulum.MONDAY.
:type day_of_week: int
:rtype: Pendulum
"""
dt = self.start_of('day')
if day_of_week is None:
return dt.day_(1)
month = calendar.monthcalendar(dt.year, dt.month)
calendar_day = (day_of_week - 1) % 7
if month[0][calendar_day] > 0:
day_of_month = month[0][calendar_day]
else:
day_of_month = month[1][calendar_day]
return dt.day_(day_of_month)
def _last_of_month(self, day_of_week=None):
"""
Modify to the last occurrence of a given day of the week
in the current month. If no day_of_week is provided,
modify to the last day of the month. Use the supplied consts
to indicate the desired day_of_week, ex. Pendulum.MONDAY.
:type day_of_week: int or None
:rtype: Pendulum
"""
dt = self.start_of('day')
if day_of_week is None:
return dt.day_(self.days_in_month)
month = calendar.monthcalendar(dt.year, dt.month)
calendar_day = (day_of_week - 1) % 7
if month[-1][calendar_day] > 0:
day_of_month = month[-1][calendar_day]
else:
day_of_month = month[-2][calendar_day]
return dt.day_(day_of_month)
def create_calendar(year,month):
markup = types.InlineKeyboardMarkup()
#First row - Month and Year
row=[]
row.append(types.InlineKeyboardButton(calendar.month_name[month]+" "+str(year),callback_data="ignore"))
markup.row(*row)
#Second row - Week Days
week_days=["M","T","W","R","F","S","U"]
row=[]
for day in week_days:
row.append(types.InlineKeyboardButton(day,callback_data="ignore"))
markup.row(*row)
my_calendar = calendar.monthcalendar(year, month)
for week in my_calendar:
row=[]
for day in week:
if(day==0):
row.append(types.InlineKeyboardButton(" ",callback_data="ignore"))
else:
row.append(types.InlineKeyboardButton(str(day),callback_data="calendar-day-"+str(day)))
markup.row(*row)
#Last row - Buttons
row=[]
row.append(types.InlineKeyboardButton("<",callback_data="previous-month"))
row.append(types.InlineKeyboardButton(" ",callback_data="ignore"))
row.append(types.InlineKeyboardButton(">",callback_data="next-month"))
markup.row(*row)
return markup
def check_weeks(self, year, month, weeks):
cal = calendar.monthcalendar(year, month)
self.assertEqual(len(cal), len(weeks))
for i in range(len(weeks)):
self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
def check_weeks(self, year, month, weeks):
cal = calendar.monthcalendar(year, month)
self.assertEqual(len(cal), len(weeks))
for i in xrange(len(weeks)):
self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
def check_weeks(self, year, month, weeks):
cal = calendar.monthcalendar(year, month)
self.assertEqual(len(cal), len(weeks))
for i in range(len(weeks)):
self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
def check_weeks(self, year, month, weeks):
cal = calendar.monthcalendar(year, month)
self.assertEqual(len(cal), len(weeks))
for i in xrange(len(weeks)):
self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
def check_weeks(self, year, month, weeks):
cal = calendar.monthcalendar(year, month)
self.assertEqual(len(cal), len(weeks))
for i in range(len(weeks)):
self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
def calendar_sql(gid, pid, year, month, show_subtasks):
if show_subtasks:
q = DBS.query(Task).filter_by(gid=gid, pid=pid)
else:
q = DBS.query(Task).filter_by(gid=gid, pid=pid, ptid=0)
rs = q.all()
task_d = {}
for r in rs:
if str(r.due_date) not in task_d:
task_d[str(r.due_date)] = r.name
else:
task_d[str(r.due_date)] += "\n" + r.name
calendar.setfirstweekday(6)
calendar_a = calendar.monthcalendar(year, month)
for week in range(len(calendar_a)):
for day in range(7):
date_num = calendar_a[week][day]
if date_num == 0:
calendar_a[week][day] = {'date_num': 0, 'tasks': ""}
# TODO: show out of month days and tasks
continue
str_date = datetime.date(year, month, date_num)\
.strftime('%Y-%m-%d')
calendar_a[week][day] = {'date_num': date_num,
'tasks': task_d[str_date]
if str_date in task_d else ''}
return calendar_a
def create_calendar(self, year, month):
self.logger.debug("in create calendar")
markup = []
markup.append([InlineKeyboardButton(calendar.month_name[month] + ", " + str(year), callback_data=' ')])
week_days = ["M", "T", "W", "R", "F", "S", "U"]
temp = []
for day in week_days:
temp.append(InlineKeyboardButton(day, callback_data=' '))
markup.append(temp)
cur_cal = calendar.monthcalendar(year, month)
for week in cur_cal:
temp = []
for day in week:
if day is 0:
temp.append(InlineKeyboardButton(" ", callback_data=' '))
continue
temp.append(InlineKeyboardButton(str(day), callback_data='cal-d-%d-%d-%d' % (day, month, year)))
markup.append(temp)
temp = []
temp.append(InlineKeyboardButton('<', callback_data='cal-p'))
temp.append(InlineKeyboardButton('>', callback_data='cal-n'))
markup.append(temp)
self.logger.debug("Created calendar")
return InlineKeyboardMarkup(markup)
#return InlineKeyboardMarkup([[InlineKeyboardButton("This is a test", callback_data=' ')]])