• time函数及其用法


    st_atime:文件中的数据最后被访问的时间

               Time when file data was last accessed. Changed by  the
               following   functions:   creat(),   mknod(),   pipe(),
               utime(2), and read(2).

     st_mtime:文件中的内容最后修改时间
               Time when data was last modified. Changed by the  fol-
               lowing  functions:  creat(), mknod(), pipe(), utime(),
               and write(2).
     st_ctime:文件的所有者,所属的组,链接数,权限最后发生改变的时间
               Time when file status was last changed. Changed by the
               following   functions:   chmod(),   chown(),  creat(),
               link(2),  mknod(),  pipe(),  unlink(2),  utime(),  and
               write().

    一:结构体的定义

    1、time_t

    time_t实际上是长整数类型
    typedef long time_t; /* time value */

    2.struct timespec

        typedef long time_t;
        #ifndef _TIMESPEC
        #define _TIMESPEC
        struct timespec
        {
            time_t tv_sec; // seconds
            long tv_nsec; // and nanoseconds
        };
        #endif

      struct timespec有两个成员,一个是秒,一个是纳秒, 所以最高精确度是纳秒。
      一般由函数int clock_gettime(clockid_t, struct timespec *)获取特定时钟的时间,常用如下4种时钟:

    1. CLOCK_REALTIME 统当前时间,从1970年1.1日算起
    2. CLOCK_MONOTONIC 系统的启动时间,不能被设置
    3. CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
    4. CLOCK_THREAD_CPUTIME_ID 本线程运行时间

    struct tm *localtime(const time_t *clock);  //线程不安全
    struct tm* localtime_r( const time_t* timer, struct tm* result );//线程安全
    size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );

    3.timeval

    timeval是一个结构体,在time.h中定义为:
    struct timeval
    {
         __time_t tv_sec;                /* Seconds. */
         __suseconds_t tv_usec;      /* Microseconds. */
    };
    其中,tv_sec为Epoch(1970-1-1零点零分)到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。

    4.tm

    tm是一个结构体,定义为:
    struct tm
    {
        int tm_sec;      /*代表目前秒数,正常范围为0-59,但允许至61秒 */
        int tm_min;     /*代表目前分数,范围0-59*/
        int tm_hour;   /* 从午夜算起的时数,范围为0-23 */
        int tm_mday;  /* 目前月份的日数,范围01-31 */
        int tm_mon;   /*代表目前月份,从一月算起,范围从0-11 */
        int tm_year;   /*从1900 年算起至今的年数*/
        int tm_wday;   /* 一星期的日数,从星期一算起,范围为0-6。*/
        int tm_yday;   /* Days in year.[0-365] */
        int tm_isdst;   /*日光节约时间的旗标DST. [-1/0/1]*/
    };

    二:函数的具体操作

    time()

    time_t time(time_t * timer)
      功 能: 获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。然后调用localtime将time_t所表示的CUT时间转换为本地时间(我们是+8区,比CUT多8个小时)并转成struct tm类型,该类型的各数据成员分别表示年月日时分秒。
     程序例1:
      time函数获得日历时间。日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。这个标准时间点对不同的编译器来说会有所不同,但对一个编译系统来说,这个标准时间点是不变的,该编译系统中的时间对应的日历时间都通过该标准时间点来衡量,所以可以说日历时间是“相对时间”,但是无论你在哪一个时区,在同一时刻对同一个标准时间点来说,日历时间都是一样的。

    clock_gettime()

    #include <sys/time.h>
    int clock_gettime(clockid_t clock_id, struct timespec *tsp);
    //返回值:若成功,返回0;若出错,返回-1
      可用于获取指定时钟的时间。返回的时间在timespec结构中,表示为秒和纳秒。

    clock_settime()

      要对特定的时钟设置时间,可以调用clock_settime函数。

    #include <sys/time.h>
    int clock_settime(clockid_t clock_id, const struct timespec *tsp);
    //返回值:若成功,返回0;若出错,返回-1

    gettimeofday

      需要适当的权限来更改时钟值,打哪会有些时钟是不能修改的。

    #include <sys/time.h>
    int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
    //返回值:总是返回0

      tzp的唯一合法值是NULL,其他值将产生不确定的结果。gettimeofday函数以距特定时间(1970年1月1日00:00:00)的秒数的方式将当前时间存放在tp指向的timeval结构中,而该结构将当前时间表示为秒和微秒。

    localtime和gmtime

    #include <time.h>
    struct tm *gmtime(const time_t *calptr);
    struct tm *localtime(const time_t *calptr);
    //返回值:指向分解的tm结构的指针;若出错,返回NULL

      两个函数localtime和gmtime将日历时间转换成分解的时间,并将这些存放在一个tm结构中。

    localtime()和gmtime()的区别:

      localtime将日历时间转换成本地时间,而gmtime则将日历时间转换成协调统一时间的、年、月、日、时、分、秒、周日分解结构。 

    localtime_r()和gmtime_r()函数

      struct tm *gmtime_r(const time_t *timep, struct tm *result); 
      struct tm *localtime_r(const time_t *timep, struct tm *result);
      gmtime_r()函数功能与此相同,但是它可以将数据存储到用户提供的结构体中。
      localtime_r()函数功能与此相同,但是它可以将数据存储到用户提供的结构体中。它不需要设置tzname。
      使用gmtime和localtime后要立即处理结果,否则返回的指针指向的内容可能会被覆盖。
           一个好的方法是使用gmtime_r和localtime_r,由于使用了用户分配的内存,这两个函数是不会出错的。

    mktime()

      函数mktime以本地时间的年、月、日等作为参数,将其变换成time_t值。

    #include <time.h>
    time_t mktime(struct tm *tmptr);
    //返回值:若成功,返回日历时间;若出错,返回-1

    strftime

    size_t strftime(char *restrict buf, size_t maxsize, const char *restrict format, const  struct tm *restrict tmptr);
    size_t strftime_l(char *restrict buf, size_t maxsize,const char *restrict format, const struct tm *restrict tmptr, locale_t locale);
    //返回值:若有空间,返回存入数组的字节数;否则,返回0

      格式化结果存放在一个长度为maxsize个字符的buf数组中,如果buf长度足以存放格式化结果及一个null终止符,则返回在buf中存放的字节数;否则返回0。 
      format参数控制时间值的格式。 

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main(void)
    {
        time_t t;
        struct tm *tmp;
        char buf1[16];
        char buf2[64];
    
        time(&t);
        tmp = localtime(&t);
        if (strftime(buf1, 16, "time and date: %r, %a %b %d, %Y",tmp) == 0)
            printf("buffer1 length is too small
    ");
        else
            printf("%s
    ",buf1);
    
        if (strftime(buf2, 64, "time and date: %r, %a %b %d, %Y",tmp) == 0)
            printf("buffer2 length is too small
    ");
        else
            printf("%s
    ",buf2);
        return 0;
    }

    strptime

      是strftime函数反过来的版本,把字符串时间转换成分解时间。

    #include <time.h>
    char *strptime(const char *restrict buf, const char *restrict format, struct tm *restrict tmptr);
    //返回值:指向上次解析的字符的下一个字符的指针;否则,返回NULL

      format参数给出了buf参数指向的缓冲区的字符串的格式。虽然与strftime函数的说明稍有不同,但是格式说明是类似的。

    asctime()函数

      功 能: 转换日期和时间为相应的字符串(英文简写形式,形如:Mon Feb 16 11:29:26 2009)
      用 法: char *asctime(const struct tm *tblock);

    ctime()函数

      功 能: 把日期和时间转换为字符串。(英文简写形式,形如:Mon Feb 16 11:29:26 2009)

      函数返回的const char *末尾有一个 (换行符)。man手册给出ctime()说明: It  converts  the calendar time t into a null-terminated string of the form;"Wed Jun 30 21:49:08 1993 "

      用 法: char *ctime(const time_t *time);
      说 明:ctime同asctime的区别在于,ctime是通过日历时间来生成时间字符串,
                      而asctime是通过tm结构来生成时间字符串。

    difftime()函数

      功 能:计算时间间隔才长度,以秒为单位,且只能精确到秒。
      原 型:double difftime(time_t time1, time_t time0);
      说 明:虽然该函数返回值是double类型的,但这并不说明该时间间隔具有同double一样的精度,
                       这是由它的参数决定的。

     三:sleep usleep clock

    1..Sleep函数(不同平台、编译器之间可能函数名,函数参数单位不一样)

    头文件:#include<windows.h>

    定义函数:unsigned sleep(unsigned seconds);

    函数说明:此函数执行挂起一段时间。

    example:(对于windows+codeblocks下,Sleep(),单位为ms)

    #include<stdio.h>  
    #include<windows.h>  
    main()  
    {  
        int i,j;  
        i=time((time_t*)NULL);  
        Sleep(2000); //延迟2s  
        j=time((time_t*)NULL);  
        printf("延时了%d秒",j-i);  
    }  

    2.clock函数

    函数定义:clock_t clock(void) ;

    函数说明:该程序从启动到函数调用占用CPU的时间。

    example:

    #include<stdio.h>  
    #include<windows.h>  
    main()  
    {  
        int i,j;  
        Sleep(2000);  
        i=clock();  
        Sleep(2000);  
        j=clock();  
        printf("开始%d
    结束%d
    经过%d
    ",i,j,j-i);  
    } 

    3. usleep 

    头文件: #include <unistd.h>
     功  能: usleep功能把进程挂起一段时间, 单位是微秒(百万分之一秒);
     语  法: void usleep(int micro_seconds);
     返回值: 无
     内容说明:本函数可暂时使程序停止执行。参数 micro_seconds 为要暂停的微秒数(us)。
     注 意:
     这个函数不能工作在windows 操作系统中。用在Linux的测试环境下面。
     参 见:usleep() 与sleep()类似,用于延迟挂起进程。进程被挂起放到reday queue。
     是一般情况下,延迟时间数量级是秒的时候,尽可能使用sleep()函数。
     如果延迟时间为几十毫秒(1ms = 1000us),或者更小,尽可能使用usleep()函数。这样才能最佳的利用CPU时间
     

    时钟换算:
     微秒,时间单位,符号us(英语:microsecond ).
     1微秒等于百万分之一秒(10的负6 次方秒)
     0.000 001 微秒 = 1皮秒
     0.001 微秒 = 1纳秒
     1,000 微秒 = 1毫秒
     1,000,000 微秒 = 1秒
     1s = 1000ms
     1ms = 1000μs
     1μs = 1000ns
     1ns = 1000ps

  • 相关阅读:
    ensp上防火墙上配置nat
    简单介绍oracle误删除表和表数据的恢复方法
    linux基本命令介绍(二)
    linux基本命令介绍(一)
    Vsan分布式文件系统逻辑架构损坏恢复过程
    iPhone手机硬件拆解介绍
    硬盘分区损坏导致SqlServer数据丢失怎么恢复
    安卓手机密码工作原理及破解方式
    EMC UNITY 400存储卷删除数据恢复案例
    服务器2块硬盘掉线的数据恢复过程分享
  • 原文地址:https://www.cnblogs.com/tianzeng/p/9194851.html
Copyright © 2020-2023  润新知