第九章 时间练习

1、计算程序执行耗时
import time
start_time=time.time()
sum=0
for i in range(1000):
sum+=i
print(sum)
end_time=time.time()
cost_time=end_time-start_time
print(cost_time)

执行结果:
499500
0.0010020732879638672

import time
def procedure() :
time.sleep(5)

time1 = time.clock()
procedure()
print (time.clock() -time1, “secondsprocess time!”) #使用time.time()来计算程序耗时

time2 = time.time()
procedure()
print (time.time() -time2, “seconds!”)

执行结果:
5.001825537869351 secondsprocess time!
5.000338554382324 seconds!

2、将时间字符串转换为时间戳
mktime函数
import datetime
import time
t = datetime.datetime.now()
print(t)
struct_time = t.timetuple()
print (struct_time)
timestamp = time.mktime(struct_time)
print(timestamp)

执行结果:
2018-10-09 21:43:53.525405
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=9, tm_hour=21, tm_min=43, tm_sec=53, tm_wday=1, tm_yday=282, tm_isdst=-1)
1539092633.0

3、将格式时间字符串转换成时间元组,然后再转换成自定义的时间格式字符串
import datetime

now = datetime.datetime.now()
print (now)
struct_time = now.timetuple()
print(struct_time)

执行结果:
2018-10-09 21:53:52.622463
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=9, tm_hour=21, tm_min=53, tm_sec=52, tm_wday=1, tm_yday=282, tm_isdst=-1)

4、将当前时间戳转换为指定格式日期

import time
now=time.time()
print(now)
t1=time.localtime(now)
print(t1)
t2=time.strftime("%Y-%m-%d %H:%M:%S",t1)
print(t2)
t3=time.gmtime(now)
print(t3)
t4=time.strftime("%Y-%m-%d %H:%M:%S",t3)
print(t4)

执行结果:
1539093807.709625
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=9, tm_hour=22, tm_min=3, tm_sec=27, tm_wday=1, tm_yday=282, tm_isdst=0)
2018-10-09 22:03:27
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=9, tm_hour=14, tm_min=3, tm_sec=27, tm_wday=1, tm_yday=282, tm_isdst=0)
2018-10-09 14:03:27

#coding=utf-8
import time
import locale
locale.setlocale(locale.LC_CTYPE, ‘chinese’)
print(time.localtime())
strTime=time.strftime("%Y年-%m月-%d日 %H:%M:%S",time.localtime())
print(strTime)

执行结果:
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=9, tm_hour=22, tm_min=2, tm_sec=12, tm_wday=1, tm_yday=282, tm_isdst=0)
2018年-10月-09日 22:02:12

5、创建名称为当前时间(年月日)的目录,在这个目录下创建名称为当前时间(年月日)的txt文件,并且输入内容为“你好”
import time
import os
import locale
locale.setlocale(locale.LC_CTYPE, ‘chinese’)
t1=time.strftime("%Y年%m月%d日",time.localtime())
print(t1)
os.chdir(“c:\code”)
os.mkdir(t1)
os.chdir(t1)
filename=t1+".txt"

with open(filename,‘w’,encoding=‘utf-8’) as f:
f.write(“你好”)
print(os.path.abspath(filename))
with open(filename,‘r’,encoding=‘utf-8’) as f1:
print(f1.readlines())

执行结果:
2018年10月09日
c:\code\2018年10月09日\2018年10月09日.txt
[‘你好’]

6、获得三天(三小时和三分钟)前的时间方法

from datetime import *
now = datetime.now()
print(now)
print(now - timedelta(days = 3)) #三天前时间
print(now - timedelta(hours = 3)) #三小时前时间
print(now - timedelta(minutes = 3)) #三分钟前时间

执行结果:
2018-10-09 22:39:16.773737
2018-10-06 22:39:16.773737
2018-10-09 19:39:16.773737
2018-10-09 22:36:16.773737

7、计算昨天和明天的日期
from datetime import *
today = date.today()
print(today)
print(today - timedelta(days = 1)) #昨天日期
print(today + timedelta(days = 1)) #明天日期

执行结果:
2018-10-09
2018-10-08
2018-10-10

8、使用datetime模块来获取当前的日期和时间

import datetime
print(datetime.datetime.now())
print(datetime.datetime.today())

执行结果:
2018-10-09 22:40:44.349600
2018-10-09 22:40:44.349601

from datetime import *
print (datetime.today())
print(datetime.now())

执行结果:
2018-10-09 22:43:49.477526
2018-10-09 22:43:49.477526
9、创建名称为log的目录,目录下创建三个文件夹,名分别为去年今天的日期、当前日期(年月日)、明年今天的日期,然后分别在这三个目录中创建三个.log文件,名分别为当年的今天在当年中第多少天,文件中分别写入当年的今天是这
一年的第几个星期以及当前是星期几。

import time
import os
os.mkdir(“c:\code\log\”)
os.chdir(“c:\code\log\”)

year=[]
now=time.localtime()
this_year=time.strftime("%Y-%m-%d %H:%M:%S",now)
year.append(this_year)
last_year=str(int(this_year[0:4])-1)+this_year[4::]
year.append(last_year)
next_year=str(int(this_year[0:4])+1)+this_year[4::]
year.append(next_year)
print(year)

for i in year:
print("******************")
dir_name=i[0:4]+“年”+i[5:7]+“月”+i[8:10]+“日”
os.mkdir(dir_name)
os.chdir(dir_name)
formattime = time.strptime(i, “%Y-%m-%d %H:%M:%S”)
print(formattime)
file_name=time.strftime(’%j’,formattime)+".log"
print(os.path.abspath(file_name))
file_data=time.strftime(’%U’,formattime)+","+time.strftime(’%w’,formattime)
with open(file_name,‘w+’,encoding=‘utf-8’) as f:
f.write(file_data)
f.seek(0,0)
print(f.readlines())

os.chdir("c:\\code\\log\\")

执行结果:
C:\Users\admin\PycharmProjects\untitled1\venv\Scripts\python.exe C:/Users/admin/PycharmProjects/untitled1/test.py
[‘2018-10-11 22:57:59’, ‘2017-10-11 22:57:59’, ‘2019-10-11 22:57:59’]


time.struct_time(tm_year=2018, tm_mon=10, tm_mday=11, tm_hour=22, tm_min=57, tm_sec=59, tm_wday=3, tm_yday=284, tm_isdst=-1)
c:\code\log\2018年10月11日\284.log
[‘40,4’]


time.struct_time(tm_year=2017, tm_mon=10, tm_mday=11, tm_hour=22, tm_min=57, tm_sec=59, tm_wday=2, tm_yday=284, tm_isdst=-1)
c:\code\log\2017年10月11日\284.log
[‘41,3’]


time.struct_time(tm_year=2019, tm_mon=10, tm_mday=11, tm_hour=22, tm_min=57, tm_sec=59, tm_wday=4, tm_yday=284, tm_isdst=-1)
c:\code\log\2019年10月11日\284.log
[‘40,5’]

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值