你真的了解Python中的日期时间处理吗?

PartheniaVa 7年前
   <p style="text-align:center"><img src="https://simg.open-open.com/show/69d0c2ed958b802e9902c8e3815c19e9.png"></p>    <p>Python中关于时间、日期的处理库有三个:time、datetime和Calendar,其中datetime又有datetime.date、datetime.time、datetime.datetime三个类。而时间又可以分为时间戳、本地时间和UTC时间(世界标准时间)。是不是听起来有点乱?那么他们相互之间有什么区别?有什么联系?又如何转换呢?</p>    <h2><strong>time模块</strong></h2>    <p>在time模块中,时间有三种表现形式:</p>    <ul>     <li>时间戳,一般指Unix时间戳,是从1970年开始到现在的秒数。</li>     <li>本地时间的struct_time形式:一个长度为11的命名元组,第一位为年,第二位为月....</li>     <li>UTC时间的struct_time形式:一个长度为11的命名元组,类似于上个,只不过为UTC时间</li>    </ul>    <p>其中后两者的类型一致,区别在于一个是本地时间(localtime),一个是utc时间。</p>    <p>各种时间形式的获取方式:</p>    <pre>  <code class="language-python">print("time stamp:", time.time())         # 时间戳:1479193181.829338    print("local time:", time.localtime())    # struct_time类型的本地时间  time.struct_time(tm_year=2016, tm_mon=11, tm_mday=15, tm_hour=14, tm_min=59, tm_sec=41, tm_wday=1, tm_yday=320, tm_isdst=0)    print("utc time:", time.gmtime())         # struct_time类型的utc时间  time.struct_time(tm_year=2016, tm_mon=11, tm_mday=15, tm_hour=6, tm_min=59, tm_sec=41, tm_wday=1, tm_yday=320, tm_isdst=0)</code></pre>    <p>这里可以看出,本地时间(北京时间)和UTC时间相差8个小时。</p>    <p>各种时间形式的转换:</p>    <pre>  <code class="language-python">time_stamp = time.time()               # 时间戳  local_time = time.localtime(time_stamp)# 时间戳转struct_time类型的本地时间  utc_time = time.gmtime(time_stamp)     # 时间戳转struct_time类型的utc时间    time_stamp_1 = time.mktime(local_time) # struct_time类型的本地时间转时间戳  time_stamp_2 = calendar.timegm(utc_time)# struct_time类型的utc时间转时间戳  print(time_stamp, time_stamp_1, time_stamp_2)</code></pre>    <p>各种时间形式和字符串之间的转换:</p>    <pre>  <code class="language-python">print(time.ctime(time_stamp))       # 时间戳转字符串(本地时间字符串)    print(time.asctime(local_time))     # struct_time类型的本地时间转字符串  print(time.asctime(utc_time))       # struct_time类型的utc时间转字符串    # struct_time类型的本地时间转字符串:自定义格式  print(time.strftime("%Y-%m-%d, %H:%M:%S, %w", local_time))  # struct_time类型的utc时间转字符串:自定义格式  print(time.strftime("%Y-%m-%d, %H:%M:%S, %w", utc_time))    struct_time = time.strptime("2016-11-15, 15:32:12, 2", "%Y-%m-%d, %H:%M:%S, %w")       # 字符串转struct_time类型</code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/f966f7a62d08bcab4f9f11b6b03d1443.png"></p>    <h2><strong>datetime模块</strong></h2>    <p>接下来再看datetime模块。该模块中包含4个主要的类:</p>    <ul>     <li>datetime.time:时间类,只包含时、分、秒、微秒等时间信息。</li>     <li>datetime.date:日期类,只包含年、月、日、星期等日期信息。</li>     <li>datetime.datetime:日期时间类,包含以上两者的全部信息。</li>     <li>datetime.timedelta:时间日期差值类,用来表示两个datetime之间的差值。</li>    </ul>    <p>这里主要说一下datetime.datetime类的用法,其他两个的用法类似,只有轻微差别:</p>    <pre>  <code class="language-python">a_datetime_local = datetime.datetime.now()  # 获取datetime.datetime类型的本地时间  a_datetime_utc = datetime.datetime.utcnow() # 获取datetime.datetime类型的utc时间    # datetime.datetime类型转字符串  print(a_datetime_local.strftime("%Y-%m-%d, %H:%M:%S, %w"))    # datetime.datetime类型转字符串  print(a_datetime_utc.strftime("%Y-%m-%d, %H:%M:%S, %w"))        a_datetime = datetime.datetime.strptime("2016-11-15, 15:32:12, 2", "%Y-%m-%d, %H:%M:%S, %w")    # 字符串转datetime.datetime格式</code></pre>    <h2><strong>datetime类型和时间戳、struct_time类型的相互转换</strong></h2>    <p>datetime.datetime和时间戳、struct_time之间,也能够相互转换:</p>    <pre>  <code class="language-python">time_stamp = a_datetime_local.timestamp()    # datetime类型转时间戳  print(time_stamp)    a_datetime_local = datetime.datetime.fromtimestamp(time.time())     # 时间戳转datetime.datetime类型的本地时间  a_datetime_utc = datetime.datetime.utcfromtimestamp(time.time())    # 时间戳转datetime.datetime类型的utc时间  print(a_datetime_local, a_datetime_utc)    print(a_datetime_local.timetuple())     # datetime类型转struct_time类型  print(a_datetime_utc.utctimetuple())    # datetime类型转struct_time类型</code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/6b815143c91601d28697948e6827abc5.png"></p>    <p>本文主要讲了各种时间、日期类型的基本用法,以及他们之间相互转换的方法,以及他们和字符串之间相互转换的方法。</p>    <p> </p>    <p> </p>    <p>来自:https://zhuanlan.zhihu.com/p/23679915</p>    <p> </p>