欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python 时间常用函数及结构

程序员文章站 2022-06-20 18:15:24
class time.struct_time 定义 the type of the time value sequence returned by gmtime(), loc...

class time.struct_time 定义


the type of the time value sequence returned by gmtime(), localtime(), and strptime(). it is an object with a named tuple interface: values can be accessed by index and by attribute name. the following values are present:


index  | attribute   values
---------------------------------------------------------------------
0   | tm_year    (for example, 1993)
1   | tm_mon     range [1, 12]
2   | tm_mday    range [1, 31]
3   | tm_hour    range [0, 23]
4   | tm_min     range [0, 59]
5   | tm_sec     range [0, 61]; see (2) in strftime() description
6   | tm_wday    range [0, 6], monday is 0
7   | tm_yday    range [1, 366]
8   | tm_isdst    0, 1 or -1; see below
n/a   | tm_zone    abbreviation of timezone name
n/a   | tm_gmtoff   offset east of utc in seconds
---------------------------------------------------------------------


time.time()
return the time in seconds since the epoch as a floating point number


time.gmtime([secs]) 0时区的时间
convert a time expressed in seconds since the epoch to a struct_time in utc in which the dst flag is always zero. if secs is not provided or none, the current time as returned by time() is used. fractions of a second are ignored.


time.localtime([secs]) 本地时间
like gmtime() but converts to local time. if secs is not provided or none, the current time as returned by time() is used. the dst flag is set to 1 when dst applies to the given time.


time.mktime(t)  与localtime()的功能恰好相反
this is the inverse function of localtime()


time.sleep(secs)
suspend execution for the given number of seconds


time.strftime(format[, t])
convert a tuple or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument. if t is not provided, the current time as returned by localtime() is used. format must be a string. valueerror is raised if any field in t is outside of the allowed range.

 


directive meaning
                                         notes
------------------------------------------------------------------------
%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.
 
%d          day of the month as a decimal number [01,31].
 
%h          hour (24-hour clock) as a decimal number [00,23].
 
%i          hour (12-hour clock) as a decimal number [01,12].
 
%j          day of the year as a decimal number [001,366].
 
%m          month as a decimal number [01,12].  
%m          minute as a decimal number [00,59].  
%p          locale’s equivalent of either am or pm.
(1)
%s          second as a decimal number [00,61].  (2)
%u          week 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)
%w          weekday as a decimal number [0(sunday),6].
 
%w          week 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)
%x          locale’s appropriate date representation.
 
%x          locale’s appropriate time representation.
 
%y          year without century as a decimal number [00,99].
 
%y          year with century as a decimal number.  
%z          time 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].
 
%z          time zone name (no characters if no time zone exists).
 
%%          a literal '%' character.
---------------------------------------------------------------------------


time.ctime()
返回固定格式的本地时间