python标准库学习-time,datetime包

时间与日期



time包

time.time()

返回挂钟时间。这个时间是从某个固定时间起点到现在的时间间隔。时间起点的选择与计算机相关,但一台计算机的话,这一时间起点是固定的。其它的日期信息都是从这一时间计算得到的。



time.sleep()

用于将程序置于休眠状态。



print('sleeping...')
time.sleep(5)   #延缓5秒
print('wake up!')123



struct_time对象

time包还定义了struct_time对象。该对象实际上是将挂钟时间转换为年、月、日、时、分、秒……等日期信息,存储在该对象的各个属性中(tm_year, tm_mon, tm_mday…)。下面方法可以将挂钟时间转换为struct_time对象:



st1=time.gmtime()   #返回UTC时间-->世界标准时间
st2=time.localtime()    #返回当地时间
print(st1)
print(st2)1234

输出: 
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=14, tm_hour=14, tm_min=12, tm_sec=49, tm_wday=6, tm_yday=14, tm_isdst=0) 
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=14, tm_hour=22, tm_min=12, tm_sec=49, tm_wday=6, tm_yday=14, tm_isdst=0) 
 
2018年1月14日22:12:49~周天,是今年的第14天(注意tm_wday中0是周一!)





datetime包

datetime可以理解为date和time两个组成部分。date是指年月日构成的日期(相当于日历),time是指时分秒微秒构成的一天24小时中的具体时间(相当于手表)。你可以将这两个分开管理(datetime.date类,datetime.time类),也可以将两者合在一起(datetime.datetime类)。由于其构造大同小异,我们将只介绍datetime.datetime类。



datetime.datetime()

试一下: 
现在时间:2018年1月14号22:53



import datetime
t=datetime.datetime(2018,1,14,22,53)
print(t)123

输出: 
2018-01-14 22:53:00 




datetime.timedelta()–计算时间

时间间隔对象-timedelta。一个时间点(datetime)加上一个时间间隔(timedelta)可以得到一个新的时间点(datetime)。



import datetime
t=datetime.datetime(2018,1,14,22,53)
t_next=datetime.datetime(2018,1,15,22,56)

delta1=datetime.timedelta(seconds=60)
delta2=datetime.timedelta(weeks=3)

print(t)
print(t+delta1)
print(t+delta2)
print(t_next-t) #这个挺棒!不用自己算了1234567891011

输出: 
2018-01-14 22:53:00 
2018-01-14 22:54:00 
2018-02-04 22:53:00 
1 day, 0:03:00 
 
在给datetime.timedelta传递参数(如上的seconds和weeks)的时候,还可以是days, hours, milliseconds, microseconds。

两个datetime对象还可以进行比较。比如使用上面的t和t_next:



print(t>t_next)1



datetime对象与字符转换。

假如我们有一个的字符串,我们如何将它转换成为datetime对象呢? 
(版本python3.6)



import datetime
format="output-%Y-%m-%d-%H%M%S.txt"
str="output-2018-1-14-233300.txt"
t=datetime.datetime.strptime(str,format)
print(t)
print(dir(datetime))
print(dir(datetime.datetime))1234567

输出: 
2018-01-14 23:33:00 
 
[‘MAXYEAR’, ‘MINYEAR’, ‘builtins‘, ‘cached‘, ‘doc‘, ‘file‘, ‘loader‘, ‘name‘, ‘package‘, ‘spec‘, ‘_divide_and_round’, ‘date’, ‘datetime’, ‘datetime_CAPI’, ‘time’, ‘timedelta’, ‘timezone’, ‘tzinfo’] 
 
[‘add‘, ‘class‘, ‘delattr‘, ‘dir‘, ‘doc‘, ‘eq‘, ‘format‘, ‘ge‘, ‘getattribute‘, ‘gt‘, ‘hash‘, ‘init‘, ‘init_subclass‘, ‘le‘, ‘lt‘, ‘ne‘, ‘new‘, ‘radd‘, ‘reduce‘, ‘reduce_ex‘, ‘repr‘, ‘rsub‘, ‘setattr‘, ‘sizeof‘, ‘str‘, ‘sub‘, ‘subclasshook‘, ‘astimezone’, ‘combine’, ‘ctime’, ‘date’, ‘day’, ‘dst’, ‘fold’, ‘fromordinal’, ‘fromtimestamp’, ‘hour’, ‘isocalendar’, ‘isoformat’, ‘isoweekday’, ‘max’, ‘microsecond’, ‘min’, ‘minute’, ‘month’, ‘now’, ‘replace’, ‘resolution’, ‘second’, ‘strftime’, ‘strptime’, ‘time’, ‘timestamp’, ‘timetuple’, ‘timetz’, ‘today’, ‘toordinal’, ‘tzinfo’, ‘tzname’, ‘utcfromtimestamp’, ‘utcnow’, ‘utcoffset’, ‘utctimetuple’, ‘weekday’, ‘year’] 
 
可以看出只有datetime.datetime中有方法strptime()。 
我们通过format来告知Python我们的str字符串中包含的日期的格式。在format中,%Y表示年所出现的位置, %m表示月份所出现的位置……。

反过来,我们也可以调用datetime对象的strftime()方法,来将datetime对象转换为特定格式的字符串。比如上面所定义的t_next,



print(t_next.strftime(format))1

输出: 
output-2018-01-15-225600.txt 



本文来自 DXT00 的CSDN 博客 https://blog.csdn.net/qq_32095699/article/details/79059618?utm_source=copy 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值