c語言的系統時間

來源:互聯網
上載者:User

1.變數的定義:

struct tm
{
 int tm_sec; /* (0 - 61) */
 int tm_min; /* (0 - 59) */
 int tm_hour; /* (0 - 23) */
 int tm_mday; /* (1 - 31) */
 int tm_mon; /* (0 - 11) */
 int tm_year; /* past 1900 */
 int tm_wday; /* (0 - 6) */
 int tm_yday; /* (0 - 365) */
 int tm_isdst; /* daylight savings flag */
};

2.一個執行個體:讀取系統時間,顯示:

/*
c語言中與系統時間相關的函數的樣本

*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>

void main( void )
{
    /*時間*/
    time_t timer ;

    /*指向struct tm的指標*/
    struct tm *ptrtime;

    /*調用time()函數擷取目前時間*/
    timer = time( NULL ) ;

    /*調用localtime()函數將獲得的系統時間轉化為指向struct tm的指標指向的結構體*/
    ptrtime = localtime( &timer ) ;

    /*用asctime()將結構體轉化為字串輸出*/
    printf("Now is: %s",asctime( ptrtime ) ) ;
    getch();
}

3.相關標頭檔

#include<time.h>

4.其他函數

asctime()
相關函數 time,ctime,gmtime,localtime
表標頭檔 #include<time.h>
定義函數 char * asctime(const struct tm * timeptr);
函數說明 asctime()將參數timeptr所指的tm結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後將結果以字串形態返回。此函數已經由時區轉換成當地時間,字串格式為:“Wed Jun 30 21:49:08 1993/n”
傳回值 若再調用相關的時間日期函數,此字串可能會被破壞。此函數與ctime不同處在於傳入的參數是不同的結構。
附加說明 返回一字串表示目前當地的時間日期。
gettimeofday(取得目前的時間)
相關函數 time,ctime,ftime,settimeofday
表標頭檔 #include <sys/time.h>
#include <unistd.h>
定義函數 int gettimeofday ( struct timeval * tv , struct timezone * tz )
函數說明 gettimeofday()會把目前的時間有tv所指的結構返回,當地時區的資訊則放到tz所指的結構中。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.