python中怎么计算放假日期-创新互联
这篇文章将为大家详细讲解有关python中怎么计算放假日期,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
目前成都创新互联已为上千多家的企业提供了网站建设、域名、虚拟空间、网站托管维护、企业网站设计、富蕴网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。代码如下:
#encoding=utf-8 print '中国' #自动查询节日 给定起始日期和结束日期,自动获取总共的节假日天数 import datetime from dateutil import rrule,easter try: set except NameError: from sets import Set as set #复活节 def all_easter(start, end): easters = [easter.easter(y) for y in xrange(start.year,end.year+1)] return [d for d in easters if start<=d<=end] #开始到结束的节礼日列表 def all_boxing(start, end): one_day = datetime.timedelta(days=1) boxings = [easter.easter(y) + one_day for y in xrange(start.year,end.year+1)] return [d for d in boxings if start<=d<=end] #返回开始和结束日期之间的圣诞节列表 def all_christmas(start, end): christmases = [datetime.date(y,12,25) for y in xrange(start.year, end.year + 1)] return [d for d in christmases if start<=d<=end] #返回劳动节列表 def all_labor(start, end): labors = rrule.rrule(rrule.YEARLY, bymonth=9, byweekday=rrule.MO(1),dtstart=start, until=end) return [d.date() for d in labors] #读取设定的节假日 def read_holidays(start, end, holidays_file='holidays.txt'): try: holidays_file = open(holidays_file) except IOError,err: print 'open failed' return [] holidays = [] for line in holidays_file: if line.isspace() or line.startswith('#'): continue try: y,m,d = [int(x.strip()) for x in line.split(',')] date = datetime.date(y,m,d) except ValueError: print 'Invalid line find' continue if start <= date <=end: holidays.append(date) holidays_file.close() return holidays holidays_by_country = { 'US':(all_easter,all_christmas,all_labor), 'IT':(all_easter,all_boxing,all_labor) } def holidays(cc,start,end,holidays_file='holidays.txt'): all_holidays= read_holidays(start,end,holidays_file) functions = holidays_by_country.get(cc,()) for function in functions: all_holidays += function(start,end) all_holidays = list(set(all_holidays)) return (len(all_holidays),all_holidays) test_file = open(r'D:\123.txt','w') test_file.write('2014,3,23') test_file.close() print holidays('US',datetime.date(2014,1,1),datetime.date(2014,12,31),r'D:\123.txt')
打印结果如下:
中国
(4, [datetime.date(2014, 4, 20), datetime.date(2014, 12, 25), datetime.date(2014, 3, 23), datetime.date(2014, 9, 1)])
关于python中怎么计算放假日期就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网站名称:python中怎么计算放假日期-创新互联
URL地址:http://ybzwz.com/article/ddoihh.html