当前位置:首页 > Python > 正文内容

返回当前时间及格式化已使用时间

lonely4年前 (2021-06-24)Python904
import time
import datetime

# 格式化输出使用时间
class Farmat_time():
    def __init__(self,used_time):
        self.used_time = used_time

    def single_used_time(self):
        used_time1 = datetime.datetime.fromtimestamp(self.used_time,tz=datetime.timezone.utc)
        used_time2 = used_time1.strftime('%M分%S秒')
        return used_time2

    def total_used_time(self):
        used_time1 = datetime.datetime.fromtimestamp(self.used_time,tz=datetime.timezone.utc)
        used_time2 = used_time1.strftime('%H小时%M分%S秒')
        return used_time2

# 返回实时时间
def now_time():
    now_time = time.strftime('%H:%M:%S',time.localtime(time.time()))
    return now_time
    
# 开始时间
start_time = time.time()
time.sleep(5)
# 结束时间
end_time = time.time()
# 得到已使用时间
used_time = end_time - start_time

# 打印当前时间
print('当前时间', now_time())

# 格式化输出使用时间
ftime = Farmat_time(used_time)
print('已使用时间', ftime.single_used_time())


扫描二维码推送至手机访问。

版权声明:本文由复制者发布,如需转载请注明出处。

本文链接:https://www.copyer.cn/post/11.html

分享给朋友:

相关文章

Python pip更换安装源

Python pip更换安装源

常见的镜像源:华为:https://repo.huaweicloud.com/repository/pypi/simple 清华:https://pypi.tuna.tsinghua.edu...

windows更换conda源

windows更换conda源

使用清华源一、cmd中执行conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/an...

windows中配置conda环境变量

windows中配置conda环境变量

一、下载(根据自己的电脑系统下载对应的安装包)官方下载地址:https://www.anaconda.com/products/distribution二、安装下载后双击安装包安装,可以一直下一步默认...

SQLAlchemy中使用sqlacodegen自动同步数据库中表生成model

SQLAlchemy中使用sqlacodegen自动同步数据库中表生成model

前言如果数据库中的表已经存在了,我们只想通过 SQLAlchemy 操作数据库表的数据,不需要建表。这时可以不用一个个声明每个字段类型,可以用第三方包 sqlacodegen 自动...

PyQt5安装

PyQt5安装

下面直接使用pip来安装PyQt5,此处可能是pip/pip3,或者两者皆可,后面不再重复直接pip安装PyQt5pip install PyQt5由于Qt Designer已经在...

conda 删除虚拟环境报错CondaEnvironmentError: cannot remove current environment

conda 删除虚拟环境报错CondaEnvironmentError: cannot remove current environment

conda 删除虚拟环境报错CondaEnvironmentError: cannot remove current environment. deactivate and run conda rem...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。