Python time 模块的使用

____tz_zs

 

time.struct_time 对象

返回 time.struct_time 对象的函数:gmtime(), localtime(), and strptime()
接收 time.struct_time 对象作为参数的函数:asctime(), mktime() and strftime()

time.localtime(seconds=None)

函数接收一个时间戳(timestamp),生成本地时区的 time.struct_time 对象(时间元组)。如果没有输入值,则相当于输入了当前时间戳。

"""
接收时间戳(timestamp),生成本地时区的 time.struct_time 对象(时间元组)。如果没有输入值,则相当于输入了当前时间戳。
"""

time_array1 = time.localtime()
print type(time_array1)  # <type 'time.struct_time'>
print time_array1  # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=19, tm_hour=11, tm_min=44, tm_sec=1, tm_wday=1, tm_yday=170, tm_isdst=0)

time_array2 = time.localtime(1528113599217683 / 1000000)
print time_array2  # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=19, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=155, tm_isdst=0)

.

time.gmtime(seconds=None)

函数接收一个时间戳(timestamp),生成UTC时区(0时区)的 time.struct_time 对象。如果没有输入值,则相当于输入了当前时间戳。

"""
time.gmtime() 函数将一个时间戳转换为UTC时区(0时区)的struct_time对象,可选的参数seconds表示从1970-1-1以来的秒数。
其默认值为time.time(),函数返回time.struct_time类型的对象。(struct_time是在time模块中定义的表示时间的对象)。
"""

time_gmtime = time.gmtime()
print type(time_gmtime)  # <type 'time.struct_time'>
print time_gmtime  # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=19, tm_hour=4, tm_min=15, tm_sec=29, tm_wday=1, tm_yday=170, tm_isdst=0)

time_gmtime2 = time.gmtime(1528113599217683 / 1000000)
print time_gmtime2  # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=11, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=155, tm_isdst=0)

.

 

struct_time 和字符串的相互转化

常用的格式代码:

%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.
%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.

 

time.strptime(string, format)

函数接收字符串作为参数,按格式将其解析为时间元组(struct_time 对象)。

"""
time.strptime() 函数根据指定的格式把一个时间字符串解析为时间元组,返回struct_time对象。

strptime(string, format) -> struct_time
"""
date_time = time.strptime("2018-06-04 19:59:59", "%Y-%m-%d %H:%M:%S")
print date_time  # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=19, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=155, tm_isdst=-1)

.

time.strftime(format, p_tuple=None)

函数接收时间元组对象 struct_time,格式化为字符串,格式由参数format决定。

"""
    strftime(format[, tuple]) -> string
    
    """

localtime = time.localtime()  # 本地时区时间
print(localtime)  # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=9, tm_hour=18, tm_min=9, tm_sec=47, tm_wday=3, tm_yday=221, tm_isdst=0)
print(time.strftime("%Y-%m-%d %H:%M:%S", localtime))  # 2018-08-09 18:09:47

gmtime = time.gmtime()  # 0时区时间
print(gmtime)  # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=9, tm_hour=10, tm_min=9, tm_sec=47, tm_wday=3, tm_yday=221, tm_isdst=0)
print(time.strftime("%Y-%m-%d %H:%M:%S", gmtime))  # 2018-08-09 10:09:47

.

time.asctime(p_tuple=None)

struct_t = time.localtime(1528113599)
time_asc = time.asctime(struct_t)
print(time_asc)  # Mon Jun  4 19:59:59 2018
print(type(time_asc))  # <class 'str'>

t = (2018, 6, 4, 19, 59, 59, 0, 155, -1)
time_asc2 = time.asctime(t)
print(time_asc2)  # Mon Jun  4 19:59:59 2018
print(type(time_asc2))  # <class 'str'>

.

 

struct_time 和时间戳 timestamp 的相互转换

Unix时间戳(Unix timestamp),也被称为Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。

因此,严格来说,不管你处在地球上的哪个地方,任意时间点的时间戳都是相同的。这点有利于线上和客户端分布式应用统一追踪时间信息。

time.mktime()

函数接收 struct_time 对象作为参数,返回用秒数来表示时间的浮点数 timestamp。

"""
time.mktime() 函数执行与localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数。

如果输入的值不是一个合法的时间,将触发 OverflowError 或 ValueError。
"""

t = (2018, 6, 4, 19, 59, 59, 0, 155, -1)
seconds = time.mktime(t)
print seconds  # 1528113599.0

struct_t = time.localtime(1528113599)
print struct_t  # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=19, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=155, tm_isdst=0)
print time.mktime(struct_t)  # 1528113599.0

.

 

 

time.sleep()

time.sleep(t) 函数推迟调用线程的运行,可通过参数secs指秒数,表示进程挂起的时间。t 为推迟执行的秒数。

import time

print time.ctime()
# Sat Jun 30 12:19:04 2018

time.sleep(2)

print time.ctime()
# Sat Jun 30 12:19:06 2018

.

 

time.time()

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

.

#!/usr/bin/python2.7
# -*- coding:utf-8 -*-

"""
@author:    tz_zs
"""
import time


"""
time.time()  返回当前时间的时间戳(1970纪元后经过的浮点秒数)。
"""
print "time.time(): %f " % time.time()  # time.time(): 1529379388.075540
print time.localtime(
    time.time())  # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=19, tm_hour=11, tm_min=36, tm_sec=28, tm_wday=1, tm_yday=170, tm_isdst=0)
print time.asctime(time.localtime(time.time()))  # Tue Jun 19 11:36:28 2018

.

time.clock()

函数以浮点数计算的秒数返回当前的CPU时间

.

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

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

该函数有两个功能,

在第一次调用的时候,返回的是程序运行的实际时间;

以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔

在win32系统下,这个函数返回的是真实时间(wall time),而在Unix/Linux下返回的是CPU时间。
"""


def procedure():
    time.sleep(2.5)


# measure process time
t0 = time.clock()
procedure()

print time.clock() - t0, "seconds process time"  # 3.9e-05 seconds process time

# measure wall time
t0 = time.time()
procedure()
print time.time() - t0, "seconds wall time"  # 2.50254607201 seconds wall time

.

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值