python一些常用时间戳转换

在写完自动化后,发现每个开发对时间的处理都有自己的习惯,只能多写几个方法来

class TimeShift:
    def __init__(self):
        pass

    @staticmethod
    def get_utctime():
        """
        获取当前的UTC时间 实际时间2023-03-08 11:28:05.442681
        :return: str, 2023-03-08 03:28:05.442681
        """
        utc_time = datetime.datetime.utcfromtimestamp(time.time())
        return utc_time

    def utctime_format(self):
        """
        将当前的UTC时间格式化字符串 实际时间20230308112925,一般用于文件取名
        :return: str, 20230308032925
        """
        utc_fmt = self.get_utctime().strftime("%Y%m%d%H%M%S")
        return utc_fmt

    def utctime_ui(self):
        """
        将当前的UTC时间转换成用于前端传入的UTC时间 实际时间20230308112925
        :return: str, 2023-03-28T09:58:51Z
        """
        utc_fmt = self.get_utctime().strftime("%Y-%m-%dT%H:%M:%SZ")
        return utc_fmt

    def utctime_ui_before(self, hours=1):
        """
        将前面1小时的UTC时间转换成用于前端传入的格式化UTC时间 实际时间20230333654
        :return: str, 2023-03-28T09:58:51Z
        """
        utc_fmt = (self.get_utctime() - datetime.timedelta(hours=hours)).strftime("%Y-%m-%dT%H:%M:%SZ")
        return utc_fmt

    def utctime_ui_after(self, hours=1):
        """
        将后面1小时的UTC时间转换成用于前端传入的格式化UTC时间 实际时间20230333654
        :return: str, 2023-03-28T09:58:51Z
        """
        utc_fmt = (self.get_utctime() + datetime.timedelta(hours=hours)).strftime("%Y-%m-%dT%H:%M:%SZ")
        return utc_fmt

    def utctime_ui_quote(self):
        """
        将当前的UTC时间转换成用于前端传入的格式化UTC时间 实际时间20230333229
        :return: str, 2023-03-08T03%3A33%3A29Z
        """
        utc_fmt = quote(self.get_utctime().strftime("%Y-%m-%dT%H:%M:%SZ"))
        return utc_fmt

    def utctime_ui_quote_before(self, hours=1):
        """
        将前面1小时的UTC时间转换成用于前端传入的格式化UTC时间 实际时间20230333654
        :return: str, 2023-03-08T02%3A36%3A54Z
        """
        utc_fmt = quote((self.get_utctime() - datetime.timedelta(hours=hours)).strftime("%Y-%m-%dT%H:%M:%SZ"))
        return utc_fmt

    def utctime_ui_quote_after(self, hours=1):
        """
        将后面1小时的UTC时间转换成用于前端传入的格式化UTC时间 实际时间20230333654
        :return: str, 2023-03-08T04%3A36%3A54Z
        """
        utc_fmt = quote((self.get_utctime() + datetime.timedelta(hours=hours)).strftime("%Y-%m-%dT%H:%M:%SZ"))
        return utc_fmt

    @staticmethod
    def get_localtime():
        """
        获取当前时间
        :return: 2023-03-08 11:37:40.735487
        """
        local_time = datetime.datetime.now()
        return local_time

    @staticmethod
    def get_localtime_after(hours=1, zero=False):
        """
        获取当前时间的后一个小时,一般用于传入到期时间
        :param hours: 固定1小时
        :param zero:是否在最后加.0,兼容用户授权的时间格式
        :return: 2023-03-08 17:07:55.0
        """
        if zero:
            local_time = (datetime.datetime.now() + datetime.timedelta(hours=hours)).strftime("%Y-%m-%d %H:%M:%S.0")
            return local_time
        else:
            local_time = (datetime.datetime.now() + datetime.timedelta(hours=hours)).strftime("%Y-%m-%d %H:%M:%S")
            return local_time

    @staticmethod
    def get_localtime_before(hours=1, zero=False):
        """
        获取当前时间的前一个小时,一般用于传入开始时间
        :param hours: 固定1小时
        :param zero:是否在最后加.0,兼容用户授权的时间格式
        :return: 2023-03-08 15:07:55.0
        """
        if zero:
            local_time = (datetime.datetime.now() - datetime.timedelta(hours=hours)).strftime("%Y-%m-%d %H:%M:%S.0")
            return local_time
        else:
            local_time = (datetime.datetime.now() - datetime.timedelta(hours=hours)).strftime("%Y-%m-%d %H:%M:%S")
            return local_time

    @staticmethod
    def quote_get_localtime_after(hours=1, zero=False):
        """
        获取当前时间的后一个小时,一般用于传入到期时间
        :param hours: 固定1小时
        :param zero:是否在最后加.0,兼容用户授权的时间格式
        :return: 2023-04-18%2010%3A48%3A12
        """
        if zero:
            local_time = quote((datetime.datetime.now() + datetime.timedelta(hours=hours)).strftime("%Y-%m-%d %H:%M:%S.0"))
            return local_time
        else:
            local_time = quote((datetime.datetime.now() + datetime.timedelta(hours=hours)).strftime("%Y-%m-%d %H:%M:%S"))
            return local_time

    @staticmethod
    def quote_get_localtime_before(hours=1, zero=False):
        """
        获取当前时间的前一个小时,一般用于传入开始时间
        :param hours: 固定1小时
        :param zero:是否在最后加.0,兼容用户授权的时间格式
        :return: 2023-04-18%2010%3A48%3A12
        """
        if zero:
            local_time = quote((datetime.datetime.now() - datetime.timedelta(hours=hours)).strftime("%Y-%m-%d %H:%M:%S.0"))
            return local_time
        else:
            local_time = quote((datetime.datetime.now() - datetime.timedelta(hours=hours)).strftime("%Y-%m-%d %H:%M:%S"))
            return local_time

    def localtime_format(self):
        """
        获取当前的格式化时间
        :return: 20230308113757
        """
        local_fmt = self.get_localtime().strftime("%Y%m%d%H%M%S")
        return local_fmt

    def localtime_format_hours(self):
        """
        获取当前时间的分和秒,一般用于修改某些定时的操作
        :return: 15:45
        """
        local_fmt = self.get_localtime().strftime("%H:%M")
        return local_fmt

    @staticmethod
    def time_stamp():
        """
        获取当前的时间戳 当前时间20230308125811
        :return: 1678251491160
        """
        local_fmt = int(round(time.time() * 1000))
        return local_fmt

    @staticmethod
    def time_stamp_before_7days(days=7):
        """
        获取7天前的当前的时间戳 当前时间20230308125811 一般用户前端时间段查询
        :return: 1678251491160
        """
        local_fmt = int(
            round(
                time.mktime(
                    time.strptime(
                        (datetime.datetime.now() - datetime.timedelta(days=days)).strftime("%Y-%m-%d %H:%M:%S.%f"),
                        "%Y-%m-%d %H:%M:%S.%f"))) * 1000)
        return local_fmt

    @staticmethod
    def time_stamp_before_1hours(hours):
        """
        获取1小时前的当前的时间戳 当前时间20230308125811 一般用户前端时间段查询
        :return: 1678251491160
        """
        local_fmt = int(
            round(
                time.mktime(
                    time.strptime(
                        (datetime.datetime.now() - datetime.timedelta(hours=hours)).strftime("%Y-%m-%d %H:%M:%S.%f"),
                        "%Y-%m-%d %H:%M:%S.%f"))) * 1000)
        return local_fmt

    @staticmethod
    def utc2local(utc_st):
        """
        将utc时间转换成本地时间
        :param utc_st: 传入utc时间,2023-03-08 03:28:05.442681
        :return: 返回本地时间,2023-03-08 11:39:20.620266
        """
        now_stamp = time.time()
        local_time = datetime.datetime.fromtimestamp(now_stamp)
        utc_time = datetime.datetime.utcfromtimestamp(now_stamp)
        offset = local_time - utc_time
        local_st = utc_st + offset
        return local_st

    @staticmethod
    def local2utc(local_st):
        """
        将本地时间转成utc时间
        :param local_st:传入本地时间,2023-03-08 11:39:20.620266
        :return: 返回utc时间,2023-03-08 03:40:41
        """
        time_struct = time.mktime(local_st.timetuple())
        utc_st = datetime.datetime.utcfromtimestamp(time_struct)
        return utc_st
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值