当前位置:网站首页>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'))
边栏推荐
- Preparing for the interview and sharing experience
- 强化学习-学习笔记9 | Multi-Step-TD-Target
- [200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
- How to make agile digital transformation strategy for manufacturing enterprises
- FatMouse&#39; Trade (Hangdian 1009)
- Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]
- Demon daddy B3 read extensively in a small amount, and completed 20000 vocabulary+
- NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
- [UVALive 6663 Count the Regions] (dfs + 离散化)[通俗易懂]
- GridView defines its own time for typesetting "suggestions collection"
猜你喜欢
Embedded development: how to choose the right RTOS for the project?
The maximum number of meetings you can attend [greedy + priority queue]
Virtual machine network configuration in VMWare
Ternary expressions, generative expressions, anonymous functions
[开源] .Net ORM 访问 Firebird 数据库
Win11如何解禁键盘?Win11解禁键盘的方法
Preparing for the interview and sharing experience
Solve the problem of uni in uni app Request sent a post request without response.
Goal: do not exclude yaml syntax. Try to get started quickly
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
随机推荐
Le capital - investissement est - il légal en Chine? C'est sûr?
Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
Contour layout of margin
建立自己的网站(18)
Embedded development: how to choose the right RTOS for the project?
Preparing for the interview and sharing experience
L2: current situation, prospects and pain points of ZK Rollup
The cyberspace office announced the measures for data exit security assessment, which will come into force on September 1
Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]
Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
FatMouse&#39; Trade (Hangdian 1009)
Can I open a stock account directly online now? Is it safe?
Jerry's test box configuration channel [chapter]
Which financial products will yield high returns in 2022?
POJ 3140 Contestants Division「建议收藏」
648. Word replacement
Demon daddy A3 stage near normal speed speech flow initial contact
How to turn on win11 game mode? How to turn on game mode in win11
POJ 3140 contents division "suggestions collection"