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)
以上是常用的时间封装