二十三、时间系列之生成时间戳

文章介绍了如何使用Python实现一个类,用于处理时间戳,包括格式化输出到年月日时分秒,以及根据给定条件返回不同格式的时间字符串,支持精确到秒或毫秒。
摘要由CSDN通过智能技术生成

返回当前的时间戳或者自己传入的时间对应的时间戳

import datetime
import time

class Time:


    def second(self, add_second=0, value=True,now_time=""):
        """
        返回年月日 时分秒
        :param add_second: 秒可加可减
        :param value: true 格式 ---- , false 格式 
        :return:
        """

        second_total = 0
        year_month_day_hour_minute_second = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        if now_time:
            second_total += self.poor(now_time,year_month_day_hour_minute_second)

        second = year_month_day_hour_minute_second[17:19]
        second_total += add_second + int(second)
        multiple = second_total // 60

        # 要返回的时间属于过去至最近的1分钟
        if second_total < -60:
            year_month_day_hour = self.minute(multiple)
            minute = self.__format_processing(abs(second_total % 60), val=2)
            if not value:
                req = (year_month_day_hour + minute).split('-')
                return req[0] + '/' + req[1] + '/' + req[2]

            return year_month_day_hour + minute

        # 要返回的时间属于最近过去的一分钟
        elif -60 <= second_total <= 0:
            if second_total == 0:
                year_month_day_hour = self.minute(0)
                minute = ':00'
            else:
                year_month_day_hour = self.minute(-1)
                minute = self.__format_processing(str(second_total + 60), val=2)

            if not value:
                req = (year_month_day_hour + minute).split('-')
                return req[0] + '/' + req[1] + '/' + req[2]
            return year_month_day_hour + minute

        # 要返回的时间属于当前这分钟
        elif 0 < second_total <= 60:
            if second_total == 60:
                if not value:
                    req = (self.minute(1) + ':00').split('-')
                    return req[0] + '/' + req[1] + '/' + req[2]
                return self.minute(1) + ':00'
            else:
                if not value:
                    req = (year_month_day_hour_minute_second[0:16] + self.__format_processing(second_total,
                                                                                             val=2)).split('-')
                    return req[0] + '/' + req[1] + '/' + req[2]
                return year_month_day_hour_minute_second[0:16] + self.__format_processing(second_total, val=2)

        # 要返回的时间属于1分钟以后
        elif 60 < second_total:
            year_month_day = self.minute(multiple)
            minute = self.__format_processing(second_total % 60, val=2)
            if not value:
                req = (year_month_day + minute).split('-')
                return req[0] + '/' + req[1] + '/' + req[2]
            return year_month_day + minute

    def stamp(self, times=None,precision=True):
        """
        返回当前时间戳
        :param times: time为空默认当前时间戳。也可以自己传入值
        :param precision: 精度默认返回秒 否则返回毫秒
        :return:
        """

        if times is None:
            if precision:
                data = time.strptime(self.second(), "%Y-%m-%d %H:%M:%S")
            else:
                data = time.strptime(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'), "%Y-%m-%d %H:%M:%S.%f")
        else:
            if precision:
                data = time.strptime(times, "%Y-%m-%d %H:%M:%S")
            else:
                data = time.strptime(times, "%Y-%m-%d %H:%M:%S.%f")
        return int(time.mktime(data))

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bug来袭~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值