1, UTC (Coordinated Universal Time): Greenwich astronomical time, the world standard time, in China is 8 + UTC
2, DST (Daylight Saving Time): is an artifact of energy-saving and time system, a fast transfer in the summer hour
Time representation:
1, timestamp
In integer or floating point represents a time in seconds of the time interval. The base value of the time interval is started with the start at 0:01 on January 1970 Ri
2, tuples
Python is satisfied data structure, said tuple nine integer content
Attributes | meaning | |
---|---|---|
tm_year | year | |
tm_mon | month | 1-12 |
tm_mday | day | 1-31 |
tm_hour | hour | 0-23 |
tm_min | minute | 0-59 |
tm_sec | second | 0-61 (60 or 61 leap seconds) |
tm_wday | The first few days of the week | 0-6 (0 is Monday) |
tm_yday | The first few days of the year | 1-366 |
tm_isdst | summer time | 0,1,-1 |
NOTE: tm_isdst 0 represents the normal time, represents a normal time, -1 indicates whether daylight saving time is uncertain.
More general use 0, -1 is when we artificially prescribed time is -1
3, the format string
symbol | meaning |
---|---|
%a | Local (local) to simplify the days of the week |
%A | Local full weekday name |
%b | Local simplify the name of the month |
%B | Local full month name |
%c | The corresponding local date and time representation |
%d | The first few days of the month (01--31) |
%H | The first few hours (24-hour format, 00--23) of the day |
%I | The first few hours (12-hour clock, 01--12) of day |
%j | The first day of the year (001--366) |
%m | Month (01 to 12) |
%M | The number of minutes (00--59) |
%p | Local AM or PM equivalent character |
%S | Sec (01-61) |
% U | Weeks of the year. (00-53) Sunday is the beginning of a week, all days prior to the first Sunday in Week 0 |
%w | The first few days of the week (0 is a 6,0 Sunday) |
%W | % U and basically the same, except that% W with a week beginning Monday. |
%x | Local corresponding date |
%X | Local response time |
%Y | Removing the century years (00--99) |
%Y | Complete years |
%FROM | Time zone name (if there is no null character) |
%% | '%'character |
Note: Be sure to strictly case-sensitive
函数详解
1、time()函数
time()函数用于返回当前时间的时间戳(从1970年1月1号零点零分零秒到现在的时间戳),浮点数形式,不需要参数
time()函数的语法:
time.time()
import time
print("当前时间的时间的时间戳:%f" % time.time())
运行结果:
当前时间的时间的时间戳:1580118533.995739
2、gmtime()函数
gmtime()函数函数用于将时间戳作为UTC时间元组
gmtime()函数的语法:
time.gmtime(x)
参数x - - 本地时间戳time.time(),也可以不加参数,不加参数默认为本地时间
type类型为time.struct_time
import time
print(time.gmtime())
运行结果:
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=27, tm_hour=10, tm_min=2, tm_sec=29, tm_wday=0, tm_yday=27, tm_isdst=0)
3、localtime()函数
localtime()函数函数用于将时间戳作为本地时间元组
localtime()函数的语法:
time.localtime(x)
参数x - - 本地时间戳time.time(),也可以不加参数,不加参数默认为本地时间
type类型为time.struct_time
import time
print(time.localtime())
运行结果:
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=27, tm_hour=18, tm_min=9, tm_sec=58, tm_wday=0, tm_yday=27, tm_isdst=0)
4、mktime()函数
mktime()函数函数用于将本地时间元组转成时间截
mktime()函数的语法:
time.mktime(x)
参数x - - 本地时间元组time.localtime()或者time.gmtime,或者全满 9 个元素的元组
type类型为float
import time
x = (2019, 10, 2, 10, 40, 20, 3, 155, -1)
print(time.mktime(x))
print(time.mktime(time.localtime()))
运行结果:
1569984020.0
1580120419.0
5、asctime()函数
asctime()函数函数用于将时间元组转为字符串
asctime()函数的语法:
time.asctime(x)
参数x - - 本地时间元组time.localtime()或者time.gmtime,或者全满 9 个元素的元组
type类型为str
import time
x = (2019, 10, 2, 10, 40, 20, 3, 155, -1)
print(time.asctime(x))
print(time.asctime(time.localtime()))
运行结果:
Thu Oct 2 10:40:20 2019
Mon Jan 27 18:23:10 2020
6、ctime()函数
ctime()函数函数用于将时间戳转为字符串
ctime()函数的语法:
time.ctime(x)
参数x - - 本地时间戳time.time(),或者自己拟定的时间戳,若未给出参数,则默认为time.time()
type类型为str
import time
x = (1569984020.0)
print(time.ctime(x))
print(time.ctime(time.time()))
运行结果:
Wed Oct 2 10:40:20 2019
Mon Jan 27 18:27:39 2020
7、strftime()函数
strftime()函数用于接收时间元组,并返回以可读字符串表示的当地时间。格式由参数决定
strftime()函数的语法:
time.strftime(" %Y-%m-%d %H:%M:%S ",x)
参数1 - - 格式化给定时间的指令,这个可以自己拟定,只要是用strptime合法的格式化符号
参数2 - - 被格式化以秒为单位的时间,时间元组,为一个可选参数。如果没有,默认为当前时间
type类型为str
import time
x = "%Y-%m-%d %H:%M:%S"
print(time.strftime(x))
print(time.strftime(x, time.localtime()))
运行结果:
2020-01-27 18:39:40
2020-01-27 18:39:40
8、strptime()函数
strptime()函数用于根据给定的格式把一个时间字符串解析为时间元组
strptime()函数的语法:
time.strptime(x," %Y-%m-%d %H:%M:%S ")
参数1 - - 时间字符串
参数2 - - 格式化字符串的格式
type类型为time.struct_time
import time
print(time.strptime("2020-01-27", "%Y-%m-%d"))
运行结果:
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=27, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=27, tm_isdst=-1)
9、sleep()函数
sleep()函数用于延迟一个时间,整型或者浮点型
sleep()函数的语法:
time.sleep(x)
参数x - - 暂停执行的秒数
import time
time.sleep(2)
运行结果:
就是暂停了2秒,然后程序执行完毕
9、clock()函数
clock()函数用于返回当前程序的cpu执行时间
unix系统始终返回全部的运行时间
windows从第二次开始,都是以第一个调用此函数的开始间隔作为基数
clock()函数的语法:
time.clock()
print(“测试的语句”)
time.clock()
import time
time.clock()
time.sleep(2)
y1 = time.clock()
print(y1)
time.sleep(2)
y2 = time.clock()
print(y2)
运行结果:
2.0004032
4.0008972
始终是以第一个time.clock()作为对齐标准
time.clock已在Python 3.3中弃用,将从Python 3.8中删除:使用time.perf_counter或time.process_time代替