解析Linux下的時(shí)間函數(shù):設(shè)置以及獲取時(shí)間的方法
一、時(shí)間函數(shù)
time_t time(time_t *t);
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime(const time_t *timep); //獲取的為英國時(shí)間
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep); //獲取的為本地時(shí)間,注意與英國時(shí)間的區(qū)別。
struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);
double difftime(time_t time1, time_t time0);
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
二、設(shè)置和獲取時(shí)間
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t1;
time_t t2;
struct tm *my_tm;
char buf[128] = {0};
//自Epoch (00:00:00 UTC, January 1,1970)的秒數(shù)
t1 = time(&t1);
printf("%d\n", t1); //1355905754
t2 = time(&t2);
sleep(1);
printf("%lf\n", difftime(t2, t1)); //t1,t2相差:1.000000,有時(shí)候可以用這個(gè)函數(shù)來做偽定時(shí)器
printf("%s\n",ctime(&t1)); //Wed Dec 19 16:29:14 2012
//init tm
my_tm->tm_year = 2012-1900;
my_tm->tm_mon = 12-1;
my_tm->tm_mday = 12;
my_tm->tm_hour = 12;
my_tm->tm_min = 12;
my_tm->tm_sec = 12;
//設(shè)置時(shí)間
t1 = mktime(my_tm);
//獲取時(shí)間
my_tm = localtime(&t1);
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
my_tm->tm_year + 1900, my_tm->tm_mon + 1, my_tm->tm_mday, my_tm->tm_hour, my_tm->tm_min, my_tm->tm_sec);
printf("%s\n", buf);//2012-12-12 12:12:12
return 0;
}
上一篇:用c語言根據(jù)可變參數(shù)合成字符串的實(shí)現(xiàn)代碼
欄 目:C語言
下一篇:深入理解C/C++混合編程
本文標(biāo)題:解析Linux下的時(shí)間函數(shù):設(shè)置以及獲取時(shí)間的方法
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4489.html
您可能感興趣的文章
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)- 解析最少換車次數(shù)的問題詳解
- 01-10深入解析最長公共子串
- 01-10大數(shù)(高精度數(shù))模板(分享)
- 01-10深入解析Linux下\r\n的問題
- 01-10Linux線程管理必備:解析互斥量與條件變量的詳解
- 01-10Linux C 獲取進(jìn)程退出值的實(shí)現(xiàn)代碼
- 01-10深入探討linux下進(jìn)程的最大線程數(shù)、進(jìn)程最大數(shù)、進(jìn)程打開的文
- 01-10DHCP:解析開發(fā)板上動(dòng)態(tài)獲取ip的2種實(shí)現(xiàn)方法詳解
- 01-10基于linux下獲取時(shí)間函數(shù)的詳解
- 01-10linux c 查找使用庫的cflags與libs的方法詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)