百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

Python3中常用模块-time模块

bigegpt 2024-09-10 11:25 7 浏览

1:获取模块帮助文档

import time
 
print(time.__doc__) 
print(dir(time)) 
for item in dir(time):
    print(item)
 
print(help(time.time))
print(help(time.strftime))
print(help(time.strptime))

2:time模块方法

2.1:time.sleep(t)

time模块最常用的方法之一,用来睡眠或者暂停程序t秒,t可以是浮点数或整数。

2.2:time.time()

import time 
print(time.time())   # 1590908279.7763638

2.3:gmtime([seconds]) 时间戳-->UTC结构化时间

将一个时间戳转换为UTC时区的结构化时间。可选参数secs的默认值为time.time()。

2.4:localtime([seconds]) 时间戳-->本地结构化时间

将一个时间戳转换为当前时区的结构化时间。如果secs参数未提供,则以当前时间为准,即time.time()。

import time
 
print(time.time())   # 1590908279.7763638
 
print(time.gmtime())
print(time.localtime())
  
print('*'*20)
print(time.gmtime(1580908279))
print(time.localtime(1580908279))
 
"""
1590908802.0024452
time.struct_time(tm_year=2020, tm_mon=5, tm_mday=31, tm_hour=7, tm_min=6, tm_sec=42, tm_wday=6, tm_yday=152, tm_isdst=0)
time.struct_time(tm_year=2020, tm_mon=5, tm_mday=31, tm_hour=15, tm_min=6, tm_sec=42, tm_wday=6, tm_yday=152, tm_isdst=0)
********************
time.struct_time(tm_year=2020, tm_mon=2, tm_mday=5, tm_hour=13, tm_min=11, tm_sec=19, tm_wday=2, tm_yday=36, tm_isdst=0)
time.struct_time(tm_year=2020, tm_mon=2, tm_mday=5, tm_hour=21, tm_min=11, tm_sec=19, tm_wday=2, tm_yday=36, tm_isdst=0)
"""

2.5:time.ctime(seconds) 时间戳-->本地格式化字符串

把一个时间戳转化为本地时间的格式化字符串。默认使用time.time()作为参数。

import time
 
print(time.ctime())
print(time.ctime(time.time()))
print(time.ctime(1580908279))
 
"""
Sun May 31 15:12:56 2020
Sun May 31 15:12:56 2020
Wed Feb  5 21:11:19 2020
"""

2.6:time.asctime([tuple]) 结构化时间-->格式化时间字符串

把一个结构化时间转换为Sun May 31 15:15:36 2020这种形式的格式化时间字符串。默认将time.localtime()作为参数。

import time
 
print(time.asctime())
print(time.asctime(time.localtime()))
print(time.asctime(time.localtime(1580908279))) 
"""
Sun May 31 15:15:36 2020
Sun May 31 15:15:36 2020
Wed Feb  5 21:11:19 2020
"""

2.7:time.mktime(tuple) 结构化时间-->时间戳

将一个结构化时间转化为时间戳。time.mktime()执行与gmtime(),localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数表示时间的浮点数。

import time
print(time.mktime(time.localtime()))  # 1590909638.0

2.8:strftime(format[, tuple]) 结构化时间-->时间字符串

把一个struct_time(如time.localtime()和time.gmtime()的返回值)转化为格式化的时间字符串,显示的格式由参数format决定。如果未指定t,默认传入time.localtime()。

import time
print(time.strftime("%Y-%m-%d %H:%M:%S")) 
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())) 
"""
2020-05-31 15:25:37
2020-05-31 07:25:37
"""
 
"""
    %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.
"""

2.9:time.strptime(string, format) 时间字符串-->结构化时间

将格式化时间字符串转化成结构化时间。该方法是time.strftime()方法的逆操作。

import time
stime = "2020-05-31 15:25:37"
st  = time.strptime(stime,"%Y-%m-%d %H:%M:%S")
print(st)
print(time.mktime(st)) # 转换为时间戳
"""
time.struct_time(tm_year=2020, tm_mon=5, tm_mday=31, tm_hour=15, tm_min=25, tm_sec=37, tm_wday=6, tm_yday=152, tm_isdst=-1)
 %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.
"""

3:时间格式转换

3.1:时间格式转换汇总:


# encoding=gbk
import time
# stime 与 format需要对应
def str2time(stime,format="%Y-%m-%d %H:%M:%S"):
	st  = time.strptime(stime,format)
	return time.mktime(st)
	# return time.mktime(time.strptime(stime,format))
 
 
if __name__ == "__main__": 
	print(str2time("2020-05-31 15:58:34" ))
	print(str2time("2020-05-31 15","%Y-%m-%d %H" ))
	print(str2time("2020-05-31","%Y-%m-%d" ))
 
"""
1590911914.0
1590908400.0
1590854400.0
"""

3.2 时间戳-->格式化字符串

# encoding=gbk
import time
 
def time2str(itime,format="%Y-%m-%d %H:%M:%S"):
	st=time.localtime(itime)
	return time.strftime(format,st)
	# return time.strftime(format,time.localtime(itime))
 
 
if __name__ == "__main__": 
	print(time2str(1590911914))
	print(time2str(1590854400,"%Y-%m-%d %H"))
 
 
"""
2020-05-31 15:58:34
2020-05-31 00
"""


相关推荐

Go语言泛型-泛型约束与实践(go1.7泛型)

来源:械说在Go语言中,Go泛型-泛型约束与实践部分主要探讨如何定义和使用泛型约束(Constraints),以及如何在实际开发中利用泛型进行更灵活的编程。以下是详细内容:一、什么是泛型约束?**泛型...

golang总结(golang实战教程)

基础部分Go语言有哪些优势?1简单易学:语法简洁,减少了代码的冗余。高效并发:内置强大的goroutine和channel,使并发编程更加高效且易于管理。内存管理:拥有自动垃圾回收机制,减少内...

Go 官宣:新版 Protobuf API(go pro版本)

原文作者:JoeTsai,DamienNeil和HerbieOng原文链接:https://blog.golang.org/a-new-go-api-for-protocol-buffer...

Golang开发的一些注意事项(一)(golang入门项目)

1.channel关闭后读的问题当channel关闭之后再去读取它,虽然不会引发panic,但会直接得到零值,而且ok的值为false。packagemainimport"...

golang 托盘菜单应用及打开系统默认浏览器

之前看到一个应用,用go语言编写,说是某某程序的windows图形化客户端,体验一下发现只是一个托盘,然后托盘菜单的控制面板功能直接打开本地浏览器访问程序启动的webserver网页完成gui相关功...

golang标准库每日一库之 io/ioutil

一、核心函数概览函数作用描述替代方案(Go1.16+)ioutil.ReadFile(filename)一次性读取整个文件内容(返回[]byte)os.ReadFileioutil.WriteFi...

文件类型更改器——GoLang 中的 CLI 工具

我是如何为一项琐碎的工作任务创建一个简单的工具的,你也可以上周我开始玩GoLang,它是一种由Google制作的类C编译语言,非常轻量和快速,事实上它经常在Techempower的基准测...

Go (Golang) 中的 Channels 简介(golang channel长度和容量)

这篇文章重点介绍Channels(通道)在Go中的工作方式,以及如何在代码中使用它们。在Go中,Channels是一种编程结构,它允许我们在代码的不同部分之间移动数据,通常来自不同的goro...

Golang引入泛型:Go将Interface「」替换为“Any”

现在Go将拥有泛型:Go将Interface{}替换为“Any”,这是一个类型别名:typeany=interface{}这会引入了泛型作好准备,实际上,带有泛型的Go1.18Beta...

一文带你看懂Golang最新特性(golang2.0特性)

作者:腾讯PCG代码委员会经过十余年的迭代,Go语言逐渐成为云计算时代主流的编程语言。下到云计算基础设施,上到微服务,越来越多的流行产品使用Go语言编写。可见其影响力已经非常强大。一、Go语言发展历史...

Go 每日一库之 java 转 go 遇到 Apollo?让 agollo 来平滑迁移

以下文章来源于GoOfficialBlog,作者GoOfficialBlogIntroductionagollo是Apollo的Golang客户端Apollo(阿波罗)是携程框架部门研...

Golang使用grpc详解(golang gcc)

gRPC是Google开源的一种高性能、跨语言的远程过程调用(RPC)框架,它使用ProtocolBuffers作为序列化工具,支持多种编程语言,如C++,Java,Python,Go等。gR...

Etcd服务注册与发现封装实现--golang

服务注册register.gopackageregisterimport("fmt""time"etcd3"github.com/cor...

Golang:将日志以Json格式输出到Kafka

在上一篇文章中我实现了一个支持Debug、Info、Error等多个级别的日志库,并将日志写到了磁盘文件中,代码比较简单,适合练手。有兴趣的可以通过这个链接前往:https://github.com/...

如何从 PHP 过渡到 Golang?(php转golang)

我是PHP开发者,转Go两个月了吧,记录一下使用Golang怎么一步步开发新项目。本着有坑填坑,有错改错的宗旨,从零开始,开始学习。因为我司没有专门的Golang大牛,所以我也只能一步步自己去...