Python時間time庫
time庫
時間獲取函式:
time() # 獲取從1970.7.1 0:0到目前的秒數
ctime() # 獲取一個時間的字串
gmtime() #獲取一個時間資料
例如
>>> time.time()
1523105888.8286796
>>> time.ctime()
'Sat Apr 7 20:58:12 2018'
>>> time.gmtime
<built-in function gmtime>
>>> time.gmtime()
time.struct_time(tm_year=2018 , tm_mon=4, tm_mday=7, tm_hour=12, tm_min=58, tm_sec=22, tm_wday=5, tm_yday=97, tm_isdst=0)
時間格式化
strftime(tpl, ts) # 第一個引數模板, 第二個引數gmtime()
strptime() # 作用和strftime()相反, 結果得到一個gmtime()型別的值
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
'2018-04-07 13:06:49'
引數:
%Y 4位數的年0001-9999
%y 2位數的年
%m 月份00-12
%H 24制小時
%h 12制小時
%M 分鐘
%S 秒
%B 全拼的月份
%b 縮寫的月份
%A 星期
%a 縮寫的星期
%p 上午/下午
例項
>>> strftime('%Y-%m-%d %H:%M:%S', t)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'strftime' is not defined
>>> time.strftime('%Y-%m-%d %H:%M:%S' , t)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 't' is not defined
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
'2018-04-07 13:06:49'
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
'2018-04-07 13:07:09'
>>> strtime = '2018-04-07 13:07:09'
>>> strptime(strtime, ''%Y-%m-%d %H:%M:%S')
File "<stdin>", line 1
strptime(strtime, ''%Y-%m-%d %H:%M:%S')
^
SyntaxError: invalid syntax
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
'2018-04-07 13:08:42'
>>> time.strftime('%y-%m-%d %H:%M:%S', time.gmtime())
'18-04-07 13:08:47'
>>> time.strftime('%y-%M-%d %H:%M:%S', time.gmtime())
'18-08-07 13:08:55'
>>> time.strftime('%y-%M-%D %H:%M:%S', time.gmtime())
'18-09-04/07/18 13:09:06'
>>> time.strftime('%y-%m-%d %H:%M:%S', time.gmtime())
'18-04-07 13:09:16'
>>> time.strftime('%d', time.gmtime())
'07'
>>> time.strftime('%D', time.gmtime())
'04/07/18'
>>> time.strftime('%m', time.gmtime())
'04'
>>> time.strftime('%M', time.gmtime())
'10'
>>> time.strftime('%M', time.gmtime())
'10'
>>> time.strftime('%B', time.gmtime())
'April'
>>> time.strftime('%h, time.gmtime())
File "<stdin>", line 1
time.strftime('%h, time.gmtime())
^
SyntaxError: EOL while scanning string literal
>>> time.strftime('%m, time.gmtime())
File "<stdin>", line 1
time.strftime('%m, time.gmtime())
^
SyntaxError: EOL while scanning string literal
>>> time.strftime('%p %h:%M:%S', time.gmtime())
'PM Apr:13:15'
>>> time.strftime('%p %H:%M:%S', time.gmtime())
'PM 13:13:49'
>>> time.strftime('%p %h:%M:%S', time.gmtime())
'PM Apr:13:57'
>>>
程式計時
sleep() # 休眠, 以s為單位
perf_counter() # 當前時間, 以秒為單位, 獲取差值
製作一個簡單的文字進度條
#-*- coding:-utf -*-
import time
scale = 50
print('執行開始'.center(scale, '-'))
start = time.perf_counter();
for i in range(101):
a = '*'*(i//2)
b = '.'*(50-i//2)
cur = time.perf_counter() - start
print('\r{:>3.0f}%[{}->{}]{:.2f}'.format(i, a, b, cur), end = '')
time.sleep(0.05)
print()
print('執行結束'.center(scale, '-'))
使用\r重新整理當前行
製作簡易時鐘
import time
while 1:
print(time.strftime('\r%Y-%m-%d %H:%M:%S', time.gmtime()), end = '')
time.sleep(1)
相關推薦
Python時間time庫
time庫 時間獲取函式: time() # 獲取從1970.7.1 0:0到目前的秒數 ctime() # 獲取一個時間的字串 gmtime() #獲取一個時間資料 例如 >>> time.time(
sandglass(沙漏)——一個讓人解脫的python時間處理庫
blank git over google nco trac 通過 對象 pytho 在遊戲開發的過程中頻繁的須要跟時間相關的做處理。而python內置了好多個時間處理庫,datetime/date/time/calendar/timedelta等,細節繁多略具迷惑
Python的time庫和文本進度條 大發彩_票平臺搭建
格式 err 類函數 格式化 per .com 開始 range star 大發彩_票平臺搭建 地址一:【hubawl.com】狐霸源碼論壇地址二:【bbscherry.com】 是Python中處理時間的標準庫1、time庫包括三類函數 時間獲取:time() ctime
python 時間time模塊介紹和應用
art 表示 偏移 import end cloc 獲取 mtime mkt 1、其中format_string 類型的時間和struct_time之間可以轉換,timestamp時間戳可以和struct_time之間進行轉化,但是時間戳和格式化時間是不能直接轉換的。 t
關於python時間time、datetime、date之間轉換,獲取今天、昨天、上週、上月、去年的時間
一、time、datetime、date之間轉換 概念: 首先需要理解三個名詞: 時間戳(int), 時間陣列, 日期格式(str) utc time : 國際時間(倫敦時間) localtime: 本地時間時間(如北京時間 = 倫敦時間 + 8小時) 國際時間獲取: time.gm
python中time庫的學習日記
版權宣告 本篇內容為自己的學習內容,僅做日記記錄以便日後複習使用。並非本人原創。 time.sleep延時函式 sleep函式可以實現程式的延時,可控制期望時延後執行下一條語句。 import time time.sleep(10) # 延時 10s, t
09.2 python基礎--time庫
09.2.1 常用函式 time()----獲取當前時間戳,即計算機內部時間值,浮點數 ctime()----獲取當前時間並以易讀方式表示,返回字串 gmtime()----獲取當前時間,表示為計算機可處理的時間格式 import time as t pr
Python時間time詳解
Python中與時間有關的模組time,datetime以及calendar。 -----------------time包----------------- 在Python中,通常有這幾種方式來表示時間: 1)時間戳(timestamp) 通常來說,時間戳表示的是從197
python 時間庫 time
在開始之前,首先要說明這幾點: 1.在Python中,通常有這幾種方式來表示時間:1)時間戳 2)格式化的時間字串 3)元組(struct_time)共九個元素。由於Python的time模組實現主要呼叫C庫,所以各個平臺可能有所不同。 2.UTC(Coordinated
python+opencv計算程式碼執行時間:time庫和opencv自帶方法getTickCount
import cv2 import time ############################## 利用opencv的兩個函式進行時間耗費計算 # cv2.getTickCount()記錄當前
Python 時間庫 之 標準模組time
在學習Python的時間庫時,應最先學習Python標準庫中的模組:Time、Calendar、datetime、pytz、dateutil。打好基礎後,再學習第三方庫。本篇為Python時間庫中的第一篇。 其他模組見: 一 time 模組 下面按照,從基礎概念到常
python 時間模塊小結(time and datetime)
間隔 -i date對象 per inf ear macbook port 兩個 一:經常使用的時間方法 1.得到當前時間 使用time模塊,首先得到當前的時間戳 In [42]: time.time() Out[42]: 1408066927.208922 將時間戳轉換
Python中time模塊和datetime模塊的常用操作以及幾種常用時間格式間的轉換
pyrhon time datatime 幾種常用時間格式的轉換 最常見以及常用的幾種時間格式 1、時間戳(timestamp),時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量。 2、時間元組(struct_time),共有九個元素組。 3、格式化時間(fo
Python時間模塊之Time模塊解析
可選 去掉 說明 協調 all strong haml repr ecs 在我們平常的代碼中,經常需要和時間打交道。在Python中,與時間處理相關的模塊有:time、datetime以及calendar。學會計算時間,對程序的調優非常重要,可以在程序中狂打時間戳,來具體判
python基礎學習時間time和datetime模塊
使用 負數 orm 本地 amp 元組 format 夏令時 字符轉換 時間模塊time和datetime()時間表現為三種格式 1.時間戳 2.時間元組 3.格式化字符串(2017-11-1)時間戳:通常來說時間戳表示的是從格林威治1970年1月1日00:00:00
python時間模塊time和datetime
python時間模塊time python時間模塊datetime python時間模塊 通用時間格式: 1.時間戳(timestamp) import time time.time() 1970年-目前 2.格式化的時間字符串 3.元組(struct_time)共九
python中time模塊獲取時間的使用
轉化 毫秒 3.0 精確 ftime 時間戳 import 格式 local import time() ·獲取本地時間: time.time() #本地時間的時間戳格式。 1514273633.0474658 eg: int(time.time()) , int
Python基礎-----time時間模塊
時間差 min python win cti asc mtime 浮點數 print #!/usr/bin/env python#-*- coding:utf-8 -*-import time#1 時間戳,獲取從1970.1.1 00:00算起到目前的秒數t1 = time
Python time庫常用函數
ont 自定義 spa 使用 info 元素 時間格式 時間戳 struct time模塊中時間表現的格式主要有三種: timestamp 時間戳,時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量 struct_time 時間元組,共有
Python 時間模組time常用操作
time模組---->時間的獲取和轉換 time模組提供各種時間相關的功能 下面列舉一些常用的操作 獲取時間戳 timestamp = time.time() print "時間戳:",timestamp # 時間戳: 1540985031.5 獲