Python编程:time时间模块

简介: Python编程:time时间模块

image.png

时间戳 timestamp

1970年1月1日计时,unix诞生于1970年,”UNIX元年”

格林威治时间1970年01月01日00时00分00秒起至现在的 总秒数


import time
x = time.time()  # 以时间戳形式,返回当前时间
print(x)  # 1515306968.886664
print(time.clock())  # 0.0
time1 = time.sleep(2)  # 延迟2秒
print(time.clock())    #上次调用clock之后的间隔
# 2.00014532747535

元组形式 struct_time

UTC coordinated universal time 世界标准时间,

格林威治时间Greenwich Mean Time(GMT),中国UTC-8

DST daylight saving time 夏令时

import time
print(time.gmtime())  # 本初子午线时间,格林威治时间
"""
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=7,
tm_hour=9, tm_min=18, tm_sec=51,
tm_wday=6, tm_yday=7, tm_isdst=0)
"""
y = time.localtime()  # 本地时间
print(y)
"""
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=7,
tm_hour=17, tm_min=18, tm_sec=51,
tm_wday=6, tm_yday=7, tm_isdst=0)
"""

字符串形式

import time
z = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  # 格式化时间
print(z)  # 2018-01-07 16:07:47

转换

print(time.asctime(time.localtime()))  # 将元组转为字符串
# Sun Jan  7 17:23:55 2018
print(time.ctime(time.time()))   # 将时间戳转为字符串
# Sun Jan  7 17:24:24 2018
print(time.mktime(time.localtime()))  # 将元组转为时间戳
# 1515317184.0
print(time.strptime(z, "%Y-%m-%d %H:%M:%S"))  # 将字符串转为元组
"""
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=7,
tm_hour=17, tm_min=28, tm_sec=26,
tm_wday=6, tm_yday=7, tm_isdst=-1)
"""
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
"""
<type 'datetime.datetime'> 2018-04-20 11:11:52.007203
<type 'str'> 2018-04-20
"""

时间的加减

import datetime
print(datetime.datetime.now())  # 2018-01-07 17:53:23.579155
print(datetime.datetime.now()+datetime.timedelta(3)) # 默认为天
# 2018-01-10 17:53:23.579155
print(datetime.datetime.now()+datetime.timedelta(hours=3))
# 2018-01-07 20:53:23.579155

help(time)

The tuple items are:
      year (including century, e.g. 1998)
      month (1-12)
      day (1-31)
      hours (0-23)
      minutes (0-59)
      seconds (0-59)
      weekday (0-6, Monday is 0)
      Julian day (day in the year, 1-366)
      DST (Daylight Savings Time) flag (-1, 0 or 1)
Functions:
    time() -- return current time in seconds since the Epoch as a float
    clock() -- return CPU time since process start as a float
    sleep() -- delay for a number of seconds given as a float
    gmtime() -- convert seconds since Epoch to UTC tuple
    localtime() -- convert seconds since Epoch to local time tuple
    asctime() -- convert time tuple to string
    ctime() -- convert time in seconds to string
    mktime() -- convert local time tuple to seconds since Epoch
    strftime() -- convert time tuple to string according to format specification
    strptime() -- parse string to time tuple according to format specification
    tzset() -- change the local timezone
Commonly used format codes:
        %Y  Year with century as a decimal number.
        %m  Month as a decimal number [01,12].
        %d  Day of the month as a decimal number [01,31].
        %H  Hour (24-hour clock) as a decimal number [00,23].
        %M  Minute as a decimal number [00,59].
        %S  Second as a decimal number [00,61].
        %z  Time zone offset from UTC.
        %a  Locale's abbreviated weekday name.
        %A  Locale's full weekday name.
        %b  Locale's abbreviated month name.
        %B  Locale's full month name.
        %c  Locale's appropriate date and time representation.
        %I  Hour (12-hour clock) as a decimal number [01,12].
        %p  Locale's equivalent of either AM or PM.
        Other codes may be available on your platform.  See documentation for
        the C library strftime function.
相关文章
|
2月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
385 7
|
2月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
273 0
|
3月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
319 102
|
3月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
345 104
|
3月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
276 103
|
2月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
219 3
|
2月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
483 3
|
2月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
403 4
|
2月前
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
300 3
|
2月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
312 0

推荐镜像

更多