Python 时间常用函数及结构


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()
返回固定格式的本地时间

相关内容

    暂无相关文章

评论关闭