小记——linux时间
2016-07-21 12:23
337 查看
一些概念:Epoch,新纪元,时间点,在linux中是1970-1-1.00:00:00。
1. 获得当前时间
更高级的接口
其中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. 进程时间
3. 设置时间
4. 操作时间
Broken-down time( tm structure )
4.1 将time_t转换为tm
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
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的精度为秒,可以通过简单的加减来进来时间之差的计算,使用这个函数只是为了兼容而已。
相关文章推荐
- 每日一linux命令(35)-------top 命令
- linux中生成shh公用密匙的方法,和快捷配置。
- linux下修改时区(TIMEZONE)
- 纯新手向--Linux搭建SVN服务器
- linux--时间编程(5)
- Linux进程状态解析 之 R、S、D、T、Z、X (主要有三个状态)
- Linux 输入密码无法登陆
- linux等待队列wait_queue_head_t和wait_queue_t
- Linux下安装配置使用 Keepalived
- Linux常用命令之sed
- linux-3.0内核移植
- Linux 下安装 jdk-7u75-linux-x64.gz,jdk1.7.0_75,jdk1.7步骤:
- CentOS7下安装Chrome
- Linux 使用root用户FTP上传
- Linux系统编程--文件(三)
- Linux内存管理原理 与文件读写 图 相当详细
- 【新手出发】从搭虚拟机开始,一步一步在CentOS上跑起来.Net Core程序
- linux make: **** 报错
- U盘安装linux系统
- linux日志文件