时间戳

  • 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
  • 注意:目前Python 3.6中支持的最大的时间戳为32535244799(3001-01-01 15:59:59)
  • Python的time 块下有很多函数可以转换常见日期格式。如函数time.time()用于获取当前时间戳。例如:
>>>import time
>>>time.time()


struct_time元组

  • struct_time元组共有9组数字处理时间,包括年、月、日、时、分、秒、一年中第几周、一年中第几天、是否为夏令时。
  • Python函数用一个元组装起来的9组数字处理时间,也被称为struct_time元组。

格式化时间

  • 在python语言中,可以使用time模块的strftime()函数来格式化时间。
  • time.strftime(format [, tuple] ):将日期和时间元组转换成一个格式为format的字符串。
  • 可以根据需求选取各种格式,但是最简单的获取可读的时间模式的函数是asctime()。
  • time.asctime([tuple]):将时间转换成一个24字符的字符串,字符串的格式为"星期 月份 日 时:分:秒 年"。

三者之间的关系

python进阶学习笔记:12 python内置模块--time、datetime_字符串

import time
# time模块的方法
# time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
print(time.localtime())
# time.struct_time(tm_year=2020, tm_mnotallow=5, tm_mday=25, tm_hour=13, tm_min=44, tm_sec=0, tm_wday=1, tm_yday=72, tm_isdst=0)
 
# time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
print(time.gmtime())
# time.struct_time(tm_year=2020, tm_mnotallow=5, tm_mday=25, tm_hour=5, tm_min=49, tm_sec=16, tm_wday=1, tm_yday=72, tm_isdst=0)
 
# time.time():返回当前时间的时间戳。
print(time.time())
# 1520920188.7989295
 
# time.mktime(t):将一个struct_time转化为时间戳。
a = time.localtime()
print(time.mktime(a))
 
# time.sleep(secs):线程推迟指定的时间运行。单位为秒。
time.sleep(0.1)
print("hello world")
 
# time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:
# 'Mon May 25 12:04:38 2020'。如果没有参数,将会将time.localtime()作为参数传入。
a = time.localtime()
print(time.asctime(a))
 
# time.ctime([secs]):把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。
# 如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
print(time.ctime())
 
# time.strftime(format[, t]):把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)
# 转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。
print(time.strftime("%Y-%m-%d %X", time.localtime()) )
#输出2020-05-25 22:22:13
 
# time.strptime(string[, format]):把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
print(time.strftime("2020-05-25 23:58:23"))
# 2020-05-25 23:58:23

time模块

localtime函数
  • localtime()将以秒为单位的时间转换成本地时间。该函数将返回值是一个元组。
  • time.localtime()的语法格式如下:
  • time.localtime([ sec ])
  • 这里的time指的是time模块,secs是指需要转化的时间。如果没有设置secs参数,则使用当前的时间。
clock函数
  • clock()函数返回目前的CPU时间。返回值为一个浮点数。此时间以秒为单位。
  • time.clock()的语法格式如下:
  • time.clock()
  • #原因是 Python3.8 不再支持time.clock,但在调用时,非本工程文件CBTaggingDecoder依然包含该方法。
  • 修改方法:将time.clock 改成 time.perf_counter()
  • 这里的time指的是time模块。该函数没有参数。该函数有两个功能。
  • 第一次调用时,返回程序运行的实际时间。
  • 第二次调用时,返回自第一次调用到这次调用的时间间隔。
gmtime函数
  • localtime()将以秒为单位的时间转换成代表UTC(格林威治时间)的元组。该函数将返回值是一个元组。
  • time.gmtime()的语法格式如下:
  • time.gmtime ([ sec ])
  • 这里的time指的是time模块,secs是指需要转化的时间。如果没有设置secs参数,则使用当前的时间。
mktime函数
  • time.mktime()将time.gmtime()或是time.localtime()函数返回的tuple,转换成以秒为单位的浮点数。该函数执行的操作与time.gmtime()或是time.localtime()函数执行的操作相反。
  • time.mktime()的语法格式如下:
  • time.mktime ([tuple ])
  • 这里的time指的是time模块,tuple是指需要转化的时间。这里的tuple是指结构化的时间或者完整的9位元组元素。如果输入的值不是合法的时间,将会触发OverflowError或ValueError异常。
ctime函数
  • ctime()的作用是把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果不指定参数secs的值或者参数为None,就会默认将time.time()作为参数。ctime()相当于asctime(localtime(secs))。
  • time.ctime()的语法格式如下:
  • time.ctime ([secs])
  • 这里的time指的是time模块,secs是需要转化为字符串时间的秒数。该函数没有任何返回值。
sleep函数
  • sleep()将目前进程置入睡眠状态,睡眠时间为secs秒。
  • sleep()函数的语法格式如下:
  • time.sleep(secs)
  • 这里的time指的是time模块,secs是指需要睡眠的时间。
strptime函数
  • strptime()函数用于根据指定的格式把一个时间字符串转化为struct_time元组。实际上它和strftime()是逆操作。time.strptime()函数的语法格式如下:
  • time.strptime(string [,format])
  • 这里的time指的是time模块,string是指时间字符串,format指格式化字符串。该函数将返回struct_time元组对象。format默认为:"%a %b %d %H:%M:%S %Y"。

日历模块

  • Calendar模块有很广泛的方法用来处理年历和月历。下面将挑选最常用的方法进行讲解。
  1. calendar.calendar(year,w=2,l=1,c=6)#返回一个多行字符串格式的year年年历,3个月一行,间隔距离为c。每日宽度间隔为w字符。每行长度为21* W+18+2* C。l是每星期行数。
  2. calendar.firstweekday( )#返回当前每周起始日期的设置。默认情况下,首次载入caendar模块时返回0,即星期一。


datetime类

  • datetime类其实是date类和time类的合体,其大部分的方法和属性都继承于这二个类
  • datetime类的属性由year年份、month月份、day日期、hour小时、minute分钟、second秒、microsecond毫秒和tzinfo时区。
date类
  • date类的属性由year年份、month月份及day日期三部分构成。
>>> import datetime
>>> a = datetime.date.today()   #返回当前本地时间的datetime对象
>>> a
>>> a.year
>>> a.month
time类
  • time类由hour小时、minute分钟、second秒、microsecond毫秒和tzinfo时区组成。
>>> import datetime
>>> a = datetime.time(11,10,32,888)
>>> a
>>> a.hour

timedelta类

  • timedelta类是用来计算二个datetime对象的差值的。
  • (1) days:天数。
  • (2) microseconds:微秒数(>=0并且<1秒)。
  • (3) seconds:秒数(>=0并且<1天)。
  • 两个date或datetime对象相减就可以返回一个timedelta对象。