一日一技:python中的time时间模块

liftword4个月前 (12-16)技术文章32

time模块

我在微头条里写过,会写一节关于time模块的内容。今天,我们就来学习time模块。

下面,我将写几个代码实例演示一下,方便我们更好理解。因为time模块是python自带的,所以我们不需要安装,开箱即用,导入:

import time

time.time()

time()函数返回自纪元以来到现在的总秒数。

对于Unix系统,1970年1月1日,UTC的00:00:00是一个纪元(开始时间点)。

那么,我们来计算一下:

import time
seconds = time.time()
print("Seconds since epoch =", seconds)	

输出:

Seconds since epoch = 1592456227.1542935


time.ctime()

我们再来看看time.ctime()方法。,当中的字母c代表current,代表当前时间点。我们用代码举个实例演示一下:

import time

seconds = 1545925769.9618232
local_time = time.ctime(seconds)
print("Local time:", local_time)	

输出:


Local time: Thu Jun 18 12:57:07 2020




time.sleep()

sleep()函数在给定的秒数内暂停或者延迟当前程序的执行,演示如下:

import time

print("This is printed immediately.")
time.sleep(2.4)
print("This is printed after 2.4 seconds.")



time.struct_time

在讨论其他与时间相关的函数之前,让我们再来看看time.struct_time方法。

时间模块中的几个函数(例如gmtime(),asctime()等)将time.struct_time对象作为参数,然后将其返回。

以下是time.struct_time的示例:

time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, 
                    tm_hour=6, tm_min=35, tm_sec=17, 
                    tm_wday=3, tm_yday=361, tm_isdst=0)

以下是time.struct_time方法里面的参数设置,而且使用索引和属性均可访问time.struct_time对象的值(元素):




time.localtime()

localtime()函数将自epoch纪元以来经过的秒数作为参数,并以本地时间返回struct_time,

我们直接来看一个例子:

import time

result = time.localtime(1545925769)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)

输出:

result: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0)

year: 2018
tm_hour: 15

如果未将任何参数或None传递给localtime(),则使用time()返回的值。



time.gmtime()

gmtime()函数将自epoch纪元时间以来经过的秒数作为参数,并以UTC返回struct_time,

以下是演示实例:

import time

result = time.gmtime(1545925769)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)

输出:

result = time.struct_time(tm_year=2018, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=44, tm_sec=4, tm_wday=4, tm_yday=362, tm_isdst=0)

year = 2018
tm_hour = 8

如果没有参数或None传递给gmtime(),则使用time()返回的值。


time.mktime()

mktime()函数将struct_time(包含9个与struct_time对应的元素的元组)作为参数,

并返回自当地时间的纪元以来经过的秒数。

import time

t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)

local_time = time.mktime(t)
print("Local time:", local_time)

输出:

Local time: 1545957844.0



下面的示例显示mktime()和localtime()如何关联的:

import time

seconds = 1545925769

t = time.localtime(seconds)
print("t1: ", t)

s = time.mktime(t)
print("\s:", seconds)

输出:

t1:  time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0)

s: 1545925769.0



time.asctime()

asctime()函数将struct_time(包含9个与struct_time对应的元素的元组)作为参数,

并返回表示它的字符串。 以下是一个例子:

import time

t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)

result = time.asctime(t)
print("Result:", result)

输出:

Result: Fri Dec 28 08:44:04 2018



time.strftime()

strftime()函数将struct_time(或与其对应的元组)作为参数,

并根据所使用的格式代码返回表示它的字符串。 例如:

import time

named_tuple = time.localtime() # get struct_time
time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple)

print(time_string)

输出:

12/28/2018, 09:47:41

上面,%Y,%m,%d,%H等是格式代码。

  • %Y - year [0001,..., 2018, 2019,..., 9999]
  • %m - month [01, 02, ..., 11, 12]
  • %d - day [01, 02, ..., 30, 31]
  • %H - hour [00, 01, ..., 22, 23
  • %M - minutes [00, 01, ..., 58, 59]
  • %S - second [00, 01, ..., 58, 61]

  • time.strptime()

    strptime()函数解析表示时间的字符串并返回struct_time,示例如下:

    import time
    
    time_string = "21 June, 2018"
    result = time.strptime(time_string, "%d %B, %Y")
    
    print(result)

    输出:

    time.struct_time(tm_year=2018, tm_mon=6, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=172, tm_isdst=-1)

    相关文章

    在pandas中保存Excel文件的日期字段(只保留年月日)

    在使用pandas保存日期数据时,我们经常会只希望保存成日期类型,即类似2021-01-16的格式。但是由于pandas的Timestamp类可以处理时分秒,如果不进行处理,默认情况下,在输出成Exc...

    如何写出“高颜值”的Python代码 python好看的代码

    来自:Python大数据分析 费弗里1 简介大家好我是费老师,在日常编写Python代码的过程中,由于个人经验及编程习惯上的差异,有些人写出的代码可读性很高,一眼看上去就非常整洁易懂,而有些人写出的代...

    python内置模块datetime.date类详细介绍

    Python的datetime模块是一个强大的日期和时间处理库,它提供了多个类来处理日期和时间。主要包括几个功能类datetime.date、datetime.time、datetime.datet...

    如何在Python中将字符串转换为DateTime对象

    从原始数据中获取日期时,它们通常采用字符串对象的形式。但在此表单中,您无法访问日期的属性,如年、月等。这个问题的解决方案是将字符串对象解析(或转换)为日期时间对象,以便Python可以将其识别为日期。...