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 引号、注释、字符串

Python3.6.3    json==2.0.9    win10.0.17134 字符串 1.不可变类型,可迭代对...

使用Python批量修改文件名的代码实例

使用Python批量修改文件名的代码实例

这两天在整理一些文章,但是文件夹中每个文章没有序号会看起来很乱,所以想着能不能用Python写一个小脚本。 于是乎,参考了多方资料,简单写了下面几行代码 import osdef...

Python简单进程锁代码实例

先说说线程 在多线程中,为了保证共享资源的正确性,我们常常会用到线程同步技术. 将一些敏感操作变成原子操作,保证同一时刻多个线程中只有一个线程在执行这个原子操作。 我最常用的是互斥锁,也...

再谈Python中的字符串与字符编码(推荐)

再谈Python中的字符串与字符编码(推荐)

本节内容: 1.前言 2.相关概念 3.Python中的默认编码 4.Python2与Python3中对字符串的支持 5.字符编码转换 一、前言 Python中的字符编码是个老...

python opencv之分水岭算法示例

python opencv之分水岭算法示例

本文介绍了python opencv之分水岭算法示例,分享给大家,具体如下: 目标 使用分水岭算法对基于标记的图像进行分割 使用函数cv2.watershed() 原理:...