日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

Python學(xué)習(xí)之time模塊的基本使用

瀏覽:26日期:2022-06-29 16:39:00

前言

在我們學(xué)習(xí)的過程中,肯定會(huì)用到各種各樣的模塊。所以今天我們從time模塊開始學(xué)習(xí)

首先我們?cè)谑褂媚硞€(gè)模塊的時(shí)候,肯定要先導(dǎo)入這個(gè)模塊

import time

而當(dāng)我們想看看這個(gè)模塊是干什么的,我們可以使用help函數(shù)來看

print(help(time)) # 打印幫助信息

'E:Program Files (x86)python_3.8python.exe' D:/Application/pycharm_works/_1/test/python模塊之time模塊.pyHelp on built-in module time:NAME time - This module provides various functions to manipulate time values.DESCRIPTION There are two standard representations of time. One is the number of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer or a floating point number (to represent fractions of seconds). The Epoch is system-defined; on Unix, it is generally January 1st, 1970. The actual value can be retrieved by calling gmtime(0). The other representation is a tuple of 9 integers giving local time. The tuple items are: year (including century, e.g. 1998) month (1-12) day (1-31) hours (0-23) minutes (0-59) seconds (0-59) weekday (0-6, Monday is 0) Julian day (day in the year, 1-366) DST (Daylight Savings Time) flag (-1, 0 or 1) If the DST flag is 0, the time is given in the regular time zone; if it is 1, the time is given in the DST time zone; if it is -1, mktime() should guess based on the date and time.CLASSES builtins.tuple(builtins.object) struct_time class struct_time(builtins.tuple) | struct_time(iterable=(), /) | | The time value as returned by gmtime(), localtime(), and strptime(), and | accepted by asctime(), mktime() and strftime(). May be considered as a | sequence of 9 integers. | | Note that several fields’ values are not the same as those defined by | the C language standard for struct tm. For example, the value of the | field tm_year is the actual year, not year - 1900. See individual | fields’ descriptions for details. | | Method resolution order: | struct_time | builtins.tuple | builtins.object | | Methods defined here: | | __reduce__(...) | Helper for pickle. | | __repr__(self, /) | Return repr(self). | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | tm_gmtoff | offset from UTC in seconds | | tm_hour | hours, range [0, 23] | | tm_isdst | 1 if summer time is in effect, 0 if not, and -1 if unknown | | tm_mday | day of month, range [1, 31] | | tm_min | minutes, range [0, 59] | | tm_mon | month of year, range [1, 12] | | tm_sec | seconds, range [0, 61]) | | tm_wday | day of week, range [0, 6], Monday is 0 | | tm_yday | day of year, range [1, 366] | | tm_year | year, for example, 1993 | | tm_zone | abbreviation of timezone name | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | n_fields = 11 | | n_sequence_fields = 9 | | n_unnamed_fields = 0 | | ---------------------------------------------------------------------- | Methods inherited from builtins.tuple: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __getitem__(self, key, /) | Return self[key]. | | __getnewargs__(self, /) | | __gt__(self, value, /) | Return self>value. | | __hash__(self, /) | Return hash(self). | | __iter__(self, /) | Implement iter(self). | | __le__(self, value, /) | Return self<=value. | | __len__(self, /) | Return len(self). | | __lt__(self, value, /) | Return self<value. | | __mul__(self, value, /) | Return self*value. | | __ne__(self, value, /) | Return self!=value. | | __rmul__(self, value, /) | Return value*self. | | count(self, value, /) | Return number of occurrences of value. | | index(self, value, start=0, stop=9223372036854775807, /) | Return first index of value. | | Raises ValueError if the value is not present.FUNCTIONS asctime(...) asctime([tuple]) -> string Convert a time tuple to a string, e.g. ’Sat Jun 06 16:26:11 1998’. When the time tuple is not present, current time as returned by localtime() is used. ctime(...) ctime(seconds) -> string Convert a time in seconds since the Epoch to a string in local time. This is equivalent to asctime(localtime(seconds)). When the time tuple is not present, current time as returned by localtime() is used. get_clock_info(...) get_clock_info(name: str) -> dict Get information of the specified clock. gmtime(...) gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst) Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT). When ’seconds’ is not passed in, convert the current time instead. If the platform supports the tm_gmtoff and tm_zone, they are available as attributes only. localtime(...) localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min, tm_sec,tm_wday,tm_yday,tm_isdst) Convert seconds since the Epoch to a time tuple expressing local time. When ’seconds’ is not passed in, convert the current time instead. mktime(...) mktime(tuple) -> floating point number Convert a time tuple in local time to seconds since the Epoch. Note that mktime(gmtime(0)) will not generally return zero for most time zones; instead the returned value will either be equal to that of the timezone or altzone attributes on the time module. monotonic(...) monotonic() -> float Monotonic clock, cannot go backward. monotonic_ns(...) monotonic_ns() -> int Monotonic clock, cannot go backward, as nanoseconds. perf_counter(...) perf_counter() -> float Performance counter for benchmarking. perf_counter_ns(...) perf_counter_ns() -> int Performance counter for benchmarking as nanoseconds. process_time(...) process_time() -> float Process time for profiling: sum of the kernel and user-space CPU time. process_time_ns(...) process_time() -> int Process time for profiling as nanoseconds: sum of the kernel and user-space CPU time. sleep(...) sleep(seconds) Delay execution for a given number of seconds. The argument may be a floating point number for subsecond precision. strftime(...) strftime(format[, tuple]) -> string Convert a time tuple to a string according to a format specification. See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used. Commonly used format codes: %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. Other codes may be available on your platform. See documentation for the C library strftime function. strptime(...) strptime(string, format) -> struct_time Parse a string to a time tuple according to a format specification. See the library reference manual for formatting codes (same as strftime()). Commonly used format codes: %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. Other codes may be available on your platform. See documentation for the C library strftime function. thread_time(...) thread_time() -> float Thread time for profiling: sum of the kernel and user-space CPU time. thread_time_ns(...) thread_time() -> int Thread time for profiling as nanoseconds: sum of the kernel and user-space CPU time. time(...) time() -> floating point number Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them. time_ns(...) time_ns() -> int Return the current time in nanoseconds since the Epoch.DATA altzone = -32400 daylight = 0 timezone = -28800 tzname = (’中國標(biāo)準(zhǔn)時(shí)間’, ’中國夏令時(shí)’)FILE (built-in)NoneProcess finished with exit code 0

那么接下來我們挨個(gè)來看看

1. time.time()為當(dāng)前時(shí)間戳,從1900年開始到當(dāng)前時(shí)間的秒數(shù)

print(help(time.time)) # 打印幫助信息print(time.time()) #1610720236.653394 # 打印當(dāng)前時(shí)間戳

Help on built-in function time in module time:time(...) time() -> floating point number Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them.None1610727247.1696546

2. time.sleep(secs) 讓程序暫停secs秒

1 print(help(time.sleep)) # 打印幫助信息2 time.sleep(3) # 暫停3秒

Help on built-in function sleep in module time:sleep(...) sleep(seconds) Delay execution for a given number of seconds. The argument may be a floating point number for subsecond precision.None

3.time.gmtime() 結(jié)構(gòu)化時(shí)間,不過要注意的一點(diǎn)是這個(gè)時(shí)間是世界標(biāo)準(zhǔn)時(shí)間(格林尼治時(shí)間)

1 print(help(time.gmtime)) # 打印幫助信息2 print(time.gmtime()) # 結(jié)構(gòu)化時(shí)間 time.struct_time(tm_year=2021, tm_mon=1, tm_mday=15, tm_hour=14, tm_min=22, tm_sec=30, tm_wday=4, tm_yday=15, tm_isdst=0)

Help on built-in function gmtime in module time:gmtime(...) gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst) Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT). When ’seconds’ is not passed in, convert the current time instead. If the platform supports the tm_gmtoff and tm_zone, they are available as attributes only.Nonetime.struct_time(tm_year=2021, tm_mon=1, tm_mday=15, tm_hour=16, tm_min=16, tm_sec=39, tm_wday=4, tm_yday=15, tm_isdst=0)

不過這時(shí)肯定有人該問了,那我們的當(dāng)?shù)貢r(shí)間怎么表示呢,所以我們來介紹下一個(gè)

4.time.localtime()結(jié)構(gòu)化時(shí)間,當(dāng)前時(shí)間

1 print(help(time.localtime)) # 打印幫助信息2 print(time.localtime()) # 當(dāng)前結(jié)構(gòu)化時(shí)間

Help on built-in function localtime in module time:localtime(...) localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst) Convert seconds since the Epoch to a time tuple expressing local time. When ’seconds’ is not passed in, convert the current time instead.Nonetime.struct_time(tm_year=2021, tm_mon=1, tm_mday=16, tm_hour=0, tm_min=17, tm_sec=49, tm_wday=5, tm_yday=16, tm_isdst=0)

總說結(jié)構(gòu)化時(shí)間,那結(jié)構(gòu)化時(shí)間是什么呢,我們來看看里面的參數(shù)

我們來拿上面這個(gè)例子來解釋:

tm_year=2021 當(dāng)前所在年tm_mon=1 當(dāng)前所在月tm_mday=15 當(dāng)前所在天tm_hour=23 當(dāng)前所在時(shí)tm_min=18當(dāng)前所在分tm_sec=57當(dāng)前所在秒tm_wday=4當(dāng)前周的第幾天tm_yday=15 當(dāng)前年的第幾天

但是有時(shí)候我們需要的并不是結(jié)構(gòu)化時(shí)間,而是類似于 2021-01-15 23:28:26 這樣的格式化時(shí)間,那我們應(yīng)該怎么做呢?

6. time.strftime() 將結(jié)構(gòu)話時(shí)間化為格式化時(shí)間

1 print(help(time.strftime)) # 打印幫助信息2 struct_time=time.localtime()3 print(time.strftime('%Y-%m-%d %H:%M:%S',struct_time)) # 格式化時(shí)間

Help on built-in function strftime in module time:strftime(...) strftime(format[, tuple]) -> string Convert a time tuple to a string according to a format specification. See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used. Commonly used format codes: %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. Other codes may be available on your platform. See documentation for the C library strftime function.None2021-01-16 00:18:38

同樣這里為什么要寫成 '%Y-%m-%d %H:%M:%S' 呢,就是為了控制時(shí)間的格式。

那這些都表示什么呢,我們來看看

%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.

不過似乎也可以單獨(dú)使用 time.strftime(),我們來看看結(jié)果,但是我們必須要把格式加上,如下所示:

print(time.strftime('%Y-%m-%d %H:%M:%S')) # 格式化時(shí)間 # 2021-01-15 23:36:49

那么,有時(shí)候我們也需要把格式化時(shí)間轉(zhuǎn)化為結(jié)構(gòu)化時(shí)間來使用,這時(shí)我們僅僅需要看看接下來的知識(shí)就能掌握

7. time.strptime() 將格式化時(shí)間(字符串)轉(zhuǎn)化為結(jié)構(gòu)化時(shí)間

print(help(time.strftime))print(time.strftime('%Y-%m-%d %H:%M:%S')) # 格式化時(shí)間 # 2021-01-15 23:36:49

Help on built-in function strftime in module time:strftime(...) strftime(format[, tuple]) -> string Convert a time tuple to a string according to a format specification. See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used. Commonly used format codes: %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. Other codes may be available on your platform. See documentation for the C library strftime function.None2021-01-16 00:20:46

當(dāng)然以上只是一個(gè)舉例,具體我們可以采用如下方式:

a=time.strptime('2021-01-15 22:26:28','%Y-%m-%d %H:%M:%S')print(a.tm_yday) # 15print(a.tm_wday) # 4

最后,我們快接近了尾聲,最后我們?cè)俳榻B兩個(gè)就結(jié)束了

8. time.ctime() 將所給時(shí)間戳轉(zhuǎn)變?yōu)橐粋€(gè)格式化時(shí)間

1 print(help(time.ctime)) # 將時(shí)間戳轉(zhuǎn)變?yōu)橐粋€(gè)格式化時(shí)間2 print(time.ctime()) # 如果不帶參數(shù)則默認(rèn)為當(dāng)前時(shí)間戳3 print(time.ctime(12412415))

Help on built-in function ctime in module time:ctime(...) ctime(seconds) -> string Convert a time in seconds since the Epoch to a string in local time. This is equivalent to asctime(localtime(seconds)). When the time tuple is not present, current time as returned by localtime() is used.NoneSat Jan 16 00:21:56 2021Sun May 24 23:53:35 1970

9.time.mktime() 將所給結(jié)構(gòu)化時(shí)間轉(zhuǎn)化為時(shí)間戳

1 print(help(time.ctime)) # 打印幫助信息2 print(time.mktime(time.localtime())) # 將結(jié)構(gòu)化時(shí)間轉(zhuǎn)化為時(shí)間戳

Help on built-in function ctime in module time:ctime(...) ctime(seconds) -> string Convert a time in seconds since the Epoch to a string in local time. This is equivalent to asctime(localtime(seconds)). When the time tuple is not present, current time as returned by localtime() is used.None1610727764.0

不過值得一提的是,這種方式得到的時(shí)間戳精度要比time.time()低的多

最后,在提供一種其他求當(dāng)前時(shí)間的方法

import datetimeprint(datetime.datetime.now()) # 2021-01-15 23:55:48.985808

本次time模塊便到此結(jié)束,其他模塊下次講解

總結(jié)

到此這篇關(guān)于Python學(xué)習(xí)之time模塊的基本使用的文章就介紹到這了,更多相關(guān)Python time模塊內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产二区精品| 蜜桃久久久久久| 国产精品亚洲欧美日韩一区在线| 久久九九电影| 亚洲午夜av| 久久三级视频| 婷婷成人在线| 在线看片不卡| 丝袜脚交一区二区| 日韩精品三区四区| 国产精品一区二区精品| 狂野欧美性猛交xxxx| 国产一区2区| 精精国产xxxx视频在线播放 | 久久久久国产精品一区三寸| se01亚洲视频| 黄色成人91| 亚洲精品三级| 免费在线观看一区| 中文在线а√在线8| 久久一区二区中文字幕| 伊人久久亚洲热| 亚洲精品亚洲人成在线观看| 欧美日韩亚洲一区在线观看| 免费在线亚洲| 99久久精品费精品国产| 亚洲男女自偷自拍| 国产乱码精品一区二区三区亚洲人 | 久久亚洲专区| 蜜桃视频在线观看一区| 国产亚洲字幕| 日韩欧美不卡| 免费久久99精品国产自在现线| 日本一区中文字幕| 精品成av人一区二区三区| av一区在线| 石原莉奈在线亚洲二区| 91福利精品在线观看| 开心激情综合| 免费视频国产一区| 亚洲1区在线| 精品视频97| 91九色精品国产一区二区| 亚洲精品无播放器在线播放| 免费一级欧美在线观看视频| 999视频精品| 亚洲专区视频| 国产一区二区三区亚洲综合| 精品中文一区| 国产精品videosex极品| 免费av一区二区三区四区| 日韩综合小视频| 久久久久久自在自线| 鲁大师成人一区二区三区| 国产美女视频一区二区| 电影亚洲精品噜噜在线观看| 日韩中文字幕无砖| 精品成人免费一区二区在线播放| 亚洲精品乱码日韩| 日韩欧美三级| 日韩国产欧美视频| 久久婷婷久久| 国产精品多人| 久久国产66| а√天堂8资源在线| 中文字幕av一区二区三区人| 中文在线免费视频| 日本亚洲最大的色成网站www| 色一区二区三区| 欧美一级网址| 国产一在线精品一区在线观看| 日韩精选在线| 精品一区在线| 鲁大师精品99久久久| 噜噜噜躁狠狠躁狠狠精品视频 | 日韩中文字幕高清在线观看| 日韩欧美久久| 亚洲激情中文| 欧美激情国产在线| 国产视频一区二区在线播放| 欧美成a人免费观看久久| 国产精品美女午夜爽爽| 免费成人av在线播放| 久久精品一区二区不卡| 国产精品免费99久久久| 亚洲精品精选| 香蕉久久国产| 99久久亚洲精品蜜臀| 激情不卡一区二区三区视频在线| 少妇精品久久久| 久久国产99| 欧美精品一二| 欧洲在线一区| 国产福利片在线观看| 国产欧美综合一区二区三区| 视频一区免费在线观看| 激情综合网站| 91精品精品| 日韩欧美不卡| 欧美a级一区二区| 国产精品一区二区av交换| 亚洲一区二区三区无吗| 亚洲精品一区二区妖精| 亚洲播播91| av资源亚洲| 成人午夜网址| 美女国产精品久久久| 国产欧美另类| 欧美在线日韩| 青青国产91久久久久久| 亚洲日产av中文字幕| 久久午夜精品| 国产手机视频一区二区 | 久久国产电影| 欧美日韩免费看片| 91欧美日韩| 精品国产精品国产偷麻豆| 国产伦精品一区二区三区在线播放 | 欧美一级一区| 日韩不卡一区二区| 亚洲精品影视| 日韩精品三区四区| 日本免费一区二区视频| 亚洲深深色噜噜狠狠爱网站| 日韩午夜av| 亚洲欧洲另类| 亚洲欧美不卡| 亚洲人妖在线| 日本成人在线视频网站| 亚洲精品福利| 日本三级亚洲精品| 欧美中文高清| 国产精品多人| 韩国女主播一区二区三区| 91亚洲一区| 日韩精品一区二区三区免费观影| 国产一区二区三区四区五区| 国产精品久久久久蜜臀| 日韩影院二区| 欧美中文一区二区| 99国产精品99久久久久久粉嫩| 亚洲在线网站| 日韩免费精品| 免费一级欧美片在线观看网站| 国产一区二区三区四区| 色天使综合视频| 国产亚洲毛片| 日韩综合一区二区| 久久精品网址| 欧美丝袜一区| 免费不卡在线视频| 欧美一级二级三级视频| 欧美精品aa| 日韩三区在线| 亚洲专区欧美专区| 国产日韩中文在线中文字幕 | 激情五月色综合国产精品| 免费精品视频在线| 久久国内精品| 日韩中文首页| 综合一区二区三区| 麻豆精品99| 亚洲天堂成人| 日韩激情一二三区| av中文字幕在线观看第一页| 日韩午夜精品| 日本午夜精品久久久久| 久久精品国产99国产| 欧美丝袜一区| 奇米狠狠一区二区三区| www在线观看黄色| 日韩一级网站| 国产探花在线精品| 1024精品一区二区三区| 亚洲3区在线| 日韩欧美1区| 日韩欧美中文字幕一区二区三区| 精品国产亚洲一区二区三区| 合欧美一区二区三区| 国产欧美激情| 欧美亚洲在线日韩| 日韩超碰人人爽人人做人人添| а√天堂中文在线资源8| 免费在线观看一区二区三区| 久久中文精品| 亚洲在线观看| 成人精品国产亚洲| 亚洲毛片视频| 久久九九99| 久久不卡国产精品一区二区| 欧美日韩国产一区精品一区| 国产精品三p一区二区| 亚洲夜间福利| 美女国产精品久久久| 日韩中文字幕一区二区三区| 国产成人黄色| 久久亚洲风情| 特黄毛片在线观看| 国产一精品一av一免费爽爽| jiujiure精品视频播放|