C/C++時間函數總結

來源:互聯網
上載者:User

標籤:int   語言   asc   平台   null   sys   val   oca   結構   

 

 

C語言的時間函數


-------- 標頭檔
time.h
-------- 相關函數和介面
asctime()將時間日期以字串格式表示
ctime()將時間日期以字串格式表示
gettimeofday()擷取目前時間
gmtime()擷取目前時間和日期
localtime()擷取目前時間和日期並轉換為本地時間
mktime()將時間轉換成經過的秒數
settimeofday()設定目前時間戳
time()擷取目前時間(以秒數表示)

struct tm*gmtime(const time_t*timep);
int gettimeofday ( struct timeval * tv , struct timezone * tz )
char *ctime(const time_t *timep);
struct tm *localtime(const time_t * timep);
time_t mktime(strcut tm * timeptr);
int settimeofday ( const struct timeval *tv,const struct timezone *tz);

time_t 是一個長整型數
tm 結構體類型
timeval 包含秒和微秒的結構體

-------- 例子

#include <time.h>main() {time_t timep;time (&timep);printf("%s",asctime(gmtime(&timep)));}

 


C++ 語言的時間函數


標準庫沒有提供所謂的日期類型。C++ 繼承了 C 語言用於日期和時間操作的結構和函數。

 

Windows 平台的時間函數

-------- 標頭檔
<windows.h>
-------- 相關函數和介面
GetSystemTime 獲得UTC(等於GMT)時間

GetLocalTime 獲得系統本地時間

-------- 例子

#include <windows.h>#include <stdio.h>void main(){SYSTEMTIME st, lt;GetSystemTime(&st);GetLocalTime(&lt);printf("The system time is: %02d:%02d\n", st.wHour, st.wMinute);printf("The local time is: %02d:%02d\n", lt.wHour, lt.wMinute);}

 

 

Linux 平台的時間函數

 

-------- 標頭檔
<sys/time.h>

-------- 相關函數和介面
int gettimeofday(struct timeval *restrict tp, void *restrict tzp);

-------- 例子

#include <stdio.h>#include <sys/time.h>int main() {struct timeval start, end;gettimeofday( &start, NULL );sleep(3);gettimeofday( &end, NULL );//求出兩次時間的差值,單位為us int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;printf("time: %d us\n", timeuse);return 0;}

 

C/C++時間函數總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.