python计算日期之间的放假日期
作者:houyj1986 时间:2021-08-14 00:02:42
本文实例为大家分享了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)])
来源:https://blog.csdn.net/houyj1986/article/details/22000905
标签:python,计算,日期
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP simplexml_load_string()函数实例讲解
2023-07-09 07:19:28
![](https://img.aspxhome.com/file/2023/1/55401_0s.png)
asp 实现的冒泡排序程序
2011-03-25 11:13:00
python3实现读取chrome浏览器cookie
2023-10-18 13:18:44
用VBS语言实现的网页计算器源代码
2007-12-26 17:09:00
![](https://img.aspxhome.com/file/UploadPic/200712/26/20071226172731580s.jpg)
一文秒懂pandas中iloc()函数
2023-07-31 18:20:42
![](https://img.aspxhome.com/file/2023/9/76149_0s.png)
php 无法载入mysql扩展
2023-09-07 13:07:40
python计算时间差的方法
2023-05-19 16:08:23
不安全的js写法
2009-09-16 14:26:00
PHPCMS的使用小结
2023-11-20 20:19:47
网页设计的十要十不要
2007-12-21 13:01:00
Python 使用 attrs 和 cattrs 实现面向对象编程的实践
2022-05-29 22:38:00
Dreamweaver MX 2004新特点
2008-02-03 11:35:00
Go语言实现超时的三种方法实例
2023-06-22 18:32:43
在Python中使用NLTK库实现对词干的提取的教程
2022-11-04 15:13:53
![](https://img.aspxhome.com/file/2023/6/67756_0s.jpg)
Python双端队列deque的实现
2022-07-07 02:37:29
![](https://img.aspxhome.com/file/2023/2/71552_0s.png)
python对csv文件追加写入列的方法
2022-11-14 01:56:29
![](https://img.aspxhome.com/file/2023/4/69064_0s.png)
《CSS禅意花园》学习笔记
2008-10-20 12:43:00
![](https://img.aspxhome.com/file/UploadPic/up/2008102012534738.gif)
Web标准学习:CSS样式书写风格
2008-03-25 09:37:00
![](https://img.aspxhome.com/file/UploadPic/20083/25/200832593821947s.gif)
解决Python 遍历字典时删除元素报异常的问题
2023-11-17 04:06:47
61条面向对象设计的经验原则
2008-05-08 13:05:00