linux时间编程

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值