Python常用时间转换
2024-10-21 04:00:41
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)
以上是常用的时间封装
最新文章
- SQL Server基础知识
- Odoo 8.0 new API 之Environment
- 去掉文件夹的.svn文件
- Java多线程11:ReentrantLock的使用和Condition
- 转:PHP中防止SQL注入的方法
- 构建高转化率的着陆页-PS+HTML+网络营销
- MVC程序实现Autocomplete功能
- 部署基于JDK的webservice服务类
- Android设置定时执行执行一次任务
- 在MyEclipse中编写Web Project,编码设置全集合
- Angular开发者指南(一)入门介绍
- 51Nod 1110 距离之和最小 V3 中位数 思维
- Beta No.6
- c提高第四次作业
- ES6随手学
- 31 ArcGIS中后缀一览表(持续更新中……)
- mysql 判断字符串是否有某个字符
- Hibernate(二)
- Java 多线程(六) synchronized关键字详解
- [codeup] 2046 八皇后
热门文章
- cookie &; maxAge &; expires
- HTML spaces types: &;nbsp; &; &;emsp; &; &;ensp;
- how to fetch html content in js
- 「NGK每日快讯」12.2日NGK公链第29期官方快讯!
- go-admin在线开发平台学习-4[细节解析]
- 为什么Linux需要虚拟内存
- C# 类中操作主窗体控件
- MySQL如何搭建主库从库(Docker)
- .NET测试--模拟框架NSubstitute
- EF获取数据库表名和列名