Linux System Programming note 11 ——Time

Wall time(or real time)
Process time
Monotonic time

1. Breaking Down Time

struct tm{
     int tm_sec;
     int tm_min;
     int tm_hour;
     int tm_mday;
     int tm_mon;
     int tm_year;
     int tm_wday;
     int tm_yday;
     int tm_isdst;
#ifdef  _BSD_SOURCE
     long tm_gmtoff;
     const char *tm_zone;
#endif
};

2. POSIX Clocks
CLOCK_REALTIME
CLOCK_MONOTONIC
CLOCK_MONOTONIC_RAW
CLOCK_PROCESS_CPUTIME_ID
CLOCK_THREAD_CPUTIME_ID

3. Time Source Resolution
#include <time.h>

int clock_getres(clockid_t clock_id, struct timespec *res);

4. Getting the Current Time of Day
#include <time.h>

time_t time(time *t);  //返回自epoch以来的秒数

5. A Better Interface

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

6. An Advanced Interface
#include <time.h>

int clock_gettime(clockid_t clock_id, struct timespec *ts);

7. Getting the Process Time

#include <sys/time.h>

struct tms {
     clock_t tms_utime;
     clock_t tms_stime;
     clock_t tms_cutime;
     clock_t tms_cstime;
};

clock_t times(struct tms *buf);

8. Setting the Current Time of Day
#define _SVID_SOURCE
#include <time.h>

int stime(time_t *t);

9. Setting Time with Precision
#include <sys/time.h>

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

10. An Advanced Interface for Setting the Time
#include <time.h>

int clock_settime(clockid_t clock_id, const struct timespec *ts);

11. Playing with Time
#include <time.h>

char *asctime(const struct tm *tm); //不是线程安全的
char *asctime_r(const struct tm *tm, char *buf); // 线程安全的

#include <time.h>
time_t mktime(struct tm *tm);

#include <time.h>

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

gmtime() converts the given time_t to a tm structure,
#include <time.h>

struct tm * gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);

#include <time.h>
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);

#include <time.h>

double difftime(time_t time1, time_t time2);


12. Timers
12.1 Simple Alarm
#include <unistd.h>

unsigned int alarm(unsigned int seconds);

12.2 Interval Timers
#include <sys/time.h>

int getitimer(int which, struct itimerval *value);
int setitimer(int which, struct itimerval *value, struct itimerval *ovaule);

struct itimerval {
     struct timeval it_interval;
     struct timeval it_value;
};

13. Advanced Timers
timer_create()
timer_settime()
timer_delete()



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值