linux时间函数详解
一、时间相关说明
格林威治时间表示0时区的标准时间。其他时区的时间和此标准时间均有时间差。UTC(Universal Time Coordinated)是世界协调时间,是格林威治时间在互联网中的表示方法
二、标准C语言时间函数
1、time(取得本地目前的时间秒数)
#include<time.h>
time_t time(time_t *t);
函数说明 此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒(Epoch,linux纪元)算起到现在所经过的秒数。如果t并非空指针的话,此函数也会将返回值存到t指针所指的内存。
返回值 成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
time_t定义为long int
范例 #include<time.h>
mian()
{
long int seconds=time((time_t*)NULL);
printf(“%d\n”,seconds);
}
执行 9.73E+08
2、gmtime(根据本地时间取得目前的UTC时间)
#include<time.h>
struct tm*gmtime(consttime_t*timep);
函数说明 gmtime()将参数timep所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义为
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;
};
int tm_sec代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min代表目前分数,范围0-59
int tm_hour从午夜算起的时数,范围为0-23
int tm_mday目前月份的日数,范围01-31
int tm_mon代表目前月份,从一月算起,范围从0-11
int tm_year从1900 年算起至今的年数
int tm_wday一星期的日数,从星期一算起,范围为0-6
int tm_yday从今年1月1日算起至今的天数,范围为0-365
int tm_isdst日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值 返回结构tm代表目前UTC时间
范例 #include <time.h>
main(){
char*wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
printf(“%s%d;%d;%d\n”,wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
执行 2000/10/28 Sat 8:15:38
3、localtime(取得当地目前UTC时间和日期)
#include<time.h>
struct tm *localtime(consttime_t * timep);
函数说明 localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。
返回值 返回结构tm代表目前的当地时间。
范例 #include<time.h>
main(){
char*wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /*取得当地时间*/
printf (“%d%d%d ”,(1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf(“%s%d:%d:%d\n”,wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
执行 2000/10/28 Sat 11:12:22
4、ctime(将时间和日期以字符串格式表示)
#include<time.h>
char *ctime(const time_t *timep);
函数说明 ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21:49 :08 1993\n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
返回值 返回一字符串表示目前当地的时间日期。
范例 #include<time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,ctime(&timep));
}
执行 Sat Oct 28 10 : 12 : 05 2000
5、asctime(将时间和日期以字符串格式表示)
#include<time.h>
char * asctime(conststruct tm * timeptr);
函数说明 asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“Wed Jun 30 21:49:081993\n”
返回值 若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
附加说明 返回一字符串表示目前当地的时间日期。
范例 #include <time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
执行 Sat Oct 28 02:10:06 2000
6、mktime(将时间结构数据转换成经过的秒数)
#include<time.h>
time_t mktime(strcut tm *timeptr);
函数说明 mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0秒算起至今的UTC时间所经过的秒数。
返回值 返回经过的秒数。
范例 /*用time()取得时间(秒数),利用localtime()
转换成struct tm再利用mktine()将struct tm转换成原来的秒数*/
#include<time.h>
main()
{
time_t timep;
strcut tm *p;
time(&timep);
printf(“time() : %d\n”,timep);
p=localtime(&timep);
timep = mktime(p);
printf(“time()->localtime()->mktime():%d\n”,timep);
}
执行 time():974943297
time()->localtime()->mktime():974943297
三、linux系统时间函数
1、gettimeofday(取得目前的时间)
#include<sys/time.h>
#include <unistd.h>
int gettimeofday ( structtimeval * tv , struct timezone * tz )
函数说明 gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeval结构定义为:
struct timeval{
long tv_sec; /*秒,也是从linux纪元时间开始的秒数,和用time函数获取的数据一致*/
long tv_usec; /*微秒*/
};
timezone结构定义为:
struct timezone{
int tz_minuteswest; /*和Greenwich时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime所代表的状态如下
DST_NONE /*不使用*/
DST_USA /*美国*/
DST_AUST /*澳洲*/
DST_WET /*西欧*/
DST_MET /*中欧*/
DST_EET /*东欧*/
DST_CAN /*加拿大*/
DST_GB /*大不列颠*/
DST_RUM /*罗马尼亚*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/
返回值 成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
范例 #include<sys/time.h>
#include<unistd.h>
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv ,&tz);
printf(“tv_sec; %d\n”,tv,.tv_sec) ;
printf(“tv_usec;%d\n”,tv.tv_usec);
printf(“tz_minuteswest;%d\n”, tz.tz_minuteswest);
printf(“tz_dsttime,%d\n”,tz.tz_dsttime);
}
执行 tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
2、settimeofday(设置目前时间)
#include<sys/time.h>
#include<unistd.h>
int settimeofday ( conststruct timeval *tv,const struct timezone *tz);
函数说明 settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。
返回值 成功则返回0,失败返回-1,错误代码存于errno。
错误代码 EPERM并非由root权限调用settimeofday(),权限不够。
EINVAL时区或某个数据是不正确的,无法正确设置时间。
范例 #include <sys/time.h>
#include <unistd.h>
mian()
{
char t_string[] = "2012-04-28 22:30:00";
struct tm time_tm;
struct timeval time_tv;
time_t timep;
int ret = 0;
sscanf(t_string, "%d-%d-%d %d:%d:%d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday, &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec);
time_tm.tm_year -= 1900;
time_tm.tm_mon -= 1;
time_tm.tm_wday = 0;
time_tm.tm_yday = 0;
time_tm.tm_isdst = 0;
timep = mktime(&time_tm);
time_tv.tv_sec = timep;
time_tv.tv_usec = 0;
ret = settimeofday(&time_tv, NULL);
if(ret != 0)
{
fprintf(stderr, "settimeofday failed\n");
return -1;
}
return 0;
}
3、clock_gettime(获取指定时钟的时间值)
#include <time.h>
int clock_gettime(clockid_t clock_id,struct timespec * tp );
说明:clock_id指定要获取时间的时钟,根据Posix的指定可以是以下值:
CLOCK_REALTIME
Systemwide realtime clock.
CLOCK_MONOTONIC
Represents monotonic time.Cannot be set.
CLOCK_PROCESS_CPUTIME_ID
High resolutionper-process timer.
CLOCK_THREAD_CPUTIME_ID
Thread-specific timer.
CLOCK_REALTIME_HR
High resolution version ofCLOCK_REALTIME.
CLOCK_MONOTONIC_HR
High resolution version ofCLOCK_MONOTONIC.
struct timespec {
time_ttv_sec; /* seconds */
long tv_nsec; /* nanoseconds纳秒*/
};
4、adjtimex(tune kernel clock)
#include<sys/timex.h>
int adjtimex(struct timex*buf);
说明:
Linux uses David L. Mills' clock adjustment algorithm (see RFC 1305).The system calladjtimex() reads and optionally sets adjustment parame-ters for this algorithm. It takes a pointer to a timexstructure,updates kernel parameters from field values, and returns the same structure with current kernel values. This structure is declared as follows:
struct timex {
intmodes; /* modeselector */
longoffset; /* time offset (usec)*/
longfreq; /* frequencyoffset (scaled ppm) */
longmaxerror; /* maximum error (usec) */
longesterror; /* estimated error (usec) */
intstatus; /* clockcommand/status */
longconstant; /* pll time constant */
longprecision; /* clock precision (usec) (read only)*/
longtolerance; /* clock frequency tolerance (ppm)(read only) */
struct timeval time; /*current time (read only) */
longtick; /* usecsbetween clock ticks */
};
The modes field determineswhich parameters, if any, to set. It may contain abitwise-or combination of zero or more of the following bits:
#defineADJ_OFFSET 0x0001 /* time offset */
#defineADJ_FREQUENCY 0x0002 /*frequency offset */
#defineADJ_MAXERROR 0x0004 /*maximum time error */
#defineADJ_ESTERROR 0x0008 /*estimated time error */
#defineADJ_STATUS 0x0010 /* clock status */
#defineADJ_TIMECONST 0x0020 /* plltime constant */
#define ADJ_TICK 0x4000 /* tick value */
#defineADJ_OFFSET_SINGLESHOT 0x8001 /* old-fashioned adjtime() */
Ordinary users arerestricted to a zero value for mode. Only the supe-ruser may set anyparameters.
RETURN VALUE
On success, adjtimex() returnsthe clock state:
#defineTIME_OK 0 /* clock synchronized */
#define TIME_INS 1/* insert leap second */
#define TIME_DEL 2/* delete leap second */
#define TIME_OOP 3/* leap second in progress */
#define TIME_WAIT 4 /*leap second has occurred */
#define TIME_BAD 5/* clock not synchronized */
On failure, adjtimex()returns -1 and sets errno.
ERRORS
EFAULT
buf does not point towritable memory.
EINVAL
An attempt is made to setbuf.offset to a value outside the range -131071 to +131071, or to setbuf.status to a value other than those listed above, or to set buf.tick to avalue outside the range 900000/HZ to 1100000/HZ, where HZ is the system timer interrupt frequency.
EPERM
buf.mode is non-zero andthe caller does not have sufficient privilege.Under Linux the CAP_SYS_TIMEcapability is required.
CONFORMING TO
adjtimex() is Linuxspecific and should not be used in programs intended to be portable. Seeadjtime(3) for a more portable, but less flexible, method of adjusting thesystem clock.
linux时间函数详解相关推荐
- linux下wait函数,Linux wait函数详解
wait和waitpid出现的原因 SIGCHLD --当子进程退出的时候,内核会向父进程SIGCHLD信号,子进程的退出是个异步事件(子进程可以在父进程运行的任何时刻终止) --子进程退出时,内核将 ...
- Asp 时间函数详解
now() 获取当前系统日期和时间,ASP输出可以这样写:<%=now()%> Year(now()) 获取年份, ASP输出:<%=Year(now())%> Month(n ...
- linux系统时间函数,Linux时间时区详解与常用时间函数
时间与时区 整个地球分为二十四时区,每个时区都有自己的本地时间. UTC时间 与 GMT时间 我们可以认为格林威治时间就是时间协调时间(GMT = UTC),格林威治时间和UTC时间都用秒数来计算的. ...
- python时间函数详解_Python:Numpy库基础分析——详解datetime类型的处理
原标题:Python:Numpy库基础分析--详解datetime类型的处理 Python:Numpy库基础分析--详解datetime类型的处理 关于时间的处理,Python中自带的处理时间的模块就 ...
- MySQL内置函数中的日期和时间函数详解
标题:MySQL函数大全 出处:俊的博客 时间:Sat, 14 Mar 2009 14:33:22 +0000 作者:hhj 地址:http://hhj.gx.cn/post/308/ 内容: lec ...
- linux 信号处理函数详解
转自:http://blog.csdn.NET/sddzycnqjn/article/details/7285760 1. 信号概念 信号是进程在运行过程中,由自身产生或由进程外部发过来的消息(事件 ...
- linux mmap 函数详解,mmap函数详解与代码实操
icon1.jpg mmap 函数是 unix/linux下的系统调用. 当存在客户-服务程序中复制文件时候,其数据流如下,要经历四次数据复制,开销很大. image.png 果采用共享内存的方式,那 ...
- linux select函数详解
在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数的参数会告诉内核: •我们所关心的文件描述符 •对每个描述符,我们所关心的状态.(我们是要想从一个文件描述符中 ...
- php 时间函数详解,PHP时间函数date()详解
html> /* php语言中默认设置的是标准的格林威治时间(即采用的是零时区),与我们本地的时间相差8个小时.所以要获取本地当前时间必须要更改PHP语言中的时区设置,只需在date()函数前加 ...
最新文章
- 2022 美国国家工程院院士公布:张宏江等入选海外院士,马斯克及微软CEO在列...
- Google CEO Sundar Pichai :“谷歌最大的威胁就是自身的成功”
- 经典C语言程序100例之二九
- 如何控制product search attribute支持的操作类型
- 阅读器关闭时尝试调用Read无效时的解决方法
- linux的服务文件,Linux的nfs文件服务
- SurfaceFlinger中queueBuffer与dequeueBuffer作用(十二)
- vue快速复制快捷键_vue快捷键.doc
- 【leetcode】杨辉三角Ⅱ
- 数据库系统概论-数据库安全性
- android 检测cpu温度传感器,软件是如何测量手机CPU温度的?即使手机没有温度传感器...
- 6319. 【省选组】【USACO 2019 February Platinum】Problem 3. Mowing Mischief
- java实例分析宠物商店_Java实现宠物商店管理
- SysWow64没有权限解决办法
- 学习单片机的几点经验之谈
- mac c语言identifier,mac下自动切换输入法
- 异步协议与同步协议:面向字符的协议BSC协议
- 如何给App快速搭建虚拟服务器
- 360抢夺“度娘”?
- Android TV机顶盒开发简单介绍
热门文章
- 生产环境下,oracle不同用户间的数据迁移。第三部分
- 一直对zookeeper的应用和原理比较迷糊,今天看一篇文章,讲得很通透,分享如下(转)...
- C# 中 NPOI 库读写 Excel 文件的方法【摘】
- jQuery formValidator表单验证插件4.1.1提供下载
- 萧功秦:为什么我们缺少特立独行的人生态度
- 动态添加Html单元格时,事件怎么写?如mouseover事件
- windows系统上安装mysql操作过程及常见错误处理
- ux.form.field.Year 只能选年的时间扩展
- [Android学习笔记四] 自定义Android组件之组合方式创建密码框组件
- 中国内地楼市泡沫严重 租售比1000倍超美国