python时间操作总结
Unix时间戳
Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。Unix时间戳不仅被使用在Unix 系统、类Unix系统中,也在许多其他操作系统中被广告采用。
目前相当一部分操作系统使用32位二进制数字表示时间。此类系统的Unix时间戳最多可以使用到格林威治时间2038年01月19日03时14分07秒(二进制:01111111 11111111 11111111 11111111)。其后一秒,二进制数字会变为10000000 00000000 00000000 00000000,发生溢出错误,造成系统将时间误解为1901年12月13日20时45分52秒。这很可能会引起软件故障,甚至是系统瘫痪。使用64位二进制数字表示时间的系统(最多可以使用到格林威治时间292,277,026,596年12月04日15时30分08秒)则基本不会遇到这类溢出问题。
python中时间戳的使用
import time
import datetime #获取当前时间戳
time.time()
#返回时间戳1470733760.613 #获取localtime
time.localtime()
#time.struct_time(tm_year=2016, tm_mon=8, tm_mday=9, tm_hour=17, tm_min=11, tm_sec=13, tm_wday=1, tm_yday=222, tm_isdst=0) #上面的时间格式不好看,定义一个时间格式
ISOTIMEFORMAT='%Y-%m-%d %X'
#使用strftime做转换
a = time.strftime( ISOTIMEFORMAT, time.localtime() )
#返回'2016-08-09 17:15:27' #将'2016-08-09 17:15:27'转化为时间戳
time.mktime(time.strptime(a,ISOTIMEFORMAT))
#1470734127.0 #将时间戳转化为localtime
b = time.localtime(1470734127.0)
time.strftime(ISOTIMEFORMAT, b) #获取当前时间的前三天的时间戳
time.mktime((datetime.datetime.now() - datetime.timedelta(days = 3)).timetuple()) #获取今日的零点时间戳
now = time.time()
c = now - (now % 86400) + time.timezone #获取当前日期
date = datetime.datetime.now().strftime("%Y-%m-%d")
#获取当前时间的前n天
date_n = (datetime.datetime.now()-datetime.timedelta(days=n)).strftime("%Y-%m-%d")
python时间操作总结的更多相关文章
- Python时间操作所相关
相关模块:time,datetime,calendar(日历模块) 获取当前时间: # 获取当前10位时间戳,默认返回为float类型 print int(time.time()) # output: ...
- paip.日期时间操作以及时间戳uapi php java python 总结
paip.日期时间操作以及时间戳uapi php java python 总结 ///uapi Date 函数 | Day 函数 | Hour 函数 | Minute 函数 | Month 函数 | ...
- Python常用时间操作总结【取得当前时间、时间函数、应用等】转载
Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载 2017-05-11 作者:清风乐逍遥 我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...
- Python基础 | 日期时间操作
目录 获取时间 时间映射 格式转换 字符串转日期 日期转字符串 unixtime 时间计算 时间偏移 时间差 "日期时间数据"作为三大基础数据类型之一,在数据分析中会经常遇到. 本 ...
- Python时间与日期操作(datetime、time、calendar)
相关模块 模块 说明 time time是一个仅包含与日期和时间相关的函数和常量的模块,在本模块中定义了C/C++编写的几个类.例如,struct_time类 datetime datetime是一个 ...
- [Python学习笔记][第七章Python文件操作]
2016/1/30学习内容 第七章 Python文件操作 文本文件 文本文件存储的是常规字符串,通常每行以换行符'\n'结尾. 二进制文件 二进制文件把对象内容以字节串(bytes)进行存储,无法用笔 ...
- python/ORM操作详解
一.python/ORM操作详解 ===================增==================== models.UserInfo.objects.create(title='alex ...
- python/数据库操作补充—模板—Session
python/数据库操作补充—模板—Session 一.创建一个app目录 在models.py只能类进行进行创建表 class Foo: xx= 字段(数据库数据类型) 字段类型 字符串 Email ...
- [转]使用python来操作redis用法详解
转自:使用python来操作redis用法详解 class CommRedisBase(): def __init__(self): REDIS_CONF = {} connection_pool = ...
随机推荐
- SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int
--SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int --关键说明:--1.从系统表syscolumns中的查询所有xtype='48'的记录得到类型为[tinyint]的字段- ...
- How to retrieve instance parameters from an uninstantiated (uninserted) family
The trick to be able to read the default values for instance parameters is to get to the FamilyManag ...
- Handling events in an MVVM WPF application
Posted: June 30, 2013 | Filed under: MVVM, WPF, XAML |1 Comment In a WPF application that uses the ...
- ccc 设置图片位置
cc.Class({ extends: cc.Component, properties: { player:{ default:null, type:cc.Sprite } }, // use th ...
- BZOJ1841 : 蚂蚁搬家
树分治,对于每个分治结构,维护两棵线段树. 第一棵按dfs序维护所有点到重心的距离,第二棵维护每个分支的最长链. 那么当前结构对答案的贡献就是第二棵线段树的最大值$+$次大值. 对于操作$0$,如果是 ...
- POJ 2096 (概率DP)
题目链接: http://poj.org/problem?id=2096 题目大意:n种bug,s个子系统.每天随机找一个bug,种类随机,来自系统随机.问找齐n种bug,且每个子系统至少有一个bug ...
- why cpp is a shitty language
// the below is a standard template for any of my writings about c++ cpp_is_a_shitty_language_as { t ...
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- ACM: 限时训练题解-Street Lamps-贪心-字符串【超水】
Street Lamps Bahosain is walking in a street of N blocks. Each block is either empty or has one la ...
- 【BZOJ】1106: [POI2007]立方体大作战tet
题意 给定一个长度为\(2n(1 \le n \le 500000)\)的序列,\(1\)~\(n\)各出现两次,可以交换相邻两项,两个同样的数放在一起会对消,求把所有数对消的最小交换次数. 分析 如 ...