Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python time

作者:╃為伱變壞メ   发布日期:2025-12-01   浏览:72

import time

# 获取当前时间的时间戳
current_time = time.time()
print(f"当前时间的时间戳: {current_time}")

# 将时间戳转换为可读的日期格式
readable_time = time.ctime(current_time)
print(f"可读的日期格式: {readable_time}")

# 使用 struct_time 对象表示时间
struct_time = time.localtime(current_time)
print(f"本地时间 (struct_time): {struct_time}")

# 格式化输出时间
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", struct_time)
print(f"格式化后的时间: {formatted_time}")

# 等待一段时间(秒)
print("等待 2 秒...")
time.sleep(2)
print("2 秒已过")

# 测量代码执行时间
start_time = time.time()
for i in range(1000000):
    pass
end_time = time.time()
execution_time = end_time - start_time
print(f"代码执行时间: {execution_time} 秒")

解释说明:

  1. time.time(): 返回当前时间的时间戳(自纪元以来的秒数)。
  2. time.ctime(): 将时间戳转换为可读的日期格式。
  3. time.localtime(): 将时间戳转换为本地时间的 struct_time 对象。
  4. time.strftime(): 格式化输出时间,可以根据指定的格式字符串生成时间字符串。
  5. time.sleep(): 暂停程序执行指定的秒数。
  6. 测量代码执行时间: 使用 time.time() 记录开始和结束时间,计算差值以得到代码执行时间。

上一篇:python运行

下一篇:python获取当前时间

大家都在看

python时间格式

pythoneval函数用法

python读取文件路径

staticmethod在python中有

python开发windows应用程序

python中len是什么意思

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站