python next day,日期时间Python-下一个工作日

Two related issues: (1) All of the data I work with have weekday dates attached. At various points, I need to know what the next weekday is. I've written something like the code below to make this determination, but I'm sure there's a better way. Anyone? (2) Ideally, I need to know not just the next weekday, but the next US business day--that is, the next weekday that is not a US market holiday. Any help on this would also be great.

import datetime as dt

day = dt.datetime.strptime('2012-02-03','%Y-%m-%d').date()

print day#day=2012-03-02 (Friday)

if day.weekday()==4:

day = day+dt.timedelta(days=3)

else:

day = day+dt.timedelta(days=1)

print day#day=2012-02-06 (Monday)

day = day+dt.timedelta(days=1)

print day#day=2012-02-07 (Tuesday)

解决方案import datetime

from dateutil import rrule

holidays = [

datetime.date(2012, 5, 1,),

datetime.date(2012, 6, 1,),

# ...

]

# Create a rule to recur every weekday starting today

r = rrule.rrule(rrule.DAILY,

byweekday=[rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR],

dtstart=datetime.date.today())

# Create a rruleset

rs = rrule.rruleset()

# Attach our rrule to it

rs.rrule(r)

# Add holidays as exclusion days

for exdate in holidays:

rs.exdate(exdate)

print rs[0]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值