time and datatime

 

import   time

# print(sys.path)
#
# # os.path.abspath(__file__) 表示的是获取当前文件的绝对路径
#
# #os.path.dirname 表示获取当前目录文件的上一层的文件路径
#
# print(os.path.dirname(os.path.abspath(__file__)))
# print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
print(time.asctime()) ##返回的时间格式Sat Jan 13 15:35:08 2018
print(time.localtime())## 返回本地时间的 struct time对象格式 time.struct_time(tm_year=2018, tm_mon=1, tm_mday=13, tm_hour=15, tm_min=36, tm_sec=38, tm_wday=5, tm_yday=13, tm_isdst=0)
print(time.time()) # time.time() 是时间戳 1515829241.7104118
print(time.gmtime(time.time()-8000000))# 返回utc时间的struct时间格式的对象

print(time.asctime(time.localtime())) # 返回时间格式 “Sat Jan 13 15:43:15 2018”
print(time.ctime()) #返回时间格式 “Sat Jan 13 15:43:15 2018”


# 日期,字符串转换成为时间戳
string_2_struct = time.strptime('2016/05/22','%Y/%m/%d') # 将日期字符串 转换成为 struct 时间对象格式
print(string_2_struct)
# time.struct_time(tm_year=2016, tm_mon=5, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=143, tm_isdst=-1)
####
struct_2_stamp = time.mktime(string_2_struct) # 将struct格式的对象转化为 时间戳
print(struct_2_stamp)
## 1463846400.0


# 把时间戳装华为字符串格式
print(time.gmtime(time.time()))#将utc时间戳转换成struct_time格式
print(time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime())) # 2018-01-13 08:03:14


# 时间加减

import datetime

print(datetime.datetime.now()) # 2018-01-13 16:06:34.785807
print(datetime.date.fromtimestamp(time.time()-9000000)) #2017-10-01
print(datetime.datetime.now()+datetime.timedelta(3))# 当前的时间加上3天
print(datetime.datetime.now()-datetime.timedelta(3))# 当前的时间减去3天
print(datetime.datetime.now()+datetime.timedelta(hours=3))# 当前的时间加上3小时
print(datetime.datetime.now()+datetime.timedelta(minutes=30))# 当前的时间加上30分钟


c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) ##2018-01-13 02:03:00.961855

 

posted on 2018-01-13 16:14  dotiger  阅读(105)  评论(0)    收藏  举报

导航