Linux时间编程

这篇博客详细介绍了Linux时间编程,包括获取当前时间、时间格式转换、高精度时间获取以及Linux中的定时器。讲解了time函数、localtime、mktime、asctime、ctime等函数的使用,还涉及到了纳秒级时间获取、POSIX定时器等高级话题,适合Linux系统编程人员学习。

Linux时间编程

获取当前时间

  • 全局变量:xtime
  • typedef long time_t;
  • time_t time(time_t *tloc);

time函数返回当前的时间和日期

#include <time.h>

time_t time(time_t *calptr);

时间值作为函数的返回

如果参数非空,时间值也可以存放在calptr所指向的指针中。

时间格式转换

把秒数转换为年月日时分秒

-struct tm *localtime(const time_t *timep);

-time_t mktime(struct tm *tm);

struct tm {
int tm_sec; / Seconds (0-60) /
int tm_min; /
 Minutes (0-59) /
int tm_hour; /
 Hours (0-23) /
int tm_mday; /
 Day of the month (1-31) /
int tm_mon; /
 Month (0-11) /
int tm_year; /
 Year - 1900 /
int tm_wday; /
 Day of the week (0-6, Sunday = 0) /
int tm_yday; /
 Day in the year (0-365, 1 Jan = 0) /
int tm_isdst; /
 Daylight saving time */
};

将时间转换为字符串

时间格式->字符串格式

  • char *asctime(const struct tm *tm);
  • char *ctime(const time_t *timep);

用户指定格式的字符串

指定格式的字符串

用户自定义格式

  • size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
  • char *strptime(const char *s, const char *format, struct tm *tm);
%a缩写的星期几名称Sun
%A完整的星期几名称Sunday
%b缩写的月份名称Mar
%B完整的月份名称March
%c日期和时间表示法Sun Aug 19 02:56:02 2012
%d一月中的第几天(01-31)19
%H24 小时格式的小时(00-23)14
%I12 小时格式的小时(01-12)05
%j一年中的第几天(001-366)231
%m十进制数表示的月份(01-12)08
%M分(00-59)55
%pAM 或 PM 名称PM
%S秒(00-61)02
%U一年中的第几周,以第一个星期日作为第一周的第一天(00-53)33
%w十进制数表示的星期几,星期日表示为 0(0-6)4
%W一年中的第几周,以第一个星期一作为第一周的第一天(00-53)34
%x日期表示法08/19/12
%X时间表示法02:50:06
%y年份,最后两个数字(00-99)01
%Y年份2012
%Z时区的名称或缩写CDT
%%一个 % 符号%

获取高精度时间

微秒级时间

  • int gettimeofday(struct timeval *tv, struct timezone *tz);
  • int settimeofday(const struct timeval *tv, const struct timezone *tz);

struct timeval

{

time_t	    tv_sec;	       	  /* seconds */
suseconds_t tv_usec;  /* microseconds */

};

struct timezone

{

int tz_minuteswest;   /* minutes west of Greenwich */
int tz_dsttime;		 /* type of DST correction */

};

获取高精度时间:纳秒

纳秒级时间

int clock_gettime(clockid_t clockid, struct timespec *tp);

int clock_settime(clockid_t clockid, const struct timespec *tp);

struct timespec {
time_t tv_sec; /* seconds /
long tv_nsec; /
 nanoseconds */
};

获得高精度时间

纳秒级时间

-参数:clock_id

  • 说明:设置时钟时间类型
  • CLOCK_REALTIME:系统实时时间(从1970-1-1计时)
  • CLOCK_MONOTONIC:系统启动时间
  • CLOCK_PROCESS_CPUTIME_ID:当前进程CPU花费时间
  • CLOCK_THREAD_CPUTIME_ID:当前线程CPU花费时间

Linux中的定时器

内核对定时器的维护

  • 定时器中断——OS维护全局变量——系统调用API——获取/设置时间
  • 定时器中断——周期动作:发信号、新建线程
  • 周期性工作:
    • 定时杀毒
    • 数据备份
    • 邮件提醒
    • 日志清理

Linux中的定时器

相关API

  • 内核API:add_timer、mod_timer、del_timer

  • 简单的闹钟:alarm、sleep、usleep、nanosleep

    • 实现微秒及纳秒级进程usleep,nanosleep
  • 计时器(interval timer):getitimer、setitimer

  • 高级定时器(POSIX timer)

    • timer_create
    • timer_settime
    • timer_gettimer
    • timer_delete
    • timer_getoverrun
  • 定时器+signal

  • interval timer,共享相同的定时器

    • 定时器到期,发送信号到当前进程[定时器精度:微秒级

    #include <sys/time.h>

    int getitimer(int which, struct itimerval *curr_value);
    int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);

  • 函数参数

  • which:定时器类型

  • ITIMER_REAL:实时计时,到期后发送SIGALRM信号到进程

  • ITIMER_VIRTUAL:进程用户态运行才计时,到期发送SIGALRM信号

  • ITIMER_PROF:进程在用户和内核空间倒计时,到期发送SIGPROF信号

struct itimerval {
struct timeval it_interval; /* Interval for periodic timer /
struct timeval it_value; /
 Time until next expiration */
};

struct timeval {
time_t tv_sec; /* seconds /
suseconds_t tv_usec; /
 microseconds */
};

Linux中的定时器:POSIX timer

POSIX定时器API

高级玩意

int timer_create(clockid_t clockid, struct sigevent *sevp, timer_t *timerid);

int timer_settime(timer_t timerid, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);
int timer_gettime(timer_t timerid, struct itimerspec *curr_value);

int timer_getoverrun(timer_t timerid);

int timer_delete(timer_t timerid);

  • POSIX定时器

  • 函数参数:sevp

  • struct sigevent
    {
    __sigval_t sigev_value; //timer的ID
    int sigev_signo; //发送信号类型
    int sigev_notify; //定时器到期以后的动作:发信号、线程
    void (sigev_notify_function)(__sigval_t);/ Function to start */
    void sigev_notify_attributes; / Really pthread_attr_t */
    }

    union sigval
    {
    int sigval_int; // integer value
    void *sigval_ptr; // pointer value
    }

int timer_create(clockid_t clockid, struct sigevent *sevp, timer_t *timerid);

  • POSIX定时器
    • 函数参数:clockid_t
      • CLOCK_REALME
      • CLOCK_MONOTONIC
      • CLOCK_PROCESS_CPUTIME_ID
      • CLOCK_THREAD_CPUTIME_ID
    • sevp.sigev_notify:定时器到期后的动作
      • SIGEV_NONE:什么都不做
      • SIGEV_SIGNAL:发送信号给当前进程
      • SIGEV_THREAD:开启一个线程,执行用户指定的function
      • SIGEV_THREAD_ID:给当前进程中的指定线程发信号

感觉上面这玩意以后用的可能性更大,需要周期性完成一些工作或获取时间。

5秒后到期开辟一个线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值