一、time模块
time模块中时间表现的格式主要有三种:
a、timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。运行“type(time.time())”,返回的是1970纪元后经过的浮点秒数。返回时间戳方式的函数主要有time(),clock()
b、struct_time时间元组,共有九个元素组。4位年数,月,日,小时,分钟,秒,一周中的第几日,一年中的第几日,夏令时(默认为-1)。
c、format time 格式化时间,已格式化的结构使时间更具可读性。包括自定义格式和固定格式。
举例之前作业涉及过的函数:
1.time.time()——time模块的核心模块,返回当前时间的时间戳,返回值为浮点数(这种形式一般用于储存和比较时间,但是对于我们来说看起来太不友好了)
2.time.localtime()——将一个时间戳转换为当前时区的struct_time。
2.time.asctime()——接受时间元组并返回一个比较友好的可读形式如'Sun Jun 20 23:21:05 1993'的字符串。如果没有参数,将会将time.localtime()作为参数传入。
3.time.ctime()——把一个时间戳转化为time.asctime()的形式,作用相当于time.asctime(time.localtime(secs))。

4.time.gmtime()——将一个时间戳转换为UTC时区(0时区)的struct_time。
5.time.mktime()——将一个struct_time转化为时间戳。

6.time.strftime()——用于时间的格式化输出。
7.time.strptime()——用于把格式化时间字符串转化为struct_time。实际上与strftime()是逆操作。


二、datetime模块
datetime模块重新封装了time模块,提供更多接口,提供的类分别为:
a、datetime.date:表示日期的类。常用的属性有year, month, day;
b、datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;(暂不讲)
c、datetime.datetime:表示日期时间。
d、datetime.timedelta:表示时间间隔,即两个时间点之间的长度。
e、datetime.tzinfo:与时区有关的相关信息(这个之前作业也没有涉及,暂时不研究了)
1.date类——datetime.date(year, month, day)
date.today():返回一个表示当前本地日期的date对象;
date.fromtimestamp(timestamp):根据给定的时间戮,返回一个date对象

2.time类——datetime.time(hour[ , minute[ , second[ , microsecond[ , tzinfo]]]] )
3.datetime类——datetime相当于date和time结合起来。datetime.datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )
datetime.today():返回一个表示当前本地时间的datetime对象;
datetime.now([tz]):返回一个表示当前本地时间的datetime对象,如果提供了参数tz,则获取tz参数所指时区的本地时间;
datetime.fromtimestamp(timestamp[, tz]):根据时间戮创建一个datetime对象,参数tz指定时区信息。


4.timedelta类——时间加减。使用timedelta可以很方便的在日期上做天days,小时hour,分钟,秒,毫秒,微妙的时间计算

之前涉及这部分内容的作业链接:
Python Day70&71
网友评论