Python 模块之 time & datetime

Python 中提供了对时间日期的多种多样的处理方式,主要是在有 time 和 datetime 两个模块。

time

      在 Python 文档里,time 是归类在 Generic Operating System Services 中,它提供的功能是更加接近于操作系统层面的。通读文档可知,time 模块是围绕着 Unix Timestamp 进行的。

      该模块主要包括一个类 struct_time,另外其他几个函数及相关常量。 需要注意的是在该模块中的大多数函数是调用了所在平台C library的同名函数, 所以要特别注意有些函数是平台相关的,可能会在不同的平台有不同的效果。另外一点是,由于是基于Unix Timestamp,所以其所能表述的日期范围被限定在 1970 - 2038 之间,如果你写的代码需要处理在前面所述范围之外的日期,那可能需要考虑使用datetime模块更好。具体看看怎么用:

time()      -- 返回以浮点数计算从1970年至当前时间的秒数
clock()     -- 返回以浮点数计算从进程开始所用CPU时间
sleep()     -- 推迟给定数字秒数
gmtime()    -- 把自1970年至当前的时间秒数转换为 UTC 结构化元组
localtime() -- 把自1970年至当前的时间秒转换为时间元组
asctime()   -- 转换时间元组为字符时间格式
ctime()     -- 转换时间戳秒为字符时间格式
mktime()    -- 将当前时间元组转换成自1970年的时间戳秒
strftime()  -- 按照指定格式转换时间元组为字符
strptime()  -- 按照指定格式解析字符为时间元组
tzset()     -- 设置当前时区

应用实例

# 时间戳
time.time()
1414332433.345712
 
# 结构化时间格式
timestamp = time.time()
time.gmtime(timestamp)
time.struct_time(tm_year=2014, tm_mon=10, tm_mday=26, tm_hour=14, tm_min=7, tm_sec=13, tm_wday=6, tm_yday=299, tm_isdst=0)
 
# 当前时间转换为时间元组 
time.localtime(timestamp)
time.struct_time(tm_year=2014, tm_mon=10, tm_mday=26, tm_hour=22, tm_min=7, tm_sec=13, tm_wday=6, tm_yday=299, tm_isdst=0)
struct_time = time.localtime(timestamp)
 
# 时间戳转换为时间字符
time.ctime(timestamp)
'Sun Oct 26 22:07:13 2014'
 
# 时间元组转换为时间字符 
time.asctime(struct_time)
'Sun Oct 26 22:07:13 2014'
 
# 时间元组转换为时间戳 
time.mktime(struct_time)
1414332433.0
 
# 把给定时间格式的时间元组转换为时间字符
time.strftime("%a, %d %b %Y %H:%M:%S +0000", struct_time)
'Sun, 26 Oct 2014 22:07:13 +0000'
 
# 把给定格式时间字符转换为时间元组
time.strptime("30 Nov 00", "%d %b %y")
time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

 datetime

datetime 比 time 高级了不少,可以理解为 datetime 基于 time 进行了封装,提供了更多实用的函数。

在datetime 模块中包含了几个类,具体关系如下:

timedelta     --主要用于计算时间跨度
tzinfo        --时区相关
time          --只关注时间
date          --只关注日期
datetime      --同时有时间和日期

在实际实用中,用得比较多的是 datetime.datetime 和 datetime.timedelta ,另外两个 datetime.date 和 datetime.time 实际使用和 datetime.datetime 并无太大差别。 使用datetime.datetime.now()可以获得当前时刻的datetime.datetime 实例。 对于一个 datetime.datetime 实例,主要会有以下属性及常用方法,看名称就能理解,应该没有太大问题:

datetime.year
datetime.month
datetime.day
datetime.hour
datetime.minute
datetime.second
datetime.microsecond
datetime.tzinfo
datetime.date()                # 返回 date 对象
datetime.time()                # 返回 time 对象
datetime.replace(name=value)   # 前面所述各项属性是 read-only 的,需要此方法才可更改
datetime.timetuple()           # 返回time.struct_time 对象
datetime.strftime(format)      # 按照 format 进行格式化输出
datetime.today() # 当前时间, localtime
datetime.now([tz]) # 当前时间默认 localtime
datetime.utcnow() # UTC 时间
datetime.fromtimestamp(timestamp[,tz]) # 由 Unix Timestamp 构建对象
datetime.strptime(date_string,format) # 给定时间格式解析字符串

 日期计算应用

import time
time_now = datetime.datetime.now()
time_now
datetime.datetime(2014, 10, 27, 21, 46, 16, 657523)

delta1 = datetime.timedelta(hours=25)
print(time_now + delta1) # 输出结果:2014-10-28 22:46:16.657523
print(time_now - delta1) # 输出结果:2014-10-26 20:46:16.657523
原文地址:https://www.cnblogs.com/alfred0311/p/7723749.html