python time库

一、简介:

时间戳:

  • 时间戳是指 1970 年 01 月 01 日 00 时 00 分 00 秒起至现在的总毫秒数。
  • 1970年之前的日期就无法以此表示。太遥远的日期也不行, UNIX 和 Windows 只支持到 2038 年。

为什么会生成13位的时间戳,13位的时间戳和10时间戳分别是怎么来的?

  • java的date默认精度是毫秒,也就是说生成的时间戳就是13位的,而像 c++ 或者 Python、php 生成的时间戳默认就是 10 位的,因为其精度是秒。

13位时间戳如何转换成10位时间戳?

  • 第一种:将13位的时间戳最后三位数字截取
  • 第二种:将13位时间戳除以1000取整。

Unix纪元时间:

  • 很多编程语言起源于UNIX系统,而UNIX系统认为1970年1月1日0时0分0秒是时间纪元,所以常说的UNIX时间戳是以1970年1月1日0时0分0秒为计时起点时间的。

UTC(协调世界时):

  • 世界统一时间、世界标准时间、国际协调时间。简称 UTC,在中国为 UTC+8(小时)

DST(夏令时):

  • 时间使用策略。人为的将时间调快一小时,可以使人早睡早起,减少照明量,以充分利用光照资源从而节约照明用电。

时间元组:

  • 以元组的方式表示时间,如:(2020, 1, 2, 3, 40, 50, 6, 7, 0)

时间元组对照表:

索引值 属性
0 4位数(2020)
1 1~12
2 1~31
3 0~23
4 0~59
5 0~61(闰秒 [1]
6 星期几 0~6(0表示星期一)
7 一年中的第几天 1~366
8 是否为夏令时 0,1,-1(-1代表夏令时)

二、常用函数

time.asctime(test)
 // 接受时间元组,并以可读的形式返回“Thu Feb 20 02:20:20 2020“(2020年2月20日 星期四 2时20分20秒)的字符串

time.ctime()
 // 获取本地时间,并以可读的形式返回(“Thu Feb 20 02:20:20 2020“)

time.localtime()
 // 返回本地的时间(UTC+8)

time.localtime(0)
 // 返回到Unix纪元时间向后偏移0秒的时间,由于当前属于东八区所以+8小时(1970/1/1 8:0:0 星期四 一年中的第一天)

time.gmtime()
 // 返回本地的时间(UTC时间)

time.gmtime(0)
 // 返回Unix纪元时间向后偏移0秒的时间(1970/1/1 0:0:0 星期四 一年中的第一天)

time.time()
 // 返回当前时间的时间戳

time.strftime("%Y-%m-%d %H:%M:%S %A", test)
 // 格式化时间字符串

time.strptime('2020/2/20 2:20:20', "%Y/%m/%d %H:%M:%S")
 // 根据指定的格式把一个时间字符串解析为时间元组

test = time.strftime("%Y/%m/%d %H:%M:%S",time.localtime(time.time()))
 // 获取当前时间戳并转化为可读字符串

<table border="0" cellpadding="0" cellspacing="0" width="529" style="border-collapse: collapse;table-layout:fixed;397pt"> <colgroup><col width="33" style="mso-width-source:userset;mso-width-alt:1166;25pt"> <col width="54" style="mso-width-source:userset;mso-width-alt:1934;41pt"> <col width="442" style="mso-width-source:userset;mso-width-alt:15701;331pt"> </colgroup><tbody><tr height="18" style="height:13.8pt"> <td height="18" class="xl83" width="33" style="height:13.8pt;25pt"> </td> <td class="xl83" width="54" style="border-left:none;41pt">格式</td> <td class="xl83" width="442" style="border-left:none;331pt">说明</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td rowspan="3" height="123" class="xl65" style="height:93.15pt;border-top:none">年</td> <td class="xl67" width="54" style="border-top:none;border-left:none;41pt">%Y</td> <td class="xl69" width="442" style="border-top:none;border-left:none;331pt">四个数字表示的年份(2020)</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl67" width="54" style="height:31.05pt;border-top:none; border-left:none;41pt">%y</td> <td class="xl69" width="442" style="border-top:none;border-left:none;331pt">两个数字表示的年份(20)</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl67" width="54" style="height:31.05pt;border-top:none; border-left:none;41pt">%j</td> <td class="xl69" width="442" style="border-top:none;border-left:none;331pt">一年中的第几天(001-366)</td> </tr> <tr height="22" style="mso-height-source:userset;height:16.2pt"> <td colspan="3" height="22" class="xl84" style="height:16.2pt"> </td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td rowspan="3" height="123" class="xl85" style="height:93.15pt">月</td> <td class="xl72" width="54" style="41pt">%m</td> <td class="xl73" width="442" style="border-left:none;331pt">月份(0, 12)</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl66" width="54" style="height:31.05pt;border-top:none; 41pt">%b</td> <td class="xl68" width="442" style="border-top:none;border-left:none;331pt">月份的英文单词缩写:如一月,则返回 <font class="font7"><span style="mso-spacerun:yes">&nbsp;</span>' </font><font class="font8">Jan</font><font class="font7"> '</font></td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl74" width="54" style="height:31.05pt;border-top:none; 41pt">%B</td> <td class="xl75" width="442" style="border-top:none;border-left:none;331pt">月份的英文单词全拼:如一月,则返回 <font class="font7"><span style="mso-spacerun:yes">&nbsp;</span>' </font><font class="font8">January</font><font class="font7"> '</font></td> </tr> <tr height="18" style="height:13.8pt"> <td colspan="3" height="18" class="xl84" style="height:13.8pt"> </td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl78" style="height:31.05pt">日</td> <td class="xl79" width="54" style="41pt">%d</td> <td class="xl80" width="442" style="border-left:none;331pt">当前月的第几天(01-31)</td> </tr> <tr height="21" style="mso-height-source:userset;height:15.6pt"> <td colspan="3" height="21" class="xl84" style="height:15.6pt"> </td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td rowspan="3" height="123" class="xl85" style="height:93.15pt">时</td> <td class="xl76" width="54" style="41pt">%H</td> <td class="xl77" width="442" style="border-left:none;331pt">以24小时制表示当前小时</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl71" width="54" style="height:31.05pt;border-top:none; 41pt">%I</td> <td class="xl70" width="442" style="border-top:none;border-left:none;331pt">以12小时制表示当前小时</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl71" width="54" style="height:31.05pt;border-top:none; 41pt">%p</td> <td class="xl70" width="442" style="border-top:none;border-left:none;331pt">返回上午(AM)或者下午(PM)</td> </tr> <tr height="19" style="height:14.4pt"> <td colspan="3" height="19" class="xl84" style="height:14.4pt"> </td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl65" style="height:31.05pt;border-top:none">分</td> <td class="xl71" width="54" style="41pt">%M</td> <td class="xl70" width="442" style="border-left:none;331pt">返回分钟数(0,59)</td> </tr> <tr height="19" style="height:14.4pt"> <td colspan="3" height="19" class="xl84" style="height:14.4pt"> </td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl65" style="height:31.05pt;border-top:none">秒</td> <td class="xl71" width="54" style="41pt">%S</td> <td class="xl70" width="442" style="border-left:none;331pt">返回秒数(0,61)</td> </tr> <tr height="19" style="height:14.4pt"> <td colspan="3" height="19" class="xl84" style="height:14.4pt"> </td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td rowspan="5" height="205" class="xl65" style="height:155.25pt;border-top:none">星期</td> <td class="xl71" width="54" style="41pt">%U</td> <td class="xl70" width="442" style="border-left:none;331pt">返回现在是当前年的第几周(周日为第一天)</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl71" width="54" style="height:31.05pt;border-top:none; 41pt">%W</td> <td class="xl70" width="442" style="border-top:none;border-left:none;331pt">返回现在是当前年的第几周(周一为第一天)</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl71" width="54" style="height:31.05pt;border-top:none; 41pt">%w</td> <td class="xl70" width="442" style="border-top:none;border-left:none;331pt">返回当前是星期几(0~6),6表示星期天</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl71" width="54" style="height:31.05pt;border-top:none; 41pt">%a</td> <td class="xl70" width="442" style="border-top:none;border-left:none;331pt">星期的英文单词缩写:如星期一,则返回<span style="mso-spacerun:yes">&nbsp; </span>' Mon '<span style="mso-spacerun:yes">&nbsp;</span></td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl81" width="54" style="height:31.05pt;border-top:none; 41pt">%A</td> <td class="xl82" width="442" style="border-top:none;border-left:none;331pt">星期的英文单词全拼:如星期一,则返回<span style="mso-spacerun:yes">&nbsp; </span>' Monday '<span style="mso-spacerun:yes">&nbsp;</span></td> </tr> <tr height="18" style="height:13.8pt"> <td colspan="3" height="18" class="xl84" style="height:13.8pt"> </td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td rowspan="5" height="205" class="xl85" style="height:155.25pt">其他</td> <td class="xl76" width="54" style="41pt">%c</td> <td class="xl77" width="442" style="border-left:none;331pt">返回本地相应的日期和时间如<span style="mso-spacerun:yes">&nbsp; </span>' Fri Feb 20 02:20:20 2020 '</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl71" width="54" style="height:31.05pt;border-top:none; 41pt">%x</td> <td class="xl70" width="442" style="border-top:none;border-left:none;331pt">日期的字符串表示<span style="mso-spacerun:yes">&nbsp; </span>' <font class="font7">02/20/20</font><font class="font8"> '<span style="mso-spacerun:yes">&nbsp;&nbsp;</span></font></td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl71" width="54" style="height:31.05pt;border-top:none; 41pt">%X</td> <td class="xl70" width="442" style="border-top:none;border-left:none;331pt">时间的字符串表示<span style="mso-spacerun:yes">&nbsp; </span>' <font class="font7">02:20:20</font><font class="font8"> '<span style="mso-spacerun:yes">&nbsp;&nbsp;</span></font></td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl71" width="54" style="height:31.05pt;border-top:none; 41pt">%z</td> <td class="xl70" width="442" style="border-top:none;border-left:none;331pt">用 +HHMM<span style="mso-spacerun:yes">&nbsp; </span>或<span style="mso-spacerun:yes">&nbsp; </span>-HHMM<span style="mso-spacerun:yes">&nbsp; </span>表示距离格林威治(时间时)的时区偏移( H 代表十进制的小时数,M 代表十进制的分钟数)</td> </tr> <tr height="41" style="mso-height-source:userset;height:31.05pt"> <td height="41" class="xl71" width="54" style="height:31.05pt;border-top:none; 41pt">%Z</td> <td class="xl70" width="442" style="border-top:none;border-left:none;331pt">时区的名字(如果不存在,则返回空字符)</td> </tr> <!--[if supportMisalignedColumns]--> <tr height="0" style="display:none"> <td width="33" style="25pt"></td> <td width="54" style="41pt"></td> <td width="442" style="331pt"></td> </tr> <!--[endif]--> </tbody></table> <tbdiv id="picinpicBtn"></tbdiv><tbdiv id="playerControlBtn"></tbdiv><tbdiv id="leftFullStackButton"></tbdiv><tbdiv id="rightFullStackButton"></tbdiv></body>

time.mktime(time.localtime())
 // 获取当前时间并转化为时间戳

time.timezone
 // 当地时区(未启动夏令时)距离格林威治(时间时)的偏移秒数(美洲 >0 ;大部分欧洲、亚洲、非洲 <=0)

time.tzname
 // 包含两个字符串的元组:

  • 第一个是当地非夏令时区的名称
  • 第二个是当地的 DST(夏令时) 时区的名称

time.sleep(5)
 // 程序暂停 5 秒后继续运行

time.perf_counter()
 // cpu计时器也是用于计算代码的执行时间


三、实例

1、以可读形式返回字符串

#  接收时间元组,并以可读的形式返回 “Thu Feb 20 02:20:20 2020“ 格式的字符串
test = (2020, 2, 20, 2, 20, 20, 0, 0, 0)
test_2 = time.asctime(test)
print (test_2)

2、接收时间元组,并转换为时间戳

#  接收时间元组,并转换为时间戳
test = (2020, 2, 20, 2, 20, 20, 0, 0, 0)
test_2 = time.mktime(test)
print (test_2)

3、以指定格式,格式化时间元组

#  以指定格式,格式化时间元组
test = (2020, 2, 20, 2, 20, 20, 0, 0, 0)
test_2 = time.strftime("%Y-%m-%d  %H:%M:%S  %A", test)
print (test_2)

4、从已格式化的字符串中提取时间元组

#  从已经格式化的字符串中提取时间元组
test1 = '2020/2/20 2:20:20'
time_format = "%Y/%m/%d  %H:%M:%S"
print (time.strptime(test1, time_format))

5、获取当前时间并转化为可读的字符串格式

#  获取当前时间并转化为可读的字符串格式
test = time.localtime(time.time())
time_format = "%Y/%m/%d %H:%M:%S"
test_2 = time.strftime(time_format, test)
print (test_2)

6、获取当前时间并转化为时间戳

#  获取当前时间并转化为时间戳
test = time.localtime()
test_2 = int(time.mktime(test))
print (test_2)

  1. 1 ↩︎

原文地址:https://www.cnblogs.com/anonymous-test/p/13608259.html