time&datetime

关于time模块的代码部分

 1 #_*_coding:utf-8_*_
 2 __author__ = 'Alex Li'
 3 
 4 import time
 5 
 6 
 7 # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
 8 # print(time.altzone)  #返回与utc时间的时间差,以秒计算\
 9 # print(time.asctime()) #返回时间格式"Fri Aug 19 11:14:16 2016",
10 # print(time.localtime()) #返回本地时间 的struct time对象格式
11 # print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
12 
13 # print(time.asctime(time.localtime())) #返回时间格式"Fri Aug 19 11:14:16 2016",
14 #print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上
15 
16 
17 
18 # 日期字符串 转成  时间戳
19 # string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
20 # print(string_2_struct)
21 # #
22 # struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳
23 # print(struct_2_stamp)
24 
25 
26 
27 #将时间戳转为字符串格式
28 # print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式
29 # print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式
30 
31 
32 
33 
34 
35 #时间加减
36 import datetime
37 
38 # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
39 #print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
40 # print(datetime.datetime.now() )
41 # print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
42 # print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
43 # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
44 # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
45 
46 
47 #
48 # c_time  = datetime.datetime.now()
49 # print(c_time.replace(minute=3,hour=2)) #时间替换
View Code

 

time.localtime()
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=23, tm_hour=20, tm_min=39, tm_sec=34, tm_wday=1, tm_yday=23, tm_isdst=0)

 

time.time()   获取时间戳

 

time.timezone
-28800
-28800/3600
-8.0    中国是东八区

 

time.gmtime()    UTC时区
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=23, tm_hour=12, tm_min=48, tm_sec=3, tm_wday=1, tm_yday=23, tm_isdst=0)
time.localtime()  UTC+8时区
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=23, tm_hour=20, tm_min=49, tm_sec=2, tm_wday=1, tm_yday=23, tm_isdst=0)

 

time.mktime()

time.strftime('%Y-%m-%d %H:%M:%S,x)

 

time.asctime()
'Tue Jan 23 21:23:35 2018'

import time
x=time.localtime()
print(x.tm_year)  #根据时间戳进行时间推算
View Code

 

 

 

 

datetime

 

#获取当前时间

datetime.datetime.now()
datetime.datetime(2018, 1, 23, 21, 27, 21, 66406)

 

#往后加三天

datetime.datetime.now()+datetime.timedelta(3)
datetime.datetime(2018, 1, 26, 21, 28, 35, 72639)

#往后加三个小时

posted on 2018-01-23 09:31  90500042陈  阅读(141)  评论(0编辑  收藏  举报