Python编程:time时间模块
【摘要】 时间的三种形式:
时间戳(秒),元组形式,字符串形式
时间戳 timestamp
1970年1月1日计时,unix诞生于1970年,”UNIX元年” 格林威治时间1970年01月01日00时00分00秒起至现在的 总秒数
import time
x = time.time() # 以时间戳形式,返回当前时间
print(x) # 15153...
时间的三种形式:
时间戳(秒),元组形式,字符串形式
时间戳 timestamp
1970年1月1日计时,unix诞生于1970年,”UNIX元年”
格林威治时间1970年01月01日00时00分00秒起至现在的 总秒数
import time
x = time.time() # 以时间戳形式,返回当前时间
print(x) # 1515306968.886664
print(time.clock()) # 0.0
time1 = time.sleep(2) # 延迟2秒
print(time.clock()) #上次调用clock之后的间隔
# 2.00014532747535
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
元组形式 struct_time
UTC coordinated universal time 世界标准时间,
格林威治时间Greenwich Mean Time(GMT),中国UTC-8
DST daylight saving time 夏令时
import time
print(time.gmtime()) # 本初子午线时间,格林威治时间
"""
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=7,
tm_hour=9, tm_min=18, tm_sec=51,
tm_wday=6, tm_yday=7, tm_isdst=0)
"""
y = time.localtime() # 本地时间
print(y)
"""
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=7,
tm_hour=17, tm_min=18, tm_sec=51,
tm_wday=6, tm_yday=7, tm_isdst=0)
"""
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
字符串形式
import time
z = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 格式化时间
print(z) # 2018-01-07 16:07:47
- 1
- 2
- 3
- 4
转换
print(time.asctime(time.localtime())) # 将元组转为字符串
# Sun Jan 7 17:23:55 2018
print(time.ctime(time.time())) # 将时间戳转为字符串
# Sun Jan 7 17:24:24 2018
print(time.mktime(time.localtime())) # 将元组转为时间戳
# 1515317184.0
print(time.strptime(z, "%Y-%m-%d %H:%M:%S")) # 将字符串转为元组
"""
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=7,
tm_hour=17, tm_min=28, tm_sec=26,
tm_wday=6, tm_yday=7, tm_isdst=-1)
"""
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
"""
<type 'datetime.datetime'> 2018-04-20 11:11:52.007203
<type 'str'> 2018-04-20
"""
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
时间的加减
import datetime
print(datetime.datetime.now()) # 2018-01-07 17:53:23.579155
print(datetime.datetime.now()+datetime.timedelta(3)) # 默认为天
# 2018-01-10 17:53:23.579155
print(datetime.datetime.now()+datetime.timedelta(hours=3))
# 2018-01-07 20:53:23.579155
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
help(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)
Functions: time() -- return current time in seconds since the Epoch as a float clock() -- return CPU time since process start as a float sleep() -- delay for a number of seconds given as a float gmtime() -- convert seconds since Epoch to UTC tuple localtime() -- convert seconds since Epoch to local time tuple asctime() -- convert time tuple to string ctime() -- convert time in seconds to string mktime() -- convert local time tuple to seconds since Epoch strftime() -- convert time tuple to string according to format specification strptime() -- parse string to time tuple according to format specification tzset() -- change the local timezone 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.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/78996561
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)