您的位置:首页 > 运维架构 > Linux

linux时间编程

2014-04-10 20:21 239 查看
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
struct tm *local;
time_t t;
t=time(NULL);//获取日历时间,即相对1970.1.1.0秒数;
int seconds=time(NULL);
printf("日历时间秒数=%ld\n",t);//将tm时间转换成字符串;

//格林威治时间
local=gmtime(&t);//将日历时间转换成格林威治时间,保存为tm格式;
printf("格林威治时间%s\n",asctime(local));
printf("格林威治时间%s\n",ctime(&t));
//本地时间
local=localtime(&t);//将日历时间转换成本地时间,保存为tm格式;
printf("%s\n",asctime(local));//将tm时间转换成字符串;
printf("%s\n",ctime(&t));//将日历时间转换成字符串;
printf("今天是本年第 :%d日,本周第%d日,距离1970年%d年,本年第%d月,本月第%d天,时:%d分:%d秒:%d\n",local->tm_yday,local->tm_wday,local->tm_year,local->tm_mon,local->tm_mday,local->tm_hour,local->tm_min,local->tm_sec);

struct timeval tv_start,tv_end;

//      struct timezone tz1;
printf("2s延时\n");
gettimeofday(&tv_start,NULL);
printf("开始时刻%ld秒%ld微妙\n",tv_start.tv_sec,tv_start.tv_usec);
sleep(2);
gettimeofday(&tv_end,NULL);
printf("结束时刻%ld秒%ld微妙\n",tv_end.tv_sec,tv_end.tv_usec);
printf("10us延时\n");
gettimeofday(&tv_start,NULL);
printf("开始时刻%ld秒%ld微妙\n",tv_start.tv_sec,tv_start.tv_usec);
usleep(10);
gettimeofday(&tv_end,NULL);
printf("结束时刻%ld秒%ld微妙\n",tv_end.tv_sec,tv_end.tv_usec);
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: