import re
创新互联公司于2013年开始,先为揭阳等服务建站,揭阳等地企业,进行企业商务咨询服务。为揭阳企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
def command_add(date, event_details, calendar):
'''
Add event_details to the list at calendar[date]
Create date if it was not there
:param date: A string date formatted as "YYYY-MM-DD"
:param event_details: A string describing the event
:param calendars: The calendars database
:return: a string indicating any errors, "" for no errors
'''
try:
p = re.compile(r"\d{4}-\d{2}-\d{2}")
assert p.match(date), "Param date must match YYYY-MM-DD"
assert isinstance(event_details, str), \
"Param event_details must be a string"
if date in calendar:
calendar[date].append(str(event_details))
else:
calendar.update({date: str(event_details)})
except Exception,e:
return str(e)
def main():
calendar = {}
command_add("2015-10-20", "Python class", calendar)
print calendar
command_add("2015-11-01", "go out with friends after test",
calendar)
print calendar
if __name__ == "__main__":
main()
1.
定义一个get_constellation(month,date)函数,来获取出生日期。
2.
创建一个dates和constellations分别来储存对应的日和星座。
3.
用if语句判断输入的日数是否小于出生月份减一所对应的日数。
4.
如果是就返回月份减一所对应的星座,不是就返回出生月份所对应的星座。
python格式化日期的方法:可以利用time.asctime(time.local(time.time()))函数来格式化日期。如果我们要获取当前日期,可以利用时间函数time()来获取。
python中要把字符串转换成日期格式需要使用time模块中的strptime函数,例子如下:
import time
t = time.strptime('2016-05-09 21:09:30', '%y-%m-%d %h:%m:%s')
print(t)执行结果如下:
time.struct_time(tm_year=2016,
tm_mon=5,
tm_mday=9,
tm_hour=21,
tm_min=9,
tm_sec=30,
tm_wday=0,
tm_yday=130,
tm_isdst=-1)
函数说明:
第一个参数是要转换成日期格式的字符串,第二个参数是字符串的格式
函数官方文档如下:
help on built-in function strptime in module time:
strptime(...)
strptime(string, format) - struct_time
parse a string to a time tuple according to a format specification.
see the library reference manual for formatting codes (same as
strftime()).
commonly used format codes:
%y year with century as a decimal number.
%m month as a decimal number [01,12].
%d day of the month as a decimal number [01,31].
%h hour (24-hour clock) as a decimal number [00,23].
%m minute as a decimal number [00,59].
%s second as a decimal number [00,61].
%z time zone offset from utc.
%a locale's abbreviated weekday name.
%a locale's full weekday name.
%b locale's abbreviated month name.
%b locale's full month name.
%c locale's appropriate date and time representation.
%i hour (12-hour clock) as a decimal number [01,12].
%p locale's equivalent of either am or pm.
other codes may be available on your platform. see documentation for the c library strftime function.