Python常用时间转换
1 import time
2 import math
3
4 # 定义一些时间段的常量(秒)
5 TimeSec_Hour = 3600
6 TimeSec_Day = 86400
7 TimeSec_Week = 604800
8 TimeSec_Month = 2592000
9 TimeSec_Year = 31536000
10
11
12 def timestampToSec(timestamp):
13 if timestampIsMS(timestamp):
14 return round(timestamp / 1000)
15 else:
16 return timestamp
17
18
19 def timestampToMS(timestamp):
20 if timestampIsMS(timestamp):
21 return timestamp
22 else:
23 return timestamp * 1000
24
25
26 def timestampIsMS(timestamp):
27 return timestamp > 1000000000000
28
29
30 def formatDuration(duration, ms=False):
31 if ms:
32 duration = round(duration / 60)
33 sec = duration % 60
34 minute = math.floor((duration % 3600) / 60)
35 hour = math.floor((duration % 86400) / 3600)
36 day = math.floor(duration / 86400)
37 if day > 0:
38 ret = ''.join([str(day), "天"])
39 if hour > 0:
40 ret = ''.join([ret, str(hour), "小时"])
41 elif hour > 0:
42 ret = ''.join([str(hour), "小时"])
43 if minute > 0:
44 ret = ''.join([ret, str(minute), "分钟"])
45 elif minute > 0:
46 ret = ''.join([str(minute), "分钟"])
47 if sec > 0:
48 ret = ''.join([ret, str(sec), "秒"])
49 else:
50 ret = ''.join([str(sec), '秒'])
51 return ret
52
53
54 def datetimeFormat(timestamp, ms=False):
55 if ms:
56 timestamp = int(timestamp / 1000)
57 timeArray = time.localtime(timestamp)
58 return time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
59
60
61 def datetimestrFormat(timestamp, ms=False):
62 if ms:
63 timestamp = int(timestamp / 1000)
64 timeArray = time.localtime(timestamp)
65 return time.strftime("%Y%m%d%H%M%S", timeArray)
66
67
68 def dateFormat(timestamp, ms=False):
69 if ms:
70 timestamp = int(timestamp / 1000)
71 timeArray = time.localtime(timestamp)
72 return time.strftime("%Y-%m-%d", timeArray)
73
74
75 def timeFormat(formatString, timestamp, ms=False):
76 if ms:
77 timestamp = int(timestamp / 1000)
78 timeArray = time.localtime(timestamp)
79 return time.strftime(formatString, timeArray)
80
81
82 def datetimeToStamp(timeString, ms=False):
83 timeArray = time.strptime(timeString, "%Y-%m-%d %H:%M:%S")
84 ret = int(time.mktime(timeArray))
85 if ms:
86 ret = ret * 1000
87 return ret
88
89
90 def dateToStamp(dateString, ms=False):
91 try:
92 timeArray = time.strptime(dateString, "%Y-%m-%d")
93 ret = int(time.mktime(timeArray))
94 except ValueError:
95 ret = 0
96 if ms:
97 ret = ret * 1000
98 return ret
99
100
101 def getTimestamp(ms=False):
102 if ms:
103 return int(round(time.time() * 1000))
104 else:
105 return int(time.time())
106
107
108 def getDayStart(timestamp=0, ms=False):
109 if timestamp == 0:
110 timestamp = int(time.time())
111 elif ms:
112 timestamp = int(timestamp / 1000)
113 midnight = dateToStamp(dateFormat(timestamp))
114 if ms:
115 midnight = midnight * 1000
116 return midnight
117
118
119 def getDayEnd(timestamp=0, ms=False):
120 if ms:
121 timestamp = int(timestamp / 1000)
122 ret = getDayStart(timestamp) + 86399
123 if ms:
124 ret = ret * 1000 + 999
125 return ret
126
127
128 def datetime_format_utc(timestamp):
129 return time.strftime("%Y-%m-%dT%H:%M:%SZ", timestamp)
以上是常用的时间封装
Python常用时间转换的更多相关文章
- Python常用时间操作总结【取得当前时间、时间函数、应用等】转载
Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载 2017-05-11 作者:清风乐逍遥 我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...
- Python基本时间转换
时间转换 python中处理时间的时候,最常用的就是字符形式与时间戳之间的转换. 把最基本的转换在这里记下来 string -> timestamp import time import dat ...
- python3 5月26日 time模块常用时间转换 &datetime()模块学习 random()
import time 获取当前时间: 指定字符串格式:time.strftime("%Y-%m-%d %H:%M:%S") 当前时间戳:time.time() 当前时间元组格式 ...
- Sql 常用时间转换
CONVERT(varchar(100), GETDATE(), 0); -- 08 31 2015 04:57PM CONVERT(varchar(100), GETDATE(), 20); --2 ...
- 【python自动化】python 常用时间获取方法
代码如下: import datetime import time DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" DATE_FORMAT = &quo ...
- js常用时间转换函数
1.秒转换为时分秒 // 秒转换为时分秒 export function secExchangeMoment(value = 0){ let secondTime = parseInt(value); ...
- python 中英文时间转换
上例子:time='Friday, November 18, 2016',将其转换为标准格式: time_format = datetime.datetime.strptime(time, '%A, ...
- Python time datetime常用时间处理方法
常用时间转换及处理函数: import datetime # 获取当前时间 d1 = datetime.datetime.now() print d1 # 当前时间加上半小时 d2 = d1 + da ...
- python中时间格式
问题:通过MySQLdb查询datetime字段,然后通过浏览器显示出来,得到的格式是: 'Thu, 19 Feb 2009 16:00:07 GMT' (http呈现出来的格式) ...
随机推荐
- js double 精度损失 bugs
js double 精度损失 bugs const arr = [ 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01 ]; // [ ...
- TypeORM Entity
TypeORM Entity Entity Entity is a class that maps to a database table when using SQL database(or col ...
- TDD & Unit testing
TDD & Unit testing TDD jest https://github.com/facebook/jest https://facebook.github.io/jest/zh- ...
- vue & table with operation slot
vue & table with operation slot seed demo <!-- @format --> <template> <seed ref=& ...
- 宝塔面板配置Let's Encrypt证书自动续签失效及解决方案
一.背景小故事 笔者手里有个朋友交给我去日常运维项目是PHP+微信小程序,部署在Linux系统上. 这个项目是用宝塔面板去进行日常的可视化运维管理,用起来蛮香的. 如不清楚宝塔的同学,可以自行了解,这 ...
- Redis 日志篇:系统高可用的杀手锏
特立独行是对的,融入圈子也是对的,重点是要想清楚自己向往怎样的生活,为此愿意付出怎样的代价. 我们通常将 Redis 作为缓存使用,提高读取响应性能,一旦 Redis 宕机,内存中的数据全部丢失,假如 ...
- PythonPEP8 风格规范指南
PEP是Python Enhancement Proposal的缩写,通常翻译为"Python增强提案".每个PEP都是一份为Python社区提供的指导Python往更好的方向发展 ...
- Java内存模型(JMM)是什么?JMM 通过控制主内存与每个线程的本地内存之间的交互,来提供内存可见性保证
Java内存模型就是一种符合内存模型规范的,屏蔽了各种硬件和操作系统的访问差异的,保证了Java程序在各种平台下对内存的访问都能保证效果一致的机制及规范. Java内存模型是根据英文Java Memo ...
- Python网络编程相关的库与爬虫基础
PythonWeb编程 ①相关的库:urlib.urlib2.requests python中自带urlib和urlib2,他们主要使用函数如下: urllib: urlib.urlopen() ur ...
- 《Linux学习笔记:文本编辑最佳实践》
[Linux文本编辑的四种方法] 例如,要想test.txt文件添加内容"I am a boy",test.txt在当前目录中 方法一:vi编辑法 [推荐] 打开终端,输入vi t ...