zoukankan      html  css  js  c++  java
  • 操作时间相关函数

    # -*- encoding:utf-8 -*-
    """
    __title__ = '操作时间的工具类' 
     """
    import datetime
    import time
    
    
    # ======================
    # ======== time ========
    # ======================
    def getCurrentMilliSecondTime():
        """
        description:  获取当前时间-毫秒级
        :return: 1602230320037 -> str
        """
        timestamps =str(round(time.time()*1000))
        return timestamps
    
    
    
    def getCurrentSecondTime():
        """
        description:  获取当前时间-秒级
        :return: 1602230320 -> str
        """
        timestamps = str(round(time.time()))
        return timestamps
    
    def getCurrentTimeTuple(times=time.time()):
        """
        description:  接受秒级时间戳并返回时间元组(与mktime(tuple)相反)
        :param times: 默认当前时间 可传second
        :return: (tm_year=2020, tm_mon=10, tm_mday=09, tm_hour=16, tm_min=20, tm_sec=28, tm_wday=0, tm_yday=133, tm_isdst=0) -> tuple
        tips:         time.localtime() 不传参则取当前时间
        """
        timestamps = time.localtime(times)
        return timestamps
    
    def getTimeByTuple(tupleTime=time.localtime()):
        """
        description:  接受时间元组并返回秒级时间戳(与localtime(sec)相反)
        :param tupleTime: 默认当前时间的元组 可通过time.localtime() or datetime.datetime.now().timetuple()获取
        :return: 1602230320 -> str
        """
        timestamps = str(round(time.mktime(tupleTime)))
        return timestamps
    
    def getCurrentFormatTimeStr(times=time.time()):
        """
        description:  将指定时间元组格式化为字符串
        :param times: 默认当前时间 可传second
        :return:      2020-10-09 16:00:47 -> str
        tips:         %y 两位数的年份表示(00-99)    %Y 四位数的年份表示(000-9999)   %m 月份(01-12)    %d 月内中的一天(0-31)
                      %H 24小时制小时数(0-23)      %I 12小时制小时数(01-12)        %M 分钟数(00=59)  %S 秒(00-59)   %w 星期(0-6)
        """
    
        timestamps = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(times))
        return timestamps
    
    def getCurrentTimeTupleByFormatStr(time_str=str(datetime.datetime.now()).split(".")[0], format_type="%Y-%m-%d %H:%M:%S"):
        """
        description:         接受格式化字符串返回时间元组
        :param time_str:     格式化字符串   如:2020-10-09 16:00:47    默认当前时间
        :param format_type:  格式化规则    如:%Y-%m-%d %H:%M:%S      默认%Y-%m-%d %H:%M:%S
        :return:             (tm_year=2020, tm_mon=10, tm_mday=09, tm_hour=16, tm_min=20, tm_sec=28, tm_wday=0, tm_yday=133, tm_isdst=0) -> tuple
        """
        return time.strptime(time_str, format_type)
    
    def getCurrentTimeStr():
        """
        description:  获取当前时间的可读形式字符串
        :return:      Fri Oct  9 15:58:40 2020 -> str
        """
        return time.ctime()
    
    def getCurrentTimeStrByTuple(tupleTime=time.localtime()):
        """
        description:      获取指定时间的可读形式字符串
        :param tupleTime: 时间元组 可通过time.localtime() or datetime.datetime.now().timetuple()获取 默认当前时间的元组
        :return:          Fri Oct  9 15:58:40 2020 -> str
        """
        return time.asctime(tupleTime)
    
    def sleepTime():
        """
        description:  推迟调用线程的运行
        """
        for i in range(4):
            print(i)
            time.sleep(3)
    
    # ======================
    # ====== datetime ======
    # ======================
    
    def getNowDateTime():
        """
        description:  获取当前日期&时间
        return:       2020-10-09 15:58:40 -> str
        """
        timestamps = str(datetime.datetime.now()).split(".")[0]
        return timestamps
    
    def getNowTime():
        """
        description:  获取当前时间
        :return:       15:58:40 -> str
        """
        timestamps = str(datetime.datetime.now().time()).split(".")[0]
        return timestamps
    
    def getTodayDate():
        """
        description:  获取当前日期
        return:       2020-10-09 -> str
        tipe:         datetime.datetime.now().date()有相同效果
        """
        timestamps = str(datetime.date.today())
        return timestamps
    
    def getTimeDate(times=time.time()):
        """
        description:  获取指定时间戳的日期
        time:         秒 默认当前时间
        return:       2020-10-09 -> str
        tips:         一天86400秒
        """
        timestamps = str(datetime.date.fromtimestamp(round(times)))
        return timestamps
    
    # 获取距离现在时间的任意时间的日期     正数 加,负数 减  
    def getAnyDateTime(day, hour=0, min=0, sec=0):
        """
        description:  获取距离现在时间的任意时间的日期&时间
        day:          天数 1代表当前时间+1天    -1代表当前时间-1天
        hour:         小时 2代表当前时间+2h     -2代表当前时间-2h     默认=0
        min:          分钟 30代表当前时间+30min -30代表当前时间-30m   默认=0
        sec:          秒   120代表当前时间+120s -120代表当前时间-120s 默认=0
        return:       2020-10-11 15:58:40 -> str
        """
        return str(datetime.datetime.now() + datetime.timedelta(days=day, hours=hour, minutes=min, seconds=sec)).split(".")[0]
    
    def getAnyDateSecondTime(day, hour=0, min=0, sec=0):
        """
        description:  获取距离现在时间的任意时间的秒数
        day:          天数 1代表当前时间+1天    -1代表当前时间-1天
        hour:         小时 2代表当前时间+2h     -2代表当前时间-2h     默认=0
        min:          分钟 30代表当前时间+30min -30代表当前时间-30m   默认=0
        sec:          秒   120代表当前时间+120s -120代表当前时间-120s 默认=0
        return:       1602403120 -> str
        """
        anyDay = datetime.datetime.now() + datetime.timedelta(days=day, hours=hour, minutes=min, seconds=sec)
        return str(round(time.mktime(anyDay.timetuple())))
    
    def getTodayTime():
        """
        description:  获取当天0点的时间戳
        return:       1602172800 -> str
        """
        return str(round(time.mktime(datetime.date.today().timetuple())))
    
    def getCurrentWeekTime():
        """
        description:  获取本周周一0点
        return:       1601827200 -> str
        tips:         可替换成: timestamps = time.mktime(time.strptime(time.strftime("%Y-%m-%d", time.localtime(times)), "%Y-%m-%d"))
        """
        week = int(time.strftime("%w", time.localtime()))
        times = round(time.time()) - (week - 1) * 86400
        timestamps = time.mktime(datetime.date.fromtimestamp(times).timetuple())
        return str(round(timestamps))
    
    def test():
        print(getCurrentMilliSecondTime())
        print(getCurrentSecondTime())
        print(getCurrentFormatTimeStr())
        print(getCurrentTimeTupleByFormatStr())
        print("=======")
        print(getCurrentTimeStr())
        print(getCurrentTimeStrByTuple(time.localtime()))
        print(getTimeByTuple(time.localtime()))
        print("=======")
        print(getNowDateTime())
        print(getNowTime())
        print(getNowDateTime())
        print(getTodayDate())
        print(getTimeDate(time.time() - 86400))
        print("=======")
        print(getAnyDateTime(2))
        print(getAnyDateSecondTime(2))
        print("=======")
        print(getTodayTime())
        print(getCurrentWeekTime())
    
    if __name__ == '__main__':
        print(test())
  • 相关阅读:
    面试点滴
    算法之归并排序
    博客园代码高亮样式更换-测试
    MacOS 10.12 设置找不到 任何来源 的话 这么操作 教程
    HTTP代理协议 HTTP/1.1的CONNECT方法
    Linux命令
    Linux命令
    Linux命令
    vmware虚拟机linux桥接模式设置
    GDB调试 (七)
  • 原文地址:https://www.cnblogs.com/ymlpk/p/13786375.html
Copyright © 2011-2022 走看看