Linux列印time_t的方法__Linux

來源:互聯網
上載者:User
Linux下time_t時間類型
time_t類型定義在time.h標頭檔中:
#ifndef __TIME_T  #define __TIME_T  typedef  long  time_t;  #endif  

可見,time_t實際是一個長整型。其值表示為從UTC(coordinated universal time)時間1970年1月1日00時00分00秒(也稱為Linux系統的Epoch時間)到當前時刻的秒數。由於time_t類型長度的限制,它所表示的時間不能晚於2038年1月19日03時14分07秒(UTC)。為了能夠表示更久遠的時間,可用64位或更長的整形數來儲存日曆時間,這裡不作詳述。
使用time()函數擷取目前時間的time_t值,使用ctime()函數將time_t轉為當地時間字串。
備忘:UTC時間有時也稱為GMT時間,其實UTC和GMT兩者幾乎是同一概念。它們都是指格林尼治標準時間,只不過UTC的稱呼更為正式一點。兩者區別在於前者是天文上的概念,而後者是基於一個原子鐘。

Linux下列印時間的格式為:

printf(“%lu\n”,time);

Linux下通常會涉及到時間參數的列印:比如擷取檔案屬性stat函數,該函數原型為:

  int stat(const char *path, struct stat *buf);  其中buf是一個檔案的資訊結構體,該結構體內成員如下:  struct stat {    dev_t  st_dev; /* ID of device containing file */    ino_t     st_ino;     /* inode number */    mode_t    st_mode;    /* protection */    nlink_t   st_nlink;   /* number of hard links */    uid_t     st_uid;     /* user ID of owner */    gid_t     st_gid;     /* group ID of owner */    dev_t     st_rdev;    /* device ID (if special file) */    off_t     st_size;    /* total size, in bytes */    blksize_t st_blksize; /* blocksize for filesystem I/O */    blkcnt_t  st_blocks;  /* number of 512B blocks allocated */   time_t    st_atime;   /* time of last access */   time_t    st_mtime;   /* time of last modification */   time_t    st_ctime;   /* time of last status change */           };

共用記憶體中的shmctl函數中也會涉及到時間time參數,所以linux開發中難免會用到time參數列印;

聯繫我們

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