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常用时间转换的更多相关文章

  1. Python常用时间操作总结【取得当前时间、时间函数、应用等】转载

    Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载  2017-05-11   作者:清风乐逍遥    我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...

  2. Python基本时间转换

    时间转换 python中处理时间的时候,最常用的就是字符形式与时间戳之间的转换. 把最基本的转换在这里记下来 string -> timestamp import time import dat ...

  3. python3 5月26日 time模块常用时间转换 &datetime()模块学习 random()

    import time  获取当前时间: 指定字符串格式:time.strftime("%Y-%m-%d %H:%M:%S") 当前时间戳:time.time() 当前时间元组格式 ...

  4. Sql 常用时间转换

    CONVERT(varchar(100), GETDATE(), 0); -- 08 31 2015 04:57PM CONVERT(varchar(100), GETDATE(), 20); --2 ...

  5. 【python自动化】python 常用时间获取方法

    代码如下: import datetime import time DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" DATE_FORMAT = &quo ...

  6. js常用时间转换函数

    1.秒转换为时分秒 // 秒转换为时分秒 export function secExchangeMoment(value = 0){ let secondTime = parseInt(value); ...

  7. python 中英文时间转换

    上例子:time='Friday, November 18, 2016',将其转换为标准格式: time_format = datetime.datetime.strptime(time, '%A, ...

  8. Python time datetime常用时间处理方法

    常用时间转换及处理函数: import datetime # 获取当前时间 d1 = datetime.datetime.now() print d1 # 当前时间加上半小时 d2 = d1 + da ...

  9. python中时间格式

    问题:通过MySQLdb查询datetime字段,然后通过浏览器显示出来,得到的格式是:         'Thu, 19 Feb 2009 16:00:07 GMT'   (http呈现出来的格式) ...

随机推荐

  1. ES6 arrow function vs ES5 function

    ES6 arrow function vs ES5 function ES6 arrow function 与 ES5 function 区别 this refs xgqfrms 2012-2020 ...

  2. GitHub SSH key

    GitHub SSH key https://help.github.com/en/github/authenticating-to-github steps HTTPS clone !== SSH ...

  3. chown -R & chmod 777 & chmod +x

    chown -R & chmod 777 & chmod +x https://linux.die.net/man/1/chown chown - change file owner ...

  4. github & webhooks

    github & webhooks git auto commit bash shell script https://developer.github.com/webhooks/ POST ...

  5. how to check website offline status in js

    how to check website offline status in js https://developer.mozilla.org/en-US/docs/Web/API/Navigator ...

  6. iOS remote debug & Android remote debug & Chrome & APP

    iOS remote debug & Android remote debug & Chrome & APP iOS remote debugging 如何在 iOS 真机上调 ...

  7. asm align 对齐数据

    最大成员dword data: dd 1 db 2 align 4 dw 3 000E0010 - 01 00 00 00 000E0014 - 02 00 00 00 000E0018 - 03 0 ...

  8. 教你玩转CSS border(边框)

    边框样式 边框样式属性指定要显示什么样的边界. border-style属性用来定义边框的样式 border-style的值 代码演示: <!DOCTYPE html> <html ...

  9. C++算法代码——Sumsets[uva10125]

    题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1278 题目描述 给你一个整数的集合S(里面所有的整数均不相同),请你找出最大的 d, ...

  10. Unity3d 拖拽脚本报错Can't add the script component "" because the script class cannot be found

    解决办法: ①报错原因:文件名与文件内容中的类名不相符. ②关闭360.鲁大师等防护软件,重新安装系统.