首页 > 编程 > Python > 正文

python UNIX_TIMESTAMP时间处理方法分析

2020-01-04 17:32:00
字体:
来源:转载
供稿:网友
这篇文章主要介绍了python UNIX_TIMESTAMP时间处理方法,结合实例形式分析了Python针对UNIX_TIMESTAMP时间的常见运算技巧,需要的朋友可以参考下
 

本文实例讲述了python UNIX_TIMESTAMP时间处理方法。分享给大家供大家参考,具体如下:

from datetime import datetimeimport timeint(time.mktime(rm.start_time.timetuple())) datetime.fromtimestamp(time.time())rf.start_time + timedelta(0, math.ceil(diff.total_seconds()/(3600*rf.period))*3600*rf.period)

获取按月或者小时偏移时间

def getlastday(datetimeobj): year= datetimeobj.year month = datetimeobj.month + 1 if month>12:  year = year + 1  month=1 return datetime(year, month, 1) - timedelta(1)def getSendTime(period, start_time): datenow = datetime.now() nextTime = preTime = start_time if period > 0 and period < 1000:  diff = datetime.now() - start_time  nextTime = start_time + timedelta(0, math.ceil(diff.total_seconds()/(3600*period))*3600*period)  preTime = start_time + timedelta(0, math.floor(diff.total_seconds()/(3600*period))*3600*period) if period == 1001:  if datenow.day>start_time.day:   preTime = datetime(datenow.year, datenow.month, start_time.day)   nextTime = getlastday(getlastday(datenow)+timedelta(1))   if nextTime.day > start_time.day:    nextTime = datetime(nextTime.year, nextTime.month, start_time.day)  else:   preTime = getlastday(datetime(datenow.year, datenow.month, 1) - timedelta(1))   if preTime.day > start_time.day:    preTime = datetime(preTime.year, preTime.month, start_time.day)   nextTime = getlastday(datenow)   if nextTime.day > start_time.day:    nextTime = datetime(nextTime.year, nextTime.month, start_time.day) return preTime, nextTime

排序

reportMetaPeriodInfo.sort(lambda p1, p2:cmp(p1['k'], p2['k']))

获取ip

import sockets = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  s.connect(('8.8.8.8', 0))  localIP=s.getsockname()[0]

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表