C语言时间处理

一、简介

时间处理在编程中经常遇到,包括程序的运行时间和显示时间等。在标准C中, 日期和时间的处理包含在 time.h 的头文件中,需要使用日期和时间相关的类型的函数的话, 需要导入time.h.

二、API

三、实例

1、计算时差

参考:

#include <stdio.h>                                                                                  
#include <sys/time.h>     
#include <unistd.h>       

int main()
{
    struct timeval start, end;
    unsigned long spend_time;

    gettimeofday( &start, NULL );
    printf("start : %d.%d
", start.tv_sec, start.tv_usec);
    sleep(1);
    gettimeofday( &end, NULL );
    printf("end   : %d.%d
", end.tv_sec, end.tv_usec);

    //微秒时差
    spend_time=1000000*(end.tv_sec-start.tv_sec)+(end.tv_usec-start.tv_usec);
    printf("%ld
",spend_time);

    return 0;
}

编译

gcc  -g -o time_diff time_diff.c

运行

image

原文地址:https://www.cnblogs.com/274914765qq/p/4643925.html