zoukankan      html  css  js  c++  java
  • 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)

    以上是常用的时间封装

    ------ 往事如烟,伴着远去的步伐而愈加朦胧。未来似雾,和着前进的风儿而逐渐清晰!
  • 相关阅读:
    Oracle 建用户、 表空间脚本
    Java常见Jar包的用途
    EF:无法检查模型兼容性,因为数据库不包含模型元数据。
    Eclipse -Xms256M -Xmx640M -XX:PermSize=256m -XX:MaxPermSize=768m
    CentOS远程连接Windows操作系统
    spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求
    jvm
    jvm
    spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的?
    spring boot / cloud (十八) 使用docker快速搭建本地环境
  • 原文地址:https://www.cnblogs.com/cutesnow/p/14303059.html
Copyright © 2011-2022 走看看