Python3 time库函数

更多详细time库函数参考 https://docs.python.org/3/library/time.html?highlight=strftime#time

形式:

import time
time.<b>()    #下面即将介绍各种<b>()函数

一、时间获取:

1.time():获取当前计算机内部时间,返回浮点数

>>> time.time()
1524146321.7763612  #看不懂的时间表示。。。

2.ctime():以易读方式获得当前时间,返回字符串

>>> time.ctime()
'Thu Apr 19 21:59:34 2018'   #星期四 2018-4-19 21:59:34

3.gmtime():获取当前时间,返回计算机可处理的时间格式, 以UTC的时间计

>>> time.gmtime()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=19, tm_hour=14, tm_min=1, tm_sec=8, tm_wday=3, tm_yday=109, tm_isdst=0) #不知为什么,这个时间有误。。。

4.localtime():获取计算机当前时间,以当地时间计

>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=20, tm_hour=10, tm_min=12, tm_sec=38, tm_wday=4, tm_yday=110, tm_isdst=0)

二、时间格式化:

1.strftime(tpl,ts):tpl是格式化模板字符串,用来定义输出效果,ts是时间变量

DirectiveMeaningNotes
%aLocale’s abbreviated weekday name. 
%ALocale’s full weekday name. 
%bLocale’s abbreviated month name. 
%BLocale’s full month name. 
%cLocale’s appropriate date and time representation. 
%dDay of the month as a decimal number [01,31]. 
%HHour (24-hour clock) as a decimal number [00,23]. 
%IHour (12-hour clock) as a decimal number [01,12]. 
%jDay of the year as a decimal number [001,366]. 
%mMonth as a decimal number [01,12]. 
%MMinute as a decimal number [00,59]. 
%pLocale’s equivalent of either AM or PM.(1)
%SSecond as a decimal number [00,61].(2)
%UWeek number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.(3)
%wWeekday as a decimal number [0(Sunday),6]. 
%WWeek number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.(3)
%xLocale’s appropriate date representation. 
%XLocale’s appropriate time representation. 
%yYear without century as a decimal number [00,99]. 
%YYear with century as a decimal number. 
%zTime zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. 
%ZTime zone name (no characters if no time zone exists). 
%%A literal '%' character.
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
'2018-04-20 10:34:57'

2.strptime(str,tpl):str是字符串形式的时间值,tpl是格式化模板字符串,定义输出效果

>>> time.strptime('2018-4-19 22:14:15','%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=19, tm_hour=22, tm_min=14, tm_sec=15, tm_wday=3, tm_yday=109, tm_isdst=-1)

三、程序计时:

1.sleep(s):休眠时间,以秒为单位,可以是浮点数,即等待s秒

2.perf_counter():返回一个CPU级别的精确时间计数,单位为秒,通过连续求差值可获得程序执行时间

>>> start=time.perf_counter()
>>> end=time.perf_counter()
>>> end-start
10.702251718395534

                
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pythontime库提供了一些常用的时间函数,包括time.time()、time.strftime()和time.strptime()。 time.time()函数返回当前时间的时间戳,以浮点数类型表示。例如,time.time()的返回值可以是1548826080.93。 time.strftime()函数返回一个指定格式的时间字符串。该函数接受两个参数,第一个参数是时间格式,用来指定输出时间字符串的形式,第二个参数是一个时间结构体,用来指定时间。例如,time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())会返回形如"2019-01-30 14:00:33"的时间字符串。 time.strptime()函数将一个时间字符串解析为一个时间结构化的元组。该函数接受两个参数,第一个参数是要解析的时间字符串,第二个参数是时间格式,用来指定输入时间字符串的形式。例如,time.strptime("2019-01-30","%Y-%m-%d")会返回一个元组,其中包含了年、月、日等时间信息。 总结来说,pythontime库函数包括time.time()、time.strftime()和time.strptime(),分别用于获取当前时间戳、格式化时间字符串和解析时间字符串。使用这些函数可以方便地处理时间相关的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Pythontime库的一些重要函数介绍](https://blog.csdn.net/weixin_42883530/article/details/86701845)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值