首先講一下#include
和#include
的區別,前者是c99標準庫函式,後者是linux系統函式,如果windows平台裝了mingw(minimalist gnu for windows)工具也是可以使用的,
中呼叫了
。
### 獲取時間戳-time
函式原型:
變數time_t
time
(time_t
*timer)
time_t
實際上是long
型別,使用時既可以傳入變數指標,又可以直接利用其返回值。
使用範例
或者#
include
#include
intmain()
//輸出 time = 1606706081
函式原型:time_t timer;
time
(&timer)
;printf
("time = %d"
, timer)
;
使用範例int
gettimeofday
(struct
timeval
*tv,
struct
timezone
*tz )
struct
timeval
;
根據系統的時區資訊轉換成本地時間#
include
#include
intmain()
//輸出 sec = 1606706045
// usec = 938334
函式原型:
structtm*
localtime
(const
time_t
*timer)
使用範例struct
tm;
函式原型:#
include
#include
intmain()
//輸出 2020-11-30 11:12:37
使用方法模擬localtime。structtm*
gmtime
(const
time_t
*timer)
函式原型:
使用範例time_t
mktime
(struct
tm*tmp)
#
include
#include
intmain()
//輸出 time2 = 1606706081
使用範例char
*ctime
(const
time_t
*timep)
函式原型:#
include
#include
intmain()
//輸出 mon nov 30 11:18:08 2020
使用範例char
*asctime
(const
struct
tm*timeptr)
windows#
include
#include
intmain()
//輸出 mon nov 30 11:18:08 2020
標頭檔案#include
函式原型:
單位毫秒。void
sleep
(unsigned
long time)
linux
標頭檔案#include
函式原型:
單位秒;unsigned
intsleep
(unsigned
int seconds)
;
返回值:若程序/執行緒掛起到引數所指定的時間則返回0,若有訊號中斷則返回剩餘秒數;
單位微秒。void
usleep
(unsigned
long usec)
;
常用時間函式
比較常用的時間函式有time localtime asctime 和gmtime 函式time 的原型為 time t time time t time 函式time 返回系統的當前日曆時間,如果系統丟失時間設定,則函式返回 1。對函式time的呼叫,既可以使用空指標,也可以使用指向time t型別...
php 常用時間函式
php時間 time 來取得伺服器當前時間的時間戳 expiration time 2 3600 得到當前時間延遲2小時候的時間戳 失效驗證 略 計算當前頁面執行時間的例子 do something sleep 3 do something running time time server requ...
mysql常用時間函式
mysql常用的日期函式及用法如下 當前時間 select now 結果 2020 01 18 15 25 26 當前時間戳 select current timestamp 結果 2020 01 18 15 26 36 select current timestamp 結果 2020 01 18 ...