Python 时间常用函数及结构
生活随笔
收集整理的這篇文章主要介紹了
Python 时间常用函数及结构
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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時(shí)區(qū)的時(shí)間
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]) 本地時(shí)間
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()
返回固定格式的本地時(shí)間
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時(shí)區(qū)的時(shí)間
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]) 本地時(shí)間
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()
返回固定格式的本地時(shí)間
總結(jié)
以上是生活随笔為你收集整理的Python 时间常用函数及结构的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: redis☞ python客户端
- 下一篇: 正则查看接口消耗时间超过4位数的