linux時間編程常用函數

來源:互聯網
上載者:User

首先說明時間類型分為兩種:

  • Coordinated Universal Time(UTC):世界標準時間,也就是格林威治時間(Greenwich Mean Time, GMT)
  • Calendar Time:日曆時間,從一個標準時間點(如:1970年1月1日0點)到此時經過的秒數來表示的時間。
時間擷取
#include <time.h>time_t time<time_t *tloc>//功能:擷取日曆時間,從1970年1月1日0點到現在所經曆的秒數//typedef long time_t

時間轉換

struct tm *gmtime(const time_t *timep)//功能:將日曆時間轉化為格林威治時間,並儲存至TM結構。struct tm *localtime(const time_t *timep)//功能將日曆時間轉化為本地時間,並儲存至TM結構struct tm{int tm_sec;//秒值int tm_min;//分鐘值int tm_hour;//小時值int tm_mday;//本月第幾日int tm_mon;//本年第幾月int tm_year;//哪一年int tm_wday;//本周第幾日int tm_yday;//本年第幾日int tm_lsdst;//日光節約時間}

時間顯示

char *asctime(const struct tm *tm)//功能:將TM格式的時間轉換為字串。如//Sat Jul 30 08:43:03 2010char *ctime(const time_t *timep)//功能:將日曆時間轉化為本地時間的字串形式

擷取時間

int gettimeofday(struct timeval *tv, struct timezone *tz)//功能:擷取從今日淩晨到現在的時間差,常用於計算事件耗時。struct timeval{int tv_sec;//秒數int tv_usec;//微秒數}

延時時間

unsigned int sleep(unsigned int seconds)//功能:使程式睡眠seconds秒void usleep(unsigned long usec)//功能:使程式睡眠usec微秒

下面的代碼給出一個簡單的時間編程,顯示出系統目前時間。

#include <stdio.h>#include <time.h>int main(void){time_t seconds;char *timestr;seconds = time(NULL);//擷取日曆時間timestr = ctime(&seconds);//日曆時間轉換為字串printf("%s\n", timestr);//列印顯示return 0;}

輸出為:

Wed Dec 12 14:14:57 2012
相關文章

聯繫我們

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