前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 日期和时间

Python 日期和时间

作者头像
Python知识大全
发布2020-02-13 14:58:55
2K0
发布2020-02-13 14:58:55
举报
文章被收录于专栏:Python 知识大全Python 知识大全

Python日期和时间

阅读本文需要4分钟

Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能。

Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。

时间间隔是以秒为单位的浮点小数。

每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示。

Python 的 time 模块下有很多函数可以转换常见日期格式。如函数time.time()用于获取当前时间戳, 如下实例:

代码语言:javascript
复制
>>> import time   # 引入time模块
>>> a = time.time()
>>> print(a)
1564997412.3804424

时间戳单位最适于做日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行,UNIX和Windows只支持到2038年。

很多Python函数用一个元组装起来的9组数字处理时间:

序号

字段

0

4位数年

2019

1

1 到 12

2

1到31

3

小时

0到23

4

分钟

5

0到61 (60或61 是闰秒)

6

一周的第几日

0到6 (0是周一)

7

一年的第几日

1到366 (儒略历)

8

夏令时

-1, 0, 1, -1是决定是否为夏令时的旗帜

上述也就是struct_time元组。这种结构具有如下属性:

序号

属性

0

tm_year

2008

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 (0是周一)

7

tm_yday

1 到 366(儒略历)

8

tm_isdst

-1, 0, 1, -1是决定是否为夏令时的旗帜

获取当前时间

从返回浮点数的时间辍方式向时间元组转换,只要将浮点数传递给如localtime之类的函数。

代码语言:javascript
复制
import time
>>> localtime = time.localtime(time.time())
>>> print(localtime)
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=5, tm_hour=17, tm_min=36, tm_sec=33, tm_wday=0, tm_yday=217, tm_isdst=0)

获取格式化的时间

你可以根据需求选取各种格式,但是最简单的获取可读的时间模式的函数是asctime():

代码语言:javascript
复制
>>> import time
>>> localtime = time.asctime( time.localtime(time.time()) )
>>> print(localtime)
'Mon Aug  5 17:39:25 2019'

日期格式化

我们可以使用 time 模块的 strftime 方法来格式化日期,:

代码语言:javascript
复制
import time
>>> localtime = time.asctime( time.localtime(time.time()) )

# 转换为 2019-08-05 17:42:33 格式
>>> print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) )
2019-08-05 17:42:33

# 转换为 Mon Aug 05 17:45:31 2019 格式
>>> print ( time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) )
Mon Aug 05 17:45:31 2019

# 将格式字符串转换为时间戳
>>> print ( time.mktime(time.strptime(localtime,"%a %b %d %H:%M:%S %Y")) )
1564997965.0

python中常用时间日期格式化符号:

  • %y 两位数的年份表示(00-99)
  • %Y 四位数的年份表示(000-9999)
  • %m 月份(01-12)
  • %d 月内中的一天(0-31)
  • %H 24小时制小时数(0-23)
  • %I 12小时制小时数(01-12)
  • %M 分钟数(00=59)
  • %S 秒(00-59)

Time模块

Time模块包含了以下内置函数,既有时间处理相的,也有转换时间格式的:

1 time.altzone

返回格林威治西部的夏令时地区的偏移秒数。如果该地区在格林威治东部会返回负值(如西欧,包括英国)。对夏令时启用地区才能使用。

代码语言:javascript
复制
>>> print( "time.altzone %d " % time.altzone)
time.altzone -32400

2 time.asctime([tupletime])

接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串。

代码语言:javascript
复制
import time
>>> t = time.localtime()
>>> print( "time.asctime(t): %s " % time.asctime(t))
time.asctime(t): Mon Aug  5 17:58:39 2019

3 time.clock( )

用以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用。

代码语言:javascript
复制
>>> def procedure():
...     time.sleep(2.5)
...
>>> # 测量过程的时间
... t0 = time.clock()
>>> procedure()
>>> print (time.clock() - t0)
__main__:1: 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
2.585575399999925 
>>>
>>> # 测量后的时间
... t0 = time.time()
>>> procedure()
>>> print (time.time() - t0)
8.201099157333374 
>>>

4 time.ctime([secs])

作用相当于asctime(localtime(secs)),未给参数相当于asctime()

代码语言:javascript
复制
import time
>>> print( "time.ctime() : %s" % time.ctime())
time.ctime() : Mon Aug  5 18:08:19 2019

5 time.localtime([secs])

接收时间辍(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组t(t.tm_isdst可取0或1,取决于当地当时是不是夏令时)。

代码语言:javascript
复制
>>> import time
>>> t = time.localtime()
>>> print(t)
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=5, tm_hour=18, tm_min=33, tm_sec=31, tm_wday=0, tm_yday=217, tm_isdst=0)

8 time.sleep(secs)

推迟调用线程的运行,secs指秒数。

代码语言:javascript
复制
import time
time.sleep(2, 5) # 睡眠时间在[2, 5)之间

9 time.strftime(fmt[,tupletime])

接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定。

代码语言:javascript
复制
t = (2019, 2, 17, 17, 3, 38, 1, 48, 0)
t = time.mktime(t)
print (time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t)))
# 输出
Feb 18 2019 00:03:38

10 time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')

根据fmt的格式把一个时间字符串解析为时间元组。


11 time.time( )

返回当前时间的时间戳(1970纪元后经过的浮点秒数)。

Time模块包含了以下2个非常重要的属性:

1 time.timezone

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

2 time.tzname

属性time.tzname包含一对根据情况的不同而不同的字符串,分别是带夏令时的本地时区名称,和不带的。

获取某月日历

Calendar模块有很广泛的方法用来处理年历和月历,例如打印某月的月历:

代码语言:javascript
复制
import calendar

cal = calendar.month(2019, 8)
print ("--以下输出2019年8月份的日历--")
print (cal)

--以下输出2019年8月份的日历--
  August 2019
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

END

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python 知识大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 获取当前时间
  • 获取格式化的时间
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档