python計算日期之間的放假日期
本文實例為大家分享了python計算日期之間的放假日期,供大家參考,具體內(nèi)容如下
代碼如下:
#encoding=utf-8 print '中國' #自動查詢節(jié)日 給定起始日期和結(jié)束日期,自動獲取總共的節(jié)假日天數(shù) import datetime from dateutil import rrule,easter try: set except NameError: from sets import Set as set #復(fù)活節(jié) 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] #開始到結(jié)束的節(jié)禮日列表 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] #返回開始和結(jié)束日期之間的圣誕節(jié)列表 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] #返回勞動節(jié)列表 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] #讀取設(shè)定的節(jié)假日 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')
打印結(jié)果如下:
中國
(4, [datetime.date(2014, 4, 20), datetime.date(2014, 12, 25), datetime.date(2014, 3, 23), datetime.date(2014, 9, 1)])
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python中關(guān)于時間和日期函數(shù)的常用計算總結(jié)(time和datatime)
- Python計算兩個日期相差天數(shù)的方法示例
- python計算N天之后日期的方法
- python 計算兩個日期相差多少個月實例代碼
- Python日期時間模塊datetime詳解與Python 日期時間的比較,計算實例代碼
- Python根據(jù)指定日期計算后n天,前n天是哪一天的方法
- Python 3.3實現(xiàn)計算兩個日期間隔秒數(shù)/天數(shù)的方法示例
- Python簡單計算給定某一年的某一天是星期幾示例
- Python編程判斷這天是這一年第幾天的方法示例
- Python編程實現(xiàn)輸入某年某月某日計算出這一天是該年第幾天的方法
- Python自定義函數(shù)計算給定日期是該年第幾天的方法示例
相關(guān)文章
Python Pandas常用函數(shù)方法總結(jié)
今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著Pandas常用函數(shù)方法展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06Python數(shù)據(jù)模型與Python對象模型的相關(guān)總結(jié)
這篇文章主要介紹了Python數(shù)據(jù)模型與Python對象模型的相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2021-01-01Python中自定義函方法與參數(shù)具有默認值的函數(shù)
這篇文章主要介紹了Python中自定義函方法與參數(shù)具有默認值的函數(shù),在Python編程中,可以使用已經(jīng)定義好的函數(shù),也可以自定義函數(shù)實現(xiàn)某些特殊的功能,更多相關(guān)資料,請需要的人參考下面文章內(nèi)容2022-02-02Pycharm 字體大小調(diào)整設(shè)置的方法實現(xiàn)
這篇文章主要介紹了Pycharm 字體大小調(diào)整的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Python創(chuàng)建exe運行器和截圖工具的示例詳解
本文我們將探討如何使用Python和wxPython創(chuàng)建一個強大而實用的桌面應(yīng)用程序,可以遍歷指定文件夾中的所有EXE文件,感興趣的小伙伴可以了解一下2024-10-10Python實現(xiàn)統(tǒng)計文章閱讀量的方法詳解
這篇文章主要為大家詳細介紹了如何溧陽Python語言實現(xiàn)統(tǒng)計文章閱讀量的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02關(guān)于Pytorch MaxUnpool2d中size操作方式
今天小編就為大家分享一篇關(guān)于Pytorch MaxUnpool2d中size操作方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01