python time module and datetime

A Time

Coordinated Universal Time (English: Coordinated Universal Time, France: Temps Universel Coordonné), also known as the unified world time, Universal Time,
Coordinated Universal Time. Different acronym in English (CUT) and French (TUC), and as a compromise, referred to UTC.
Is UTC + 8 in China,

DST

DST is Daylight Saving Time acronym, called upon to save the sun, in our country called daylight saving time,
the system also known as daylight saving time, saving energy is an artificially adjusted to local time.
Some countries use DST long time, (such as the US up to 7 months) spanning three seasons such as spring and autumn

时间表现的形势:
 时间戳
            以整型 或浮点型表示时间的一个以秒为单位的时间间隔
            这个时间 时间间隔的基础值是197011号零点开始算起
 元组
           一种python 的数据结构表示 这个元组有9 个整型内容
             year
             month
             day
             hours
             minutes
             seconds
             weekday
             julia day
             flag(1-10)

1. time () function

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
# 返回当前时间的时间戳 浮点数 形式    不需要参数
import time;  # 引入time模块

c=time.time()
print(c)  #1528132954.339143

# 将时间戳 转为utc时间元组
t=time.gmtime(c)
print(t)            
#time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=17, tm_min=23, tm_sec=27, tm_wday=0, tm_yday=155, tm_isdst=0)

# 将时间戳转为本地时间元组
b=time.localtime(c) #time.struct_time(tm_year=2018, tm_mon=6, tm_mday=5, tm_hour=1, tm_min=26, tm_sec=31, tm_wday=1, tm_yday=156, tm_isdst=0)
print(b)

# 将第时间元组 转成时间戳
m=time.mktime(b)

print(m)   #1528133464.0

# 将时间元组转换成字符串
s=time.asctime(b)
print(s) #Tue Jun  5 01:33:34 2018

# 将时间戳转换为字符
p=time.ctime(c)
print(p)     #  Tue Jun  5 01:35:49 2018

# 将时间元组转换成给定格式的字符串   如果没有参数2 默认转换为当前时间 
q=time.strftime("%Y-%m-%d %H:%M:%S")
print(q) #2018-06-05 01:40:38

# 将时间元组转换成给定格式的字符串 参数2为时间元组 如果没有参数2 默认转换为当前时间  
h=time.strftime("%Y-%m-%d %H:%M:%S",b) 
print(h) #2018-06-05 01:42:48
l=time.strftime("%Y-%m-%d %X",b) 
print(l) #2018-06-05 01:46:40
print(type(l))  #<class 'str'>

# 将时间字符串转换为时间元组
w=time.strptime(q,"%Y-%m-%d %X")
print(w) 
#time.struct_time(tm_year=2018, tm_mon=6, tm_mday=5, tm_hour=1, tm_min=49, tm_sec=59, tm_wday=1, tm_yday=156, tm_isdst=-1)

# 延迟一个时间  整型或者浮点型
# time.sleep(4)

# 返回当前程序的cpu 执行时间
# Unix 系统同时返回全部时间
# windows从第二次开始 都是以第一个调用函数的开始时间戳作为基数
c1=time.clock()
print(c1)          #1.7104874547148446e-06

2.datetime () function

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
# datetime  比time高级了不少  可以理解为datetime基于time进行封装 
# 提供了各位使用的函数 date模块的接口更直观  更容易
# 模块中的类:
#       datetime   同时又时间日期              
#       timedelta  主要计算时间的跨度
#       tzinfo     时区相关
#       time     只关注时间
#       date  只关注日期

import datetime  # 引入time模块

# 获取当前时间
d1=datetime.datetime.now()
print(d1)            #2018-06-05 18:20:22.763177
print(type(d1))     # <class 'datetime.datetime'>

# 获取指定的时间
d2=datetime.datetime(1999,10,1,10,28,25,123456)
print(d2)  #1999-10-01 10:28:25.123456

# 将时间转为字符串
d3=d1.strftime("%Y-%m-%d %X")
print(d3)  #18-06-05 18:26:02         2018-06-05 18:26:30

# 将格式字符串转为datetime对象
# 注意的格式要与字符串一致
d4=datetime.datetime.strptime(d3,"%Y-%m-%d %X")
print(d4) #2018-06-05 18:28:44

# dtaetime 时间类型还可以加减
d5=datetime.datetime(1999,10,1,10,28,25,123456)
d6=datetime.datetime.now()
d7=d6-d5

print(d7)        #6822 days, 8:03:35.068612
print(type(d7))   # <class 'datetime.timedelta'> 
# 能获取到间隔天数
print(d7.days)  
# 获取间隔天数以为的秒数
print(d7.seconds) #29133
import time

time.clock()
sum=0

for x in range(10000000):
    sum+=x

print(time.clock())  #3.0864349222241354
Published 705 original articles · won praise 857 · Views 1.49 million +

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/105389762