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'

[Python3] 027 常用模块 time的更多相关文章

  1. 【Python3之常用模块】

    一.time 1.三种表达方式 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.命令如下 ...

  2. 09 . Python3之常用模块

    模块的定义与分类 模块是什么? 一个函数封装一个功能,你使用的软件可能就是由n多个函数组成的(先备考虑面向对象).比如抖音这个软件,不可能将所有程序都写入一个文件,所以咱们应该将文件划分,这样其组织结 ...

  3. [Python3] 032 常用模块 random

    目录 random 1. random.random() 2. random.choice() 3. random.shuffle() 4. random.randint() 5. random.ra ...

  4. [Python3] 031 常用模块 shutil & zipfile

    目录 shutil 1. shutil.copy() 2. shutil.copy2() 3. shutil.copyfile() 4. shutil.move() 5. 归档 5.1 shutil. ...

  5. [Python3] 030 常用模块 os

    目录 os 1. os.getcwd() 2. os.chdir() 3. os.listdir() 4. os.makedir() 5. os.system() 6. os.getenv() 7. ...

  6. [Python3] 028 常用模块 datetime

    目录 datetime 1. datetime.date 2. datetime.time 3. datetime.datetime 4. datetime.timedelta 补充 datetime ...

  7. [Python3] 029 常用模块 timeit

    目录 timeit 直接举例 1. 测量生成列表的时间 2. 测量函数运行时间(一) 3. 测量函数运行时间(二) timeit 直接举例 必要的导入 import timeit 1. 测量生成列表的 ...

  8. [Python3] 026 常用模块 calendar

    目录 calendar 1. calendar.calendar(year, w, l, c, m) 2. calendar.prcal(year, w, l, c, m) 3. calendar.m ...

  9. python3 常用模块详解

    这里是python3的一些常用模块的用法详解,大家可以在这里找到它们. Python3 循环语句 python中模块sys与os的一些常用方法 Python3字符串 详解 Python3之时间模块详述 ...

随机推荐

  1. web前端_js

    在HTML中可以将JavaScript/JS的代码写在head中,被script标签所包裹,当浏览器解释HTML时,遇到style标签时,按照CSS规则解释,遇到Script标签时,按照JavaScr ...

  2. idea中JSP页面不能访问静态资源(图片,js,css)

    必须配置SpringMvc对访问静态资源的支持,idea默认就是在main/webapp 下的文件路径,要在web-info同级的resource文件下放置,JSP中 ${pageContext.re ...

  3. 51nod-1640--天气晴朗的魔法(简单最小生成树)

    1640 天气晴朗的魔法 题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 这样阴沉的天气持续下去,我们不免担心起他的健康. 51nod魔法学校近日 ...

  4. 微信小程序 — 速学速查笔记

    1. 配置 配置全解析 project.config.json ( 项目配置文件 ) { // 文件描述 "description": "项目配置文件", // ...

  5. docker容器的学习

    什么是docker   Docker 最初是 dotCloud 公司创始人 Solomon Hykes 在法国期间发起的一个公司内部项目,于 2013 年 3 月以 Apache 2.0 授权协议开源 ...

  6. C++入门经典-例5.11-动态分配空间,堆与栈

    1:在程序中定义一个变量,它的值会被放入内存中.如果没有申请动态分配,它的值将会被放在栈中.栈中的变量所属的内存大小是无法被改变的,它们的产生与消亡也与变量定义的位置和存储方式有关.堆是一种与栈相对应 ...

  7. C++入门经典-例2.10-控制输出精确度

    1:代码如下: // 2.10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...

  8. 将文件夹上传到FTP服务器,遍历上传,,,文件夹不能直接上传到FTP服务器上。。。

    <? $ftp_ip = "FTP"; $ftp_user = "user"; $ftp_pwd = "password"; $con ...

  9. 全面解读php-php会话控制技术

    一.PHP会话控制技术 1.为什么要使用会话控制技术? 因为http协议是无状态协议,所以同一个用户在请求同一个页面两次的时候,http协议不会认为这两次请求都来自于同一个用户,会把它们当做是两次请求 ...

  10. pandas dataframe类型操作

    用python做数据分析pandas库介绍之DataFrame基本操作   怎样删除list中空字符? 最简单的方法:new_list = [ x for x in li if x != '' ] 这 ...