python UNIX_TIMESTAMP时间处理方法分析

风,从水中掠过,留下粼粼波纹;阳光,从云中穿过,留下丝丝温暖;岁月,从树林中走过,留下圈圈年轮。

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

from datetime import datetime
import time
int(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 socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  s.connect(('8.8.8.8', 0))
  localIP=s.getsockname()[0]

希望本文所述对大家Python程序设计有所帮助。

到此这篇关于python UNIX_TIMESTAMP时间处理方法分析就介绍到这了。有信念不一定成功,没信念一定会失败。更多相关python UNIX_TIMESTAMP时间处理方法分析内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战