概述
#!/usr/bin/python
#coding=utf-8
# Filename : d:pyrebuildhandlerutils.py
import httplib,urllib
from datetime import *
class util(object):
#b百度短地址的获取
@staticmethod
def dwz(url):
'''short url
use baidu.com
'''
host = "dwz.cn"
urlpath = "/create.php"
httpconn = httplib.HTTPConnection(host)
#request(self, method, url, body=None, headers={})
params = {"url":url}
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
body = urllib.urlencode(params)
#dwz.cn/create.php?url=http://write.blog.csdn.net/postedit/20907063
print body
httpconn.request("POST",urlpath,body,headers)
resp = httpconn.getresponse()
msg = {}
msg = eval(resp.read())
#print msg
#for k,v in msg.items():
#print "%s=%s" % (k,v)
return msg["tinyurl"]
def difftime(self,begin,end):
'''
type datetime
begin,end format yyyy-MM-dd HH:mm:ss or datetime
return seconds
'''
date1 = None
date2 = None
if type(begin) is str:
date1 = datetime.strptime(begin,'%Y-%d-%m %H:%M:%S') #%Y-%d-%m %H:%M:%S
elif type(begin) is datetime:
date1 = begin
else:
raise ValueError,'begin invalid argument'
if type(end) is str:
date2 = datetime.strptime(end,'%Y-%d-%m %H:%M:%S')
elif type(end) is datetime:
date2 = end
else:
raise ValueError,'endtime argument error'
diff = date2 - date1 # return datedelta obj
return int(diff.total_seconds())
def difftime_now(self,strtime):
'''
now() - strtime
'''
return self.difftime(strtime,datetime.now())
def curr_night(self):
'''
return datetime
'''
now = datetime.now()
d = datetime(now.year,now.month,now.day,0,0,0)
#return d.strftime("%Y-%d-%m %H:%M:%S")
return d
def yesterday_night(self):
'''
return datetime
'''
now = datetime.now()
aday = timedelta(days=1)
now += aday
return datetime(now.year,now.month,now.day,0,0,0)
def add_day(self,n):
'''
n can nav
'''
now = datetime.now()
aday = timedelta(days=n)
now += aday
return now
if __name__ == "__main__":
longurl = "http://www.500wan.com/static/public/dlc/xml/dayexpect0.xml"
dw = util.dwz(longurl)
print "dw=%s" % dw
#test
d = datedelta_ = util().difftime('2014-03-01 16:44:00','2014-03-03 16:45:00')
#print d
d2 = util().difftime_now('2014-03-01 16:44:00')
#print d2
#print util().curr_night()
#print util().yesterday_night()
print util().add_day(-10)
最后
以上就是飞快小笼包为你收集整理的python时间处理,当天凌晨 ,次日凌晨,滚动N天的全部内容,希望文章能够帮你解决python时间处理,当天凌晨 ,次日凌晨,滚动N天所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复