python3.7 time模塊


#!/usr/bin/env python 
__author__ = "lrtao2010" 

#python3.7 time模塊

#time模塊沒有time.py文件,是內置到解釋器中的模塊

#三種時間表示方式
'''
1、時間戳(timestamp): 通常來說,時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量。
2、格式化的時間字符串:"2018-09-03 10:02:01"
3、元組(struct_time):struct_time元組共有9個元素共九個元素:(年,月,日,時,分,秒,一年中第幾周,一年中第幾天,夏令時)
'''

import time

#時間戳 time()
# print(time.time())
# 1535939025.4159343

#struct_time
#localtime([secs]) 將一個時間戳轉換為當前時區的struct_time。secs參數未提供,則以當前時間為准。
#當地時間
# print(time.localtime(time.time()))
# time.struct_time(tm_year=2018, tm_mon=9, tm_mday=3, tm_hour=9, tm_min=46, tm_sec=7, tm_wday=0, tm_yday=246, tm_isdst=0)
# print(time.localtime()) #
# time.struct_time(tm_year=2018, tm_mon=9, tm_mday=3, tm_hour=9, tm_min=48, tm_sec=19, tm_wday=0, tm_yday=246, tm_isdst=0)

# t_local=time.localtime()
# print(t_local.tm_year)
# print(t_local.tm_mon)
# print(t_local.tm_mday)
# 2018
# 9
# 3

#gmtime([secs])  將一個時間戳轉換為UTC時區(0時區)的struct_time。
# print(time.gmtime())
# time.struct_time(tm_year=2018, tm_mon=9, tm_mday=3, tm_hour=1, tm_min=51, tm_sec=38, tm_wday=0, tm_yday=246, tm_isdst=0)

#mktime(t) : 將一個struct_time轉化為時間戳。
# print(time.mktime(time.localtime()))
# 1535939934.0

# asctime([t]) : 把一個表示時間struct_time表示為這種形式:'Mon Sep  3 10:01:46 2018'。
# 默認將time.localtime()作為參數傳入。

# print(time.asctime())
# Mon Sep  3 10:01:46 2018

#ctime([secs]) : 把一個時間戳轉化為time.asctime()的形式,默認time.time()為參數。
# print(time.ctime())
# Mon Sep  3 10:05:40 2018

#strftime(format[, t])
# 把一個代表時間的struct_time轉化為格式化的時間字符串。
# 如果t未指定,將傳入time.localtime()。
# 如果元組中任何一個元素越界,ValueError的錯誤將會被拋出。
# print(time.strftime("%Y-%m-%d %X"))  #%X 等同於 %H%M%S
# print(time.strftime("%Y-%m-%d %X",time.localtime()))
# print(time.strftime("%Y-%m-%d %H:%M:%S"))
# 2018-09-03 10:14:53
# 2018-09-03 10:14:53
# 2018-09-03 10:14:53

#strptime(string[, format])
# 把一個格式化時間字符串轉化為struct_time。實際上它和strftime()是逆操作。
# print(time.strptime('2018-09-03 10:14:53', '%Y-%m-%d %X'))
# time.struct_time(tm_year=2018, tm_mon=9, tm_mday=3, tm_hour=10, tm_min=14, tm_sec=53, tm_wday=0, tm_yday=246, tm_isdst=-1)

#sleep(secs)
#time.sleep(10) #停止10秒,繼續運行

# import datetime
# print(datetime.datetime.now())
# 2018-09-03 10:20:50.680030

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM