您的位置:首页 > 运维架构 > Linux

Linux 时间 函数

2010-06-26 21:15 363 查看
Linux日期时间篇
#include<time.h>
char *ctime(const time_t *timep); 将参数中秒数转换为日期和时间形式(当地时间)用指针返回
char * asctime(const struct tm * timeptr);将参数中的结构体转换为日期和时间形式(当地时间)用指针返回
struct tm* gmtime(const time_t * timep);将参数所指的秒数转换为tm格式,并返回指向该tm的指针。(CPP注释:难道该函数的实现用了malloc?那么谁去free??)
struct tm* localtime(const time_t * timep); 同上,只是变为当地时间而已
time_t mktime(strcut tm * timeptr);将tm转换为秒数
time_t time(time_t *t);取得从1970到现在的秒数(就是获取当前的时间),由t带出,同时也用返回值带出,允许参数为NULL。
int gettimeofday ( struct timeval * tv , struct timezone * tz );同上,取得当前时间,但是精度达到微秒级!还可以得到时区信息
int settimeofday ( const struct timeval *tv,const struct timezone *tz); 设置系统时间和时区
相关结构体:
struct tm
{
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;   //日光节约时间的旗标
};
timeval结构定义为:
struct timeval {
long tv_sec; /*秒*/
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年以后)*/
//----------------------------------------------------------------------------------------------------------------------
char *ctime(const time_t *timep);
函数说明:将时间和日期以字符串格式表示为当地时间,将参数 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
//----------------------------------------------------------------------------------------------------------------------
char * asctime(const struct tm * timeptr);
将时间和日期以字符串格式表示
函数说明:将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“Wed Jun 30 21:49:08 1993/n”
返回值:返回一字符串表示目前当地的时间日期。若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime 不同处在于传入的参数是不同的结构。
范例
#include <time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
执行
Sat Oct 28 02:10:06 2000
//----------------------------------------------------------------------------------------------------------------------
gettimeofday(取得目前的时间)
表头文件
#include <sys/time.h>
#include <unistd.h>
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函数说明: 会把目前的时间由tv所指的结构返回,当地时区的信息则放到tz所指的结构中返回。
返回值:成功则返回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
 
//----------------------------------------------------------------------------------------------------------------------
struct tm*gmtime(const time_t*timep);
函数说明:gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。此函数返回的时间日期未经时区转换,而是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
 
//----------------------------------------------------------------------------------------------------------------------
struct tm *localtime(const time_t * timep);
函数说明: 将参数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
 
//----------------------------------------------------------------------------------------------------------------------
time_t mktime(strcut tm * timeptr);
函数说明:将时间结构数据转换成经过的秒数。用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
/*
特别注意:
该函数会认为参数中提供的tm时间结构体为Local Time。并且在转换过程中,该函数会调用其他相关函数获取当前系统的时区信息,然后将tm中的时间和时区信息做相应的运算后才返回UTC Time。所以假设传入tm的时间为:2010年06月26日12:56:28 ,当前系统的时区为+8区. 那么该函数的返回值经过转换后的时间就是:2010年06月26日04:56:28.很显然,该函数认为你传入的是local time ,而uct time = local time - 8

另外:
该函数会忽略参数tm的如下两个结构体成员:
int tm_wday;        /* day of the week */
int tm_yday;        /* day in the year */
但是不会忽略如下关于夏令时的成员,因此请务必给该成员赋值为0,否则将导致该函数返回错误的值,我看到的错误情况是该函数返回值比正确值小1小时!
int tm_isdst;       /* daylight saving time */
返回值:返回经过的秒数。

The mktime() function converts a broken-down time structure, expressed as local time, to calendar time representation. The function ignores the specified contents of the structure members tm_wday and tm_yday  and recomputes them from the other information in the broken-down time structure. If structure members are outside their legal interval, they will be normalized (so that, e.g., 40 October is changed into 9 November). Calling mktime() also sets the external variable tzname with information about the current time zone. If the specified broken-down time cannot be represented as calendar time (seconds since the epoch), mktime() returns a value of (time_t)(-1) and does not alter the tm_wday and tm_yday members of the broken-down time structure.
*/
范例
/* 用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
 
//----------------------------------------------------------------------------------------------------------------------

表头文件
#include<sys/time.h>
#include<unistd.h>
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函数说明:设置目前时间。 会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。
返回值:成功则返回0,失败返回-1,错误代码存于errno。
错误代码:EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。
 
//----------------------------------------------------------------------------------------------------------------------

time_t time(time_t *t);
函数说明:取得目前的时间。此函数会返回从公元 1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
返回值:成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于 errno中。
范例
#include<time.h>
mian()
{
int seconds= time((time_t*)NULL);
printf(“%d/n”,seconds);
}
//----------------------------------------------------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息