十一 獲取時間

2021-10-25 13:45:19 字數 1060 閱讀 9842

我們在以後的遊戲製作課程中,需要與時間打交道。我們即將認識乙個新的模組——time模組。

顧名思義,這個模組的名字在英文中的含義是「時間」,它裡面封裝了很多關於時間的功能,比如說:獲取當前時間,格式化時間等。

老操作,我們開啟乙個空白檔案,先導入它:

from time import time,strftime,sleep
然後,我們來試下time.time()函式:

print

(time(

))

返回了乙個浮點數:

返回的是自格林尼治時間 2023年1月1日0時 ~ 現在的總秒數,這個數字稱為時間戳。我們可以使用這個**來獲取時間過去了多少秒。

如果我們要延時幾秒鐘,可以使用這個**:

print(,

int(time())

)sleep(3)

print(,

int(time())

)

三秒鐘過去後,看一下結果:

如果不是呼叫也用到時間,結果連小數字數也是不變的。這個**可以讓我們在某乙個程式內等待幾秒鐘,考慮到我們並沒有學習多執行緒的知識,我們在遊戲中並不使用它。

我們看到time.time()返回的時間戳我們不經過計算無法看懂,這時候就要用到time.strftime()格式化時間了。

由於我在前期寫過這類文章,在這裡就不再贅述了,請點這句話開啟文章。

strftime(

'%y-%m-%d %h:%m:%s'

)

練習題

1.做乙個定時炸彈,在5秒後,輸出「boom!」

2.死迴圈輸出當前時間,每隔一秒輸出一次。

ntp時間獲取

ntp時間獲取 char szip 512 bool bf hostnametoip www.google.com szip if bf printf s n szip systemtime time bool bflag getcurtime time,61.153.197.226 char sz...

獲取時間方案

方案 優點 僅使用c標準庫 缺點 只能精確到秒級 include time.h include stdio.h intmain void size t strftime char strdest,size t maxsize,const char format,const struct tm tim...

php獲取時間

1 獲取當前日期的前一天 thedate date ymd mktime 0,0,0,date m date d 1,date y 2 獲取指定時間的前一天和後一天 yesterday date y m d strtotime thedate.1 day 前一天 tomorrow date y m ...