Python 中的 time 模块

从time 模块的帮助文档中,发现相关的函数主要有如下:

    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

那么就从以上这些函数看看time 模块的功能。

1、time()

这个函数返回的是一个时间戳。为1970年1月1日0时0分0秒为计时起点,到当前的时间长度(不考虑闰秒)。以浮点型标识

In [52]: import time

In [53]: time.time()
Out[53]: 1431868698.641031

In [54]:

这个返回值看起来特别别扭。至少人无法理解到底代表的时什么时间。

2、localtime()

这个函数至少缓和了time函数的尴尬返回值。它能将任何一个时间戳转换成一个tuple,这个tuple 是人能够理解的时间。若默认localtime中没有参数,那么它将取time.time()这个时间戳。

In [56]: time.localtime()
Out[56]: time.struct_time(tm_year=2015, tm_mon=5, tm_mday=17, tm_hour=21, tm_min=23, tm_sec=37, tm_wday=6, tm_yday=137, tm_isdst=0)

In [57]: time.localtime(10000)
Out[57]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=10, tm_min=46, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)

这个tuple 中,每一项的意义如下:

|  tm_hour
     |      hours, range [0, 23]
     |  
     |  tm_isdst
     |      1 if summer time is in effect, 0 if not, and -1 if unknown
     |  
     |  tm_mday
     |      day of month, range [1, 31]
     |  
     |  tm_min
     |      minutes, range [0, 59]
     |  
     |  tm_mon
     |      month of year, range [1, 12]
     |  
     |  tm_sec
     |      seconds, range [0, 61])
     |  
     |  tm_wday
     |      day of week, range [0, 6], Monday is 0
     |  
     |  tm_yday
     |      day of year, range [1, 366]
     |  
     |  tm_year
     |      year, for example, 1993

其中:

tm_wday, tm_yday, tm_isdst 这三个我个人感觉记不住,就特意记录一下。

tm_wday: 一周的第几天。范围为0-6,星期一为第0天,周日为第六天

tm_yday: 一年的第多少天,时间范围为0-366

tm_isdst:代表夏令时

3、gmtime()

localtime()得到的是本地时间,如果要国际化,就最好使用格林威治时间。

gmtime 表现行为和 localtime一致,若不提供时间参数,那么它将获取time.time() 做时间参数。

In [59]: time.gmtime()
Out[59]: time.struct_time(tm_year=2015, tm_mon=5, tm_mday=17, tm_hour=13, tm_min=32, tm_sec=29, tm_wday=6, tm_yday=137, tm_isdst=0)

In [60]: time.gmtime(1000)
Out[60]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=16, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)

In [61]:

4、ctime() 和 asctime()

日常编程中,大家最常用的数据类型肯定有字符串吧。那么怎么将以上的这些时间表示形式转换成字符串的形式去表现呢?

ctime 和 asctime 这两个函数就可以帮助我们完成这样的工作。

首先比较一下ctime 和 asctime 的异同。

相同地方:

这两个函数都返回字符串形式的时间

In [63]: time.ctime()
Out[63]: ‘Sun May 17 21:36:53 2015‘

In [64]: time.asctime()
Out[64]: ‘Sun May 17 21:36:56 2015‘

In [65]:

不同之处:

cimte 函数接受的参数是一个以秒(s)为单位的时间。若默认不提供参数,则获取time.time()

asctime 函数接受的是一个表示时间的tuple。若默认不提供参数,则获取的时time.localtime()

In [66]: time.ctime()
Out[66]: ‘Sun May 17 21:40:08 2015‘

In [67]: time.ctime(1000)
Out[67]: ‘Thu Jan  1 08:16:40 1970‘

In [68]: time.asctime()
Out[68]: ‘Sun May 17 21:40:34 2015‘

In [69]: t = time.localtime(1000)

In [70]: time.asctime(t)
Out[70]: ‘Thu Jan  1 08:16:40 1970‘

5、strftime() 和 strptime()
现在time() 、localtime() 等返回的时间格式都可以转换成字符串形式的时间了。不过,这个字符串的格式不能调整。假如我想时间以另外一种字符串的格式去表示呢?

这是就可以使用strftime 这个函数了。strftime 这个函数的参数时一个tuple 类型的时间。若不给参数,那么它将获取time.localtime() 作为参数.

In [86]: time.strftime(‘%Y-%m-%d‘)
Out[86]: ‘2015-05-17‘

In [87]: t = time.localtime(1000)
In [88]: time.strftime(‘%Y-%m-%d‘,t)
Out[88]: ‘1970-01-01‘

每个人想要的表示时间的格式不一样。其实这里重点的应该时如何去获取格式化参数每一项的意思.

在python 本身的帮助中我暂时没有找到这些说明,不过从linux 系统函数strftime 中我找到了每一项的说明。 man strftime 就可以看到。或者大家从网络上也可以随意的搜索到。

strptime 这个函数,做的正好时strftime的反向工作:

In [4]: t = time.strftime(‘%Y-%m-%d‘)

In [5]: time.strptime(t,‘%Y-%m-%d‘)
Out[5]: time.struct_time(tm_year=2015, tm_mon=5, tm_mday=17, tm_hour=0, 
tm_min=0, tm_sec=0, tm_wday=6, tm_yday=137, tm_isdst=-1)
时间: 2024-08-04 04:35:35

Python 中的 time 模块的相关文章

Python中的random模块,来自于Capricorn的实验室

Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniform random.uniform的函数原型为:random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限.如果a > b,则生成的随机数n: a <= n <= b.如果 a <

python中查看可用模块

1.这种方式的问题是,只列出当前import进上下文的模块. 进入python命令行.输入以下代码: >>>import sys >>>sys.modules 2.在python命令行下输入: >>>help() help>modulespython中查看可用模块,布布扣,bubuko.com

python中动态导入模块

如果导入的模块不存在,Python解释器会报 ImportError 错误: >>> import something Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named something 有的时候,两个不同的模块提供了相同的功能,比如 StringIO 和 cStringIO 都提供了Strin

Python中的random模块

Python中的random模块 (转载自http://www.cnblogs.com/yd1227/archive/2011/03/18/1988015.html) Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniform random.uniform的函数原型为:random.uniform(a, b),

解决linux系统下python中的matplotlib模块内的pyplot输出图片不能显示中文的问题

问题: 我在ubuntu14.04下用python中的matplotlib模块内的pyplot输出图片不能显示中文,怎么解决呢? 解决: 1.指定默认编码为UTF-8: 在python代码开头加入如下代码 import sys reload(sys) sys.setdefaultencoding('utf-8') 2.确认你ubuntu系统环境下拥有的中文字体文件: 在终端运行命令"fc-list :lang=zh",得到自己系统的中文字体 命令输出如下: /usr/share/fon

(转)Python中的random模块

Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniform random.uniform的函数原型为:random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限.如果a > b,则生成的随机数n: a <= n <= b.如果 a <

转载:python中的copy模块(浅复制和深复制)

主要是介绍python中的copy模块. copy模块包括创建复合对象(包括列表.元组.字典和用户定义对象的实例)的深浅复制的函数. ########copy(x)########创建新的复合对象并通过引用复制x的成员来创建x的浅复制.更加深层次说,它复制了对象,但对于对象中的元素,依然使用引用.对于内置类型,此函数并不经常使用.而是使用诸如list(x), dict(x), set(x)等调用方式来创建x的浅复制,要知道像这样直接使用类型名显然比使用copy()快很多.但是它们达到的效果是一样

Python中的logging模块【转】

基本用法 下面的代码展示了logging最基本的用法. 1 # -*- coding: utf-8 -*- 2 3 import logging 4 import sys 5 6 # 获取logger实例,如果参数为空则返回root logger 7 logger = logging.getLogger("AppName") 8 9 # 指定logger输出格式 10 formatter = logging.Formatter('%(asctime)s %(levelname)-8s:

Python中的logging模块

http://python.jobbole.com/86887/ 最近修改了项目里的logging相关功能,用到了python标准库里的logging模块,在此做一些记录.主要是从官方文档和stackoverflow上查询到的一些内容. 官方文档 技术博客 基本用法 下面的代码展示了logging最基本的用法. 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 32 33 34

在Python中调用C++模块

首先,这是自我转载:YellowTree | STbioinf的文章「在Python中调用C++模块」 在Python中成功实现了对原来C++代码模块的复用!这个好处多多,Python写得快,C++跑得快,那就是既快又快了!方法很简单,以至于我能够用一张截图记录下整个过程(点击图片看大图)! 其实,注意到,必须在原来的C++代码后面添加extern “C”来辅助(C则不需要,这也是与复用C代码时最大的不同点),不然Python在调用这个构建后的动态链接库时是找不到原来的方法或者函数的,说到底还都