python-时间模块

栏目: Python · 发布时间: 6年前

内容简介:python-时间模块

表示时间的两种方式

  • Timestamp,一个能表示一份数据在某个特定时间之前已经存在的、 完整的、 可验证的数据,通常是一个字符序列,唯一地标识某一刻的时间。时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
  • struct_time,时间元组,共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同time.struct_time(tm_year=2018, tm_mon=1, tm_mday=6, tm_hour=14, tm_min=55, tm_sec=50, tm_wday=5, tm_yday=6, tm_isdst=-1)

常用时间方法

  • 得到当前时间
# 使用time模块,得到当前时间戳
>>> time.time()
1515219161.193089

# 将当前时间转换为时间元组
>>> time.localtime(time.time())
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=6, tm_hour=14, tm_min=15, tm_sec=20, tm_wday=5, tm_yday=6, tm_isdst=0)

# 格式化输出想要时间
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
'2018-01-06 14:19:16'

# 接上文,不加参数时,默认就是输出当前的时间
>>> time.strftime('%Y-%m-%d %H:%M:%S')
'2018-01-06 14:19:51'

# 通过datetime和time模块实现
>>> t = time.time()
>>> datetime.datetime.fromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S')
datetime.datetime.fromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S')
'2018-01-06 14:24:44'

# 只使用datetime模块实现
>>> datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
'2018-01-06 14:35:12'
>>> datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S')
'2018-01-06 14:35:12'
  • 计算时间差
# 使用datetime模块
>>> def calculate_time():
...     start_time = time.time()
...     time.sleep(10)
...     end_time = time.time()
...     end_time - start_time
...
>>> calculate_time()
10.0029308796

# 使用time模块
>>> start_time = datetime.datetime.today()
>>> end_time = datetime.datetime.today()
>>> print (endtime - starttime).seconds
33

# datetime模块timedelta用法
>>> time = datetime.datetime.now()
>>> time
datetime.datetime(2018, 1, 6, 14, 47, 48, 334291)
>>> time + datetime.timedelta(days=2)
datetime.datetime(2018, 1, 8, 14, 47, 48, 334291)

# 时间元组struct_time转换为时间戳
>>> datetime.datetime.now()
datetime.datetime(2018, 1, 6, 14, 51, 33, 721488)
>>> datetime.datetime.now().timetuple()
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=6, tm_hour=14, tm_min=52, tm_sec=0, tm_wday=5, tm_yday=6, tm_isdst=-1)
>>> time.mktime(datetime.datetime.now().timetuple())
1515221695.0

# strptime,将时间字符串转换为时间元组struct_time
>>> time.strftime('%Y-%m-%d %H:%M:%S')
'2018-01-06 14:55:50'
>>> time.strptime('2018-01-06 14:55:50', '%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=6, tm_hour=14, tm_min=55, tm_sec=50, tm_wday=5, tm_yday=6, tm_isdst=-1)

time和datetime模块常用方法

  • time
# time.clock()
“”“这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间 戳)。而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间。(实际上是以WIN32 上QueryPerformanceCounter()为基础,它比毫秒表示更为精确)”“”

#!/usr/bin/env python
import time
if __name__ == '__main__':
    time.sleep(1)
    print "clock1:%s" % time.clock()
    time.sleep(1)
    print "clock2:%s" % time.clock()
    time.sleep(1)
    print "clock3:%s" % time.clock()

# 运行脚本:
clock1:0.059173
clock2:0.059299
clock3:0.059416

# time.sleep(secs)
“”“线程推迟指定的时间运行
适合放在脚本里,定时sleep一会然后继续干啥”“”
>>> while True:
...    time.sleep(3)
...    print time.strftime('%H:%M:%S')
...

16:12:42
16:12:45
16:12:48
16:12:51
16:12:54
16:12:57


# time.localtime([secs])
"""将一个时间戳转换成一个当前时区的struct_time,如果seconds参数未输入,则以当前时间为转换标准"""

# time.strftime(format[, t])
"""将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出"""

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

# time.mktime(t)
"""将一个struct_time转换为时间戳,如下time.localtime接收一个时间戳返回一个struct_time,而time.mktime接收一个struct_time,返回一个时间戳"""
  • datetime

### datetime模块常用的主要有下面这四个类:

  1. datetime.date: 是指年月日构成的日期(相当于日历)
  2. datetime.time: 是指时分秒微秒构成的一天24小时中的具体时间(相当于手表)
  3. datetime.datetime: 上面两个合在一起,既包含时间又包含日期
  4. datetime.timedelta: 时间间隔对象(timedelta)。一个时间点(datetime)加上一个时间间隔(timedelta)可以得到一个新的时间点(datetime)。比如今天的上午3点加上5个小时得到今天的上午8点。同理,两个时间点相减会得到一个时间间隔。

### datetime.date

```python

"""新建一个date对象,日期为今天,既可以直接调用datetime.date.today(),也可以直接向datetime.date()传值"""

datetime.date(2018, 1, 6)  datetime.date(2018, 1, 6)

# datetime.date.strftime(format) 格式化为需要的时间,如常用的 “年-月-日 小时:分钟:秒” 格式

datetime.date.today().strftime('%Y-%m-%d %H:%M:%S')  '2018-01-06 00:00:00'

# datetime.date.timetuple() 转成struct_time格式,这样传递给time.mktime(t) 后,直接转成时间戳格式

datetime.date.today().timetuple()  time.struct_time(tm_year=2018, tm_mon=1, tm_mday=6, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=6, tm_isdst=-1)

# datetime.date.replace(year, month, day) 返回一个替换后的date对象

# datetime.date.fromtimestamp(timestamp) 将时间戳转化为date对象

```

### datetime.time 类

```python

datetime.time(8, 45, 20)  datetime.time(8, 45, 20)
datetime.time(8, 45, 20).strftime('%Y-%m-%d %H:%M:%S')  '1900-01-01 08:45:20'

# datetime.time.replace([hour[, minute[, second[, microsecond[, tzinfo]]]]]) 返回一个替换后的time对象

```

### datetime.datetime类

方法同date和time

### datetime.timedelta类

python # 主要做时间的加减法用 >>> datetime.datetime.today() - datetime.timedelta(days=1) datetime.datetime(2018, 1, 5, 16, 33, 40, 572611) >>> datetime.datetime.today() datetime.datetime(2018, 1, 6, 16, 34, 9, 26989)

原文链接


以上所述就是小编给大家介绍的《python-时间模块》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to the Analysis of Algorithms

An Introduction to the Analysis of Algorithms

Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99

This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器