国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

解析Linux下的時間函數(shù):設(shè)置以及獲取時間的方法

2020-01-26 16:07:58
字體:
供稿:網(wǎng)友
一、時間函數(shù)
復(fù)制代碼 代碼如下:

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); //獲取的為英國時間
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);      //獲取的為本地時間,注意與英國時間的區(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è)置和獲取時間
復(fù)制代碼 代碼如下:

#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ù)來做偽定時器
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è)置時間
t1 = mktime(my_tm);
//獲取時間

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;
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 雅安市| 枞阳县| 广丰县| 中卫市| 灵山县| 丁青县| 岑溪市| 丰台区| 新民市| 湘潭市| 师宗县| 武功县| 襄垣县| 行唐县| 天镇县| 高雄县| 沈阳市| 大安市| 临猗县| 米林县| 手游| 绥江县| 巴彦淖尔市| 明星| 汾阳市| 工布江达县| 奈曼旗| 阿合奇县| 武陟县| 栾城县| 顺平县| 贵南县| 石首市| 延川县| 合作市| 彰武县| 浦东新区| 泗阳县| 绵竹市| 达日县| 杨浦区|