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呈现出来的格式) ...
随机推荐
- Vue 组件之间通信 All in One
Vue 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 https://stackblitz.com/edit/vue-parent-child-commutation?fil ...
- vue & table with operation slot
vue & table with operation slot seed demo <!-- @format --> <template> <seed ref=& ...
- Chrome 80 & SameSite & cookie
Chrome 80 & SameSite & cookie chrome://settings/help https://developers.google.com/web/updat ...
- NGK福利再升级,1万枚VAST限时免费送
NGK在推出持有算力获得SPC空投活动后,福利再升级,于美国加州时间2021年2月8日下午4点推出新人福利活动,注册NGK成为新会员,即可获得0.2枚VAST奖励. VAST免费福利送活动仅送出1万枚 ...
- 应该如何看待VAST的未来价格与价值?
提起数字货币的价格,很多币圈人士都是滔滔不绝,随口一举例,便是百倍千倍的数字货币.可是提起数字货币的价值,就很少有币圈人士能举出几个有力的例子,常常顾左右而言他,场面十分尴尬.之所以会这样,是因为很多 ...
- django学习-21.优化表数据的标题展示
目录结构 1.前言 2.表数据的标题默认展示的数据格式是[模型类名 object(主键名)]的相关信息 3.优化表数据的标题展示的数据格式是[改成我们想要展示的数据格式]的相关完整操作步骤 3.1.第 ...
- vscode 配置表
{ "git.ignoreMissingGitWarning": true, "editor.multiCursorModifier": "ctrlC ...
- 【python3.x】发送自动化测试报告邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.python的smtplib提供了 ...
- css 超过一行省略号
//超过一行省略号 overflow: hidden; white-space: nowrap; text-overflow: ellipsis; //超过两行省略号 overflow: hidden ...
- MySQL基础知识:启动管理和账号管理
整理.记录常用的MySQL基础知识:时间久了,很多就忘记了. 操作系统环境为MacOS Catalina, MySQL版本为: 8.0.13 MySQL Community Server - GPL. ...