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)

以上是常用的时间封装

最新文章

  1. SQL Server基础知识
  2. Odoo 8.0 new API 之Environment
  3. 去掉文件夹的.svn文件
  4. Java多线程11:ReentrantLock的使用和Condition
  5. 转:PHP中防止SQL注入的方法
  6. 构建高转化率的着陆页-PS+HTML+网络营销
  7. MVC程序实现Autocomplete功能
  8. 部署基于JDK的webservice服务类
  9. Android设置定时执行执行一次任务
  10. 在MyEclipse中编写Web Project,编码设置全集合
  11. Angular开发者指南(一)入门介绍
  12. 51Nod 1110 距离之和最小 V3 中位数 思维
  13. Beta No.6
  14. c提高第四次作业
  15. ES6随手学
  16. 31 ArcGIS中后缀一览表(持续更新中……)
  17. mysql 判断字符串是否有某个字符
  18. Hibernate(二)
  19. Java 多线程(六) synchronized关键字详解
  20. [codeup] 2046 八皇后

热门文章

  1. cookie & maxAge & expires
  2. HTML spaces types:   &   &  
  3. how to fetch html content in js
  4. 「NGK每日快讯」12.2日NGK公链第29期官方快讯!
  5. go-admin在线开发平台学习-4[细节解析]
  6. 为什么Linux需要虚拟内存
  7. C# 类中操作主窗体控件
  8. MySQL如何搭建主库从库(Docker)
  9. .NET测试--模拟框架NSubstitute
  10. EF获取数据库表名和列名