python UNIX_TIMESTAMP时间处理方法分析

yipeiwu_com5年前Python基础

本文实例讲述了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日期与时间操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

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

相关文章

Python使用PyGreSQL操作PostgreSQL数据库教程

PostgreSQL是一款功能强大的开源关系型数据库,本文使用python实现了对开源数据库PostgreSQL的常用操作,其开发过程简介如下: 一、环境信息:   ...

python将视频转换为全字符视频

python将视频转换为全字符视频

简介 如何简单的使用python来实现将一部视频转换为字符画视频的效果。 其实,大家都知道视频就是一帧一帧的图片构成的。   那么如今我们想要实现,将视频转换为...

Python实现的随机森林算法与简单总结

本文实例讲述了Python实现的随机森林算法。分享给大家供大家参考,具体如下: 随机森林是数据挖掘中非常常用的分类预测算法,以分类或回归的决策树为基分类器。算法的一些基本要点: *对大小...

Python Learning 列表的更多操作及示例代码

遍历列表-for循环 列表中存储的元素可能非常多,如果想一个一个的访问列表中的元素,可能是一件十分头疼的事。那有没有什么好的办法呢?当然有!使用 for循环 假如有一个食物名单列表,通过...

mysql 之通过配置文件链接数据库

mysql 之通过配置文件链接数据库 配置文件jdbc.properties ##MySQL driver=com.mysql.jdbc.Driver url=jdbc\:mysql...