当前位置:网站首页>Time standard library
Time standard library
2022-07-07 21:54:00 【Effort volume】
time There are three time representations in the module
- Time stamp
- Structured time objects
- Format time string
Time stamp
import time
Time stamp 1970.1.1 Interval to specified time The unit is seconds
time.time() Generate timestamp of current time
time.time()-3600 Timestamp an hour ago
Structured time objects
st = time.localtime()
print(type(st))
print(st)Output results :
<class 'time.struct_time'>
time.struct_time(tm_year=2022, tm_mon=6, tm_mday=17, tm_hour=16, tm_min=15, tm_sec=12, tm_wday=4, tm_yday=168, tm_isdst=0)
st Is essentially a tuple
print(' It's today {}-{:02d}-{}'.format(st[0],st[1],st[2]))
print(' It's today week {}'.format(st.tm_wday+1))Output results :
It's today 2022-06-17
It's today week 5The properties of an object are read-only Can't change
Format time string
print(time.ctime())Output results :
Fri Jun 17 16:20:20 2022strftime( Time format )'%Y-%m-%d %H:%M:%S'
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%Y year %m month %d Japan %H when %M branch %S second '))Output results :
2022-06-17 16:25:44
2022 year 06 month 17 Japan 16 when 25 branch 44 second If you make a mistake :
UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: encoding error
You need to add :
import locale
locale.setlocale(locale.LC_CTYPE,'chinese')sleep:
t1 = time.time()
print('sleep begin...')
time.sleep(1.23)
print('sleep end')
t2 = time.time()
print(" Yes {:.3f} second ".format(t2-t1))Conversion between three formats :
Time stamp Convert to Structured objects
# UTC Time
time.gmtime(time.time())
time.gmtime()# local
print(time.localtime())
print(time.localtime(time.time()))Structured objects Convert to Time stamp
# mktime(st)
print(time.time())
print(time.mktime(time.localtime()))Structured objects Convert to Format time string
# strftime(format,st)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
print(time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime()))Formatted string Convert to Structured time objects
# strptime(str, format)
strtime = '2020-07-25 13:23:18'
print(time.strptime(strtime,'%Y-%m-%d %H:%M:%S'))
边栏推荐
- 浅解ARC中的 __bridge、__bridge_retained和__bridge_transfer
- 【colmap】稀疏重建转为MVSNet格式输入
- Le capital - investissement est - il légal en Chine? C'est sûr?
- Ten thousand word summary data storage, three knowledge points
- MySQL storage expression error
- Jerry's manual matching method [chapter]
- Is it safe to open an account online now? I want to know where I can open an account in Nanning now?
- 三元表达式、各生成式、匿名函数
- Song list 11111
- OpenGL super classic learning notes (1) the first triangle "suggestions collection"
猜你喜欢

NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?

QT compile IOT management platform 39 alarm linkage

Build your own website (18)
![[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)](/img/1e/055df228853d9b464fc4bcbde0a7ee.png)
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)

Open source OA development platform: contract management user manual

Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission

Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future

Use camunda to do workflow design and reject operations

South China x99 platform chicken blood tutorial
![[C language] advanced pointer --- do you really understand pointer?](/img/ee/79c0646d4f1bfda9543345b9da0f25.png)
[C language] advanced pointer --- do you really understand pointer?
随机推荐
NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
Mahout-Pearson correlation的实现
Meta force force meta universe system development fossage model
2022 how to evaluate and select low code development platforms?
The difference between NPM uninstall and RM direct deletion
Ad domain group policy management
Matplotlib drawing interface settings
Awk processing JSON processing
How can big state-owned banks break the anti fraud dilemma?
EasyUI date control emptying value
Demon daddy A1 speech listening initial challenge
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
OpenGL super classic learning notes (1) the first triangle "suggestions collection"
Build your own website (18)
Magic weapon - sensitive file discovery tool
Implementation of mahout Pearson correlation
[UVALive 6663 Count the Regions] (dfs + 离散化)[通俗易懂]
The maximum number of meetings you can attend [greedy + priority queue]
Problems encountered in installing mysql8 for Ubuntu and the detailed installation process
gridView自己定义做时间排版「建议收藏」