def convertTimestamp(self, ts):
# 2050107034146B
self.timetuple = [0,0,0,0,0,0,0,0,0]
self.timetuple[0] = int(ts[0]) + int(ts[1]) * 10
if self.timetuple[0] >= 80:
# Convert to previous century, hopefully no one uses this after 2079 ;)
self.timetuple[0] += 1900
else:
# Convert to current century
self.timetuple[0] += 2000
#~ print ts
self.timetuple[1] = int(ts[2]) + int(ts[3]) * 10
self.timetuple[2] = int(ts[4]) + int(ts[5]) * 10
self.timetuple[3] = int(ts[6]) + int(ts[7]) * 10
self.timetuple[4] = int(ts[8]) + int(ts[9]) * 10
self.timetuple[5] = int(ts[10]) + int(ts[11]) * 10
self.timetuple[6] = calendar.weekday(self.timetuple[0], self.timetuple[1], self.timetuple[2])
return time.asctime(self.timetuple)
python类weekday()的实例源码
def strptime(date):
"""Convert the date string into a 9 tuple"""
df = [0,0,0,0,0,0,0,0,0]
sp = date.split(' ')
spt = sp[3].split(':')
df[0] = int(sp[4]) # Year
if abbrevMonthNames.has_key(sp[1]):
df[1] = int(abbrevMonthNames[sp[1]])
else:
df[1] = 1 # Month
df[2] = int(sp[2]) # Day
df[3] = int(spt[0]) # Hour
df[4] = int(spt[1]) # Minute
df[5] = int(spt[2]) # Second
df[6] = calendar.weekday(df[0], df[1], df[2])
return df
def test_W_wildcard(self):
years = [2016, 2017] # leap and normal year
for year in years:
for month in range(1, 13):
_, days = calendar.monthrange(year, month)
for day in range(1, days):
weekday = calendar.weekday(year, month, day)
result = day
if weekday == 5:
result = day - 1 if day > 1 else day + 2
elif weekday == 6:
result = day + 1 if day < days else day - 2
self.assertEquals(MonthdaySetBuilder(year, month).build(str(day) + "W"), {result})
def is_valid_date(given_date):
"""
Given a date string this function checks if the date is in the future
"""
given_date = given_date.split("/")
current_date = get_current_date().split("/")
given_day = int(given_date[1])
given_month = int(given_date[0])
given_year = int(given_date[2])
current_day = int(current_date[1])
current_month = int(current_date[0])
current_year = int(current_date[2])
try:
calendar.weekday(given_year, given_month, given_day)
except ValueError:
return False
return (
(given_year == current_year and given_month == current_month and given_day > current_day) or
(given_year == current_year and given_month > current_month) or
(given_year > current_year))
def dayofweek(day, month, year, format=True):
import calendar
if format == False:
return calendar.weekday(year, month, day) + 1
else:
if calendar.weekday(year, month, day) == 0:
return 'Monday'
elif calendar.weekday(year, month, day) == 1:
return 'Tuesday'
elif calendar.weekday(year, month, day) == 2:
return 'Wednesday'
elif calendar.weekday(year, month, day) == 3:
return 'Thursday'
elif calendar.weekday(year, month, day) == 4:
return 'Friday'
elif calendar.weekday(year, month, day) == 5:
return 'Saturday'
elif calendar.weekday(year, month, day) == 6:
return 'Sunday'
# Check If A Year Is A Leap Year
def calcDate():
now = date.today()
dateList=[]
one_day = timedelta(days=1)
calcDate=now
charweek=("?","?","?","?","?","?","?")
for i in range(7):
calcDate=calcDate+one_day
year=calcDate.year
month=calcDate.month
day=calcDate.day
week=weekday(year,month,day)
strdate=str(month)+"/"+str(day)+"("+charweek[week]+") 19:00?\n"
dateList.append(strdate)
return dateList
def __init__(self, *args, **keys):
self._spec = {}
max_priority = min(x.time_tuple_index for x in self._units.values())
for key, arg in dict(*args, **keys).iteritems():
if key not in self._units:
raise TypeError("unexpected unit {0!r}".format(key))
unit = self._units[key]
max_priority = max(max_priority, unit.time_tuple_index)
rangeobj = self._coerce(arg)
self._spec[key] = unit.resolve(rangeobj)
for key, unit in self._units.iteritems():
if key in self._spec:
continue
if max_priority >= unit.time_tuple_index:
self._spec[key] = unit.resolve(any())
else:
self._spec[key] = unit.resolve(value(unit.min))
# Special case: If both day or weekday is limited, then use OR instead of AND.
if self._is_any("day"):
self._spec["day"] = self._units["day"].resolve(empty())
elif self._is_any("weekday"):
self._spec["weekday"] = self._units["weekday"].resolve(empty())
self._spec["day"] = _ResolvedOr(self._spec.pop("day"), self._spec.pop("weekday"))
def get_weekday_format(year, month, day, include_weekend=True):
"""return numeric day in string if weekday else day with WEEKEND_SYMBOL"""
weekend = [calendar.SATURDAY, calendar.SUNDAY]
d = calendar.weekday(year, month, day)
# highlight_wkend = False
# global WEEKEND_SYMBOL
# WEEKEND_SYMBOL = ' '
if d in weekend:
if include_weekend:
return "{ws}{day} ".format(ws=WEEKEND_SYMBOL, day=day)
else:
return ""
return " {day} ".format(day=day)
def begin_end_week_day(current_day, delay=0):
begin = (datetime.now() - timedelta(days=current_day.weekday())) + timedelta(days=7 * delay)
end = begin + timedelta(days=4)
return begin, end
def unlucky_days(year):
return sum(weekday(year, month, 13) == 4 for month in range(1, 13))