time

1. 时间戳

  • 一个时间表示,根据不同语言,可以是整数或者浮点数
  • 是从 1970年1月1日0时0分0秒 到现在经历的秒数
  • 如果表示的时间是 1970年以前 或者 太遥远的未来,可能出现异常
  • 32位操作系统 能够支持到 2038年

2. UTC 时间

  • UTC 又称 世界协调时间,以英国的格林尼治天文所在地区的时间作为参考的时间,也叫做 世界标准时间
  • 中国时间是 UTC+8 东八区

3. 夏令时

  • 在夏天的时候将时间调快一小时
  • 本意是督促大家早睡早起节省蜡烛,每天变成 25 个小时,本质没变还是 24 小时

4. 时间元组

  • 一个包含时间内容的普通元组
索引 内容 属性
0 tm_year 2015
1 tm_mon 1~12
2 tm_mday 1~31
3 tm_hour 0~23
4 tm_min 0~59
5 tm_sec 0~61,60表示闰秒,61保留值
6 周几 tm_wday 0~6
7 第几天 tm_yday 1~366
8 夏令时 tm_isdst 0, 1, -1(表示夏令时)

5. 举例

  • 必要的准备工作
>>> import time

5.1 例子1

  • time.timezone

    • 当前时区和 UTC 时间相差的秒数
    • 在没有夏令时的情况下,中(零)时区与东八区相差 -28800s
  • time.altzone

    • 获取当前时区与 UTC 时间相差的秒数
    • 在有夏令时的情况下,中(零)时区与东八区相差 -32400s
  • time.daylight

    • 测当前是否是夏令时时间状态
    • 0 表示是
>>> time.timezone
-28800
>>> time.altzone
-32400
>>> time.daylight
0

例子2

  • time.time

    • 时间戳
  • time.localtime
    • Convert seconds since the Epoch to a time tuple expressing local time.
    • When 'seconds' is not passed in, convert the current time instead.
>>> time.time()
1576070236.623945
>>> time.localtime()
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=11, tm_hour=21, tm_min=17, tm_sec=29, tm_wday=2, tm_yday=345, tm_isdst=0)
>>> t = time.localtime()
>>> t.tm_hour
21

例子3

  • time.asctime

    • Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
    • When the time tuple is not present, current time as returned by localtime() is used.
>>> t1 = time.localtime()
>>> t2 = time.asctime(t1)
>>> type(t2)
<class 'str'>
>>> t2
'Wed Dec 11 21:18:03 2019'

例子4

  • time.ctime

    • Convert a time in seconds since the Epoch to a string in local time.
    • This is equivalent to asctime(localtime(seconds)). When the time tuple is not present, current time as returned by localtime() is used.
>>> t = time.ctime()
>>> type(t)
<class 'str'>
>>> t
'Wed Dec 11 21:19:02 2019'

例子5

  • time.mktime

    • Convert a time tuple in local time to seconds since the Epoch.
    • Note that mktime(gmtime(0)) will not generally return zero for most time zones; instead the returned value will either be equal to that of the timezone or altzone attributes on the time module.
>>> lt = time.localtime()
>>> ts = time.mktime(lt)
>>> type(ts)
<class 'float'>
>>> ts
1576070374.0

例子6

  • time.clock

    • Return the CPU time or real time since the start of the process or since the first call to clock().
    • This has as much precision as the system records.
  • time.sleep
    • Delay execution for a given number of seconds.
    • The argument may be a floating point number for subsecond precision.
# run in ipython
t0 = time.clock()
time.sleep(3)
t1 = time.clock()
print(t1 - t0)

>>>

...
DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
"""Entry point for launching an IPython kernel.
3.0017606999999984
...
This is separate from the ipykernel package so we can avoid doing imports until
  • 既然官方说了,那就用 time.perf_countertime.process_time

例子7

  • strftime

    • Convert a time tuple to a string according to a format specification.
    • When the time tuple is not present, current time as returned by localtime() is used.
  • help(time.strftime)
符号 释义
%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.
用 +HHMM 或 -HHMM 表示距离格林威治的时区偏移
H 代表十进制的小时数,M 代表十进制的分钟数
%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.
  • 补充
符号 释义
%j 一年中的第几天(001 - 366)
%U 一年中的星期数(00 - 53 星期天是一个星期的开始)
第一个星期天之前的所有天数都放在第 0 周
%w 一个星期中的第几天(0 - 6,0 是星期天)
%W 和 %U 基本相同,不同的是 %W 以星期一为一个星期的开始
%x 本地相应日期
%X 本地相应时间
%y 去掉世纪的年份(00 - 99)
%Y 完整的年份
%% %号本身
>>> lt = time.localtime()
>>> ft = time.strftime("%Y-%m-%d %H:%M", lt)
>>> ft
'2019-12-11 21:20'

最新文章

  1. Centos7永久修改主机名
  2. 大型B2B网站开发手记 1
  3. 不用asp.net MVC,用WebForm照样可以实现MVC(请看最后一句话)
  4. js队列
  5. IM即时通讯
  6. OllyICE 调试的程序无法处理异常 解决方法
  7. volley中图片加载
  8. input框自动填充内容背景颜色为黄色解决方法
  9. selenuim ide回放时出现的问题
  10. 教你正确地利用Netty建立连接池
  11. asp.net实现伪静态遇到的问题
  12. Linux内核驱动将多个C文件编译成一个ko文件的方法——每一个C文件中都有module_init与module_exit
  13. 1002. 写这个号码 (20)(数学啊 ZJU_PAT)
  14. 用C#实现字符串相似度算法(编辑距离算法 Levenshtein Distance)
  15. C#构造函数与析构函数--C#基础
  16. octotree-chrome插件,Github代码阅读神器
  17. Kubernetes之Controllers三
  18. 【Spring学习笔记-2.1】Spring的设值注入和构造注入
  19. 010.KVM虚机冷迁移
  20. vCenter Server Virtual Appliance features and benefits

热门文章

  1. [人物存档]【AI少女】【捏脸数据】写实系列
  2. cordova打包遇到Connection timedout:
  3. C语言 - 堆和栈
  4. 10个PHP代码片段
  5. Oracle用函数或PIVOT实现行转列
  6. 微信小程序-获取当前位置
  7. Error:Connection activation failed: No suitable device found for this connection
  8. “fatal error: hdf5.h: 没有那个文件或目录”解决方法
  9. 在一般处理程序中使用session
  10. weblogic报:java.lang.LinkageError: loader constraint violation in interface itable initialization