小记——linux时间

一些概念:Epoch,新纪元,时间点,在linux中是1970-1-1.00:00:00。

1. 获得当前时间

#include <time.h>
time_t time (time_t *t);
#include <sys/time.h>
int gettimeofday (struct timeval *tv, struct timezone *tz);
两个函数代表两种不同的精度,time_t精度为秒,struct timeval精度为微秒(百万分之一秒)。tz已经被弃用,使用时应用设置为NULL。
更高级的接口

#include <time.h>
int clock_gettime (clockid_t clock_id, struct timespec *ts);
struct timespec提供纳秒精度

其中clockid_t为以下值之一:

CLOCK_REALTIME
The system-wide real time (wall time) clock. Setting this clock requires special privileges.

CLOCK_MONOTONIC
A monotonically increasing clock that is not settable by any process. It represents the elapsed time since some unspecified starting point, such as system boot.

CLOCK_MONOTONIC_RAW
Similar to CLOCK_MONOTONIC, except the clock is not eligible for slewing (correction for clock skew). That is, if the hardware clock runs faster or slower than wall time, it won’t be adjusted when read via this clock. This clock is Linux-specific.

CLOCK_PROCESS_CPUTIME_ID
A high-resolution, per-process clock available from the processor. For example, on the x86 architecture, this clock uses the timestamp counter (TSC) register.

CLOCK_THREAD_CPUTIME_ID
Similar to the per-process clock, but unique to each thread in a process. POSIX requires only CLOCK_REALTIME. Therefore, while Linux reliably provides all five clocks, portable code should rely only on CLOCK_REALTIME.

2. 进程时间

#include <sys/times.h>
struct tms {
clock_t tms_utime; /* user time consumed */
clock_t tms_stime; /* system time consumed */
clock_t tms_cutime; /* user time consumed by children */
clock_t tms_cstime; /* system time consumed by children */
};
clock_t times (struct tms *buf);
获取进程运行的时间。

3. 设置时间

#define _SVID_SOURCE
#include <time.h>
int stime (time_t *t);
#include <sys/time.h>
int settimeofday (const struct timeval *tv, const struct timezone *tz);
#include <time.h>
int clock_settime (clockid_t clock_id, const struct timespec *ts);
设置时间需要有CAP_SYS_TIME capability,通常只有root用户才具有。


4. 操作时间

Broken-down time( tm structure )

#include <time.h>
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* the day of the month */
int tm_mon; /* the month */
int tm_year; /* the year */
int tm_wday; /* the day of the week */
int tm_yday; /* the day in the year */
int tm_isdst; /* daylight savings time? */
#ifdef _BSD_SOURCE
long tm_gmtoff; /* time zone's offset from GMT */
const char *tm_zone; /* time zone abbreviation */
#endif /* _BSD_SOURCE */
};

4.1 将time_t转换为tm

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

Expressed in terms of the UTC time zone, on failure, it returns NULL.

4.2 Converts a tm structure—broken-down time—to an ASCII string

#include <time.h>
char * asctime (const struct tm *tm);
char * asctime_r (const struct tm *tm, char *buf);
4.3 Converts a tm structrue to time_t

#include <time.h>
time_t mktime (struct tm *tm);
4.3 Converts time_t to ASCII resprentation

#include <time.h>
char * ctime (const time_t *timep);
char * ctime_r (const time_t *timep, char *buf);
4.4 将time_t转换为当地时间的tm structrue

#include <time.h>
struct tm * localtime (const time_t *timep);
struct tm * localtime_r (const time_t *timep, struct tm *result);
4.5 计算两个time_t之差

#include <time.h>
double difftime (time_t time1, time_t time0);
time_t的精度为秒,可以通过简单的加减来进来时间之差的计算,使用这个函数只是为了兼容而已。








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一尺丈量

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值