当前位置:网站首页>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 5
The properties of an object are read-only Can't change
Format time string
print(time.ctime())
Output results :
Fri Jun 17 16:20:20 2022
strftime( 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'))
边栏推荐
- Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
- 2022 how to evaluate and select low code development platforms?
- FatMouse&#39; Trade (Hangdian 1009)
- Lex & yacc of Pisa proxy SQL parsing
- Debugging and handling the problem of jamming for about 30s during SSH login
- Leetcode SQL first day
- Tupu digital twin coal mining system to create "hard power" of coal mining
- Ternary expressions, generative expressions, anonymous functions
- Google SEO external chain backlinks research tool recommendation
- 三元表达式、各生成式、匿名函数
猜你喜欢
嵌入式开发:如何为项目选择合适的RTOS?
Build your own website (18)
Tcp/ip protocol stack
双塔模型的最强出装,谷歌又开始玩起“老古董”了?
NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
MySQL storage expression error
Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
Index summary (assault version)
建立自己的网站(18)
The function is really powerful!
随机推荐
Programming mode - table driven programming
Tupu digital twin coal mining system to create "hard power" of coal mining
Is private equity legal in China? Is it safe?
Index summary (assault version)
Use br to back up tidb cluster data to azure blob storage
NVR硬盤錄像機通過國標GB28181協議接入EasyCVR,設備通道信息不顯示是什麼原因?
Reptile combat (VII): pictures of the king of reptiles' heroes
Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
Deployment, recall and deletion solutions - stsadm and PowerShell "suggestions collection"
A brief understanding of the in arc__ bridge、__ bridge_ Retained and__ bridge_ transfer
Preparing for the interview and sharing experience
Matplotlib drawing interface settings
Word inversion implements "suggestions collection"
How does win11 time display the day of the week? How does win11 display the day of the week today?
MIT6.S081-Lab9 FS [2021Fall]
嵌入式开发:如何为项目选择合适的RTOS?
Insufficient permissions
[JDBC Part 1] overview, get connection, CRUD
[colmap] sparse reconstruction is converted to mvsnet format input
Jerry's about TWS channel configuration [chapter]