当前位置:网站首页>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'))
边栏推荐
- Datatable data conversion to entity
- 648. Word replacement
- 私募基金在中國合法嗎?安全嗎?
- Reptile combat (VII): pictures of the king of reptiles' heroes
- Problems encountered in installing mysql8 for Ubuntu and the detailed installation process
- Ad domain group policy management
- Embedded development: how to choose the right RTOS for the project?
- 强化学习-学习笔记9 | Multi-Step-TD-Target
- 建立自己的网站(18)
- Tcp/ip protocol stack
猜你喜欢
Restapi version control strategy [eolink translation]
Ten thousand word summary data storage, three knowledge points
The function is really powerful!
Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future
Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
Debugging and handling the problem of jamming for about 30s during SSH login
Build your own website (18)
Jerry's manual matching method [chapter]
Ternary expressions, generative expressions, anonymous functions
Magic weapon - sensitive file discovery tool
随机推荐
I have to use my ID card to open an account. Is the bank card safe? I don't understand it
FatMouse&#39; Trade (Hangdian 1009)
[C language] advanced pointer --- do you really understand pointer?
Hoj 2245 planktonic triangle cell (Mathematics)
Demon daddy guide post - simple version
Jerry's about TWS channel configuration [chapter]
Dry goods sharing | devaxpress v22.1 original help document download collection
嵌入式开发:如何为项目选择合适的RTOS?
用语雀写文章了,功能真心强大!
MySQL storage expression error
Virtual machine network configuration in VMWare
[colmap] sparse reconstruction is converted to mvsnet format input
建立自己的网站(18)
Word inversion implements "suggestions collection"
Jerry's fast pairing does not support canceling pairing [article]
Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
GridView defines its own time for typesetting "suggestions collection"
gridView自己定义做时间排版「建议收藏」
Song list 11111
Backup tidb cluster to persistent volume