Linux时间

目录

时间类结构体

时间类结构体,包含微秒struct timeval{}

时间类结构体,包含纳秒struct timespec{}

定时器结构体struct itimerval{}

日历时间结构体struct tm{}

时间类函数

获取自1970-01-01 00:00:00到此刻的秒数:time_t time(time_t *tloc);

参数:

返回值:

 获取自1970-01-01 00:00:00到此刻的秒数和微秒数:int gettimeofday(struct timeval *tv, struct timezone *tz);

参数: 

返回值:

获取自1970-01-01 00:00:00到此刻的秒数和纳秒数,一些进程、线程的运行时间int clock_gettime(clockid_t, struct timespec *)

参数:

返回值: 

 将自1970-01-01 00:00:00到此刻的秒数转换成数据并存放到tm结构体struct tm *localtime(const time_t * timep);

参数:

返回值: 

 将自1970-01-01 00:00:00到此刻的秒数转换成日历的时间表示方式char *ctime(const time_t *timep);

参数:

返回值:

Linux时间实际运用

Linux设定定时器定时方式函数int setitimer(int which, const struct itimerval *new_value,struct itimerval *old_value);

参数:

返回值:

时间类结构体

时间类结构体,包含微秒struct timeval{}

时间类结构体,包含纳秒struct timespec{}

定时器结构体struct itimerval{}

日历时间结构体struct tm{}

时间类函数

获取自1970-01-01 00:00:00到此刻的秒数:time_t time(time_t *tloc);

参数:

  • time_t *tloc:

    time_t类型变量的地址,用来保存1970-01-01 00:00:00到此刻的秒数,一般传NULL,用返回值接收1970-01-01 00:00:00到此刻的秒数

返回值:

1970-01-01 00:00:00到此刻的秒数 

#include <stdio.h>
#include <time.h>

int main()
{
    time_t nowTime;
    time_t str;
    nowTime = time(&str);
    if(nowTime < 0){
        perror("time");
    }else{
        printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,nowTime\n",nowTime);
        printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,str\n",str);
    }
}

 获取自1970-01-01 00:00:00到此刻的秒数和微秒数:int gettimeofday(struct timeval *tv, struct timezone *tz);

参数: 

  • struct timeval *tv:


    timeval类型结构体地址,用来保存1970-01-01 00:00:00到此刻的秒数和微秒数
  • stuct timezone *tz:

    |timezone类型结构体地址,用来保存时区,和时间的修正方式,一般传NULL,不关心

返回值:

调用成调用成功返回0,调用失败返回-1 

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

int main()
{
    struct timeval nowTime;

    if(gettimeofday(&nowTime,NULL) < 0){
        perror("gettimeofday");
    }else{
        printf("seconds from 1970-01-01 00:00:00 to this moment is %ld\n",nowTime.tv_sec);
        printf("microsecond from 1970-01-01 00:00:00 to this moment is %ld\n",nowTime.tv_usec);
    }
    return 0;
}

 

获取自1970-01-01 00:00:00到此刻的秒数和纳秒数,一些进程、线程的运行时间int clock_gettime(clockid_t, struct timespec *)

参数:

  • clockid_t:
    CLOCK_REALTIME 获取自1970-01-01 00:00:00到此刻的秒数和纳秒数
    CLOCK_MONOTONIC 系统的启动时间,不能被设置
    CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
    CLOCK_THREAD_CPUTIME_ID 本线程运行时间
  • struct timespec *:

    保存该函数结果的秒数和纳秒数

返回值: 

调用成调用成功返回0,调用失败返回-1

#include <stdio.h>
#include <time.h>

int main()
{
    struct timespec nowTime;

    if(clock_gettime(CLOCK_REALTIME,&nowTime) < 0){
        perror("clock_gettime");
    }else{
        printf("seconds from 1970-01-01 00:00:00 to this moment is %ld\n",nowTime.tv_sec);
        printf("nanosecond from 1970-01-01 00:00:00 to this moment is %ld\n",nowTime.tv_nsec);
    }
    return 0;
}

 

 将自1970-01-01 00:00:00到此刻的秒数转换成数据并存放到tm结构体struct tm *localtime(const time_t * timep);

参数:

  • const time_t * timep: 

    time_t类型变量的地址,自1970-01-01 00:00:00到此刻的秒数

返回值: 

成功tm类型结构体地址,保存转换后的数据的结构体地址,失败返回NULL

#include <stdio.h>
#include <time.h>

int main()
{
    time_t nowTime;
    time_t str;
    nowTime = time(&str);
    if(nowTime < 0){
        perror("time");
    }else{
        printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,nowTime\n",nowTime);
        printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,str\n",str);
    }

    struct tm *my_tm;

    my_tm = localtime(&nowTime);

    printf("now year is %d\n",my_tm->tm_year+1900);
    printf("now month is %d\n",my_tm->tm_mon+1);
    printf("now day is %d\n",my_tm->tm_mday);
    printf("now hour is %d\n",my_tm->tm_hour);
    printf("now minute is %d\n",my_tm->tm_min);
    printf("now second is %d\n",my_tm->tm_sec);

    return 0;
}

 

 将自1970-01-01 00:00:00到此刻的秒数转换成日历的时间表示方式char *ctime(const time_t *timep);

参数:

  • const time_t *timep:

    time_t类型变量的地址,自1970-01-01 00:00:00到此刻的秒数

返回值:

返回一个char类型地址,保存该日历时间的字符串地址 

#include <stdio.h>
#include <time.h>

int main()
{
    time_t nowTime;
    time_t str;
    nowTime = time(&str);
    if(nowTime < 0){
        perror("time");
    }else{
        printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,nowTime\n",nowTime);
        printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,str\n",str);
    }

    char *my_time;

    my_time = ctime(&nowTime);

    printf("time is %s",my_time);
    return 0;
}

 

Linux时间实际运用

Linux设定定时器定时方式函数int setitimer(int which, const struct itimerval *new_value,struct itimerval *old_value);

参数:

  • int which:
  • const struct itimerval *new_value:

    itimerval类型结构体地址,用来设置定时时间和定时器开启时间

  • struct itimerval *old_value:

    itimerval类型结构体地址,用来保存定时器原有的值

返回值:

调用成功返回0,调用失败返回-1

Linux定时器:

每1s钟打印一次hello

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>

static int i;

void time_handle(int signum)
{
    i++;
    if(i == 2000){
        printf("hello\n");
        i = 0;
    }
}

int main(int argc,char *argv[])
{
    struct itimerval timer;

    //设定启动定时器的时间
    timer.it_value.tv_sec = 1;
    timer.it_value.tv_usec = 0;

    //设定定时时间
    timer.it_interval.tv_sec = 0;
    timer.it_interval.tv_usec = 500; //0.5ms

    //设定定时方式  
    if(setitimer(ITIMER_REAL,&timer,NULL) == -1){
        perror("setitimer");
        exit(-1);
    } //SIGALRM

    signal(SIGALRM,time_handle);
    while(1);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值