time
man 2 time进入man手册查看time这个API的使用方法
根据描述是get time in second
SYNOPSIS
#include <time.h>
time_t time(time_t *tloc);
在这里我遇到了两个问题
问题1:time_t 类型是什么类型?
答:就是一种类型lor,在time.h里面有定义的。
问题2:返回值是什么?
答:返回值就是time_t类型的???看一下return value
RETURN VALUE
On success, the value of time in seconds since the Epoch is returned.
On error, ((time_t) -1) is returned, and errno is set appropriately.
成功的话就返回从Epoch(这里是 1970-01-01 00:00:00 +0000 (UTC).)到现在的时间值,用秒表示。
结论:返回的是一串整数,可能很长。
注意:其实这里不仅仅是有这样一种使用方法的,定义一个time_t类型的变量,然后把这个变量的地址(相当于一个指针)传进去time函数里面,依旧可以返回同样的秒数
从这里我就开始有点意识到其实一个函数不只是说它的返回值有用,也可以通过它的输出型的变量(可能说的不准确)来获得相应的信息。
具体代码:
#include <stdio.h>
#include <time.h>
int main()
{
#if 1
time_t tloc1;
tloc1 = time(NULL);
printf("%ld\n",tloc1);
#endif
#if 1
time_t tloc2;
time(&tloc2);
printf("%ld\n",time(&tloc2));
#endif
return 0;
}
结果如下:
1577108050s通过运算大概就是50year,算算现在都十二月份了,从1970年到现在快2020年了,50年了。
ctime
这回man 2 ctime不行了,然后直接man ctime 发现这是个库函数。
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
ctime 的原型是这样的。跟上文的time比起来
第一个ctime是一个输入型函数,返回值是一个char *类型的,就是字符一类的吧。
第二个ctime_r,它就返回在一个buf里面。
注意:如果就这样开始写的话就GG了
先看看description
The ctime(), gmtime() and localtime() functions all take an
argument of data type time_t, which represents calendar time. When interpreted as an absolute time value,
it represents the number of seconds elapsed since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
就是说这个ctime的话要用到time_t的参数,相当于把之前的time获得的秒数给你从1970年加到现在并且还给你转换成一个固定格式的字符串
#if 1
time_t timep;
timep = time(NULL);
printf("%s\n",ctime(&timep));
#endif
ctime_r
用法跟ctime类似,不过他是输出到一个buf里面,这就涉及到一个可重入以及不可重入的问题,ctime是不可重入的,ctime_r是可以重入的。
#if 1
time_t timep;
timep = time(NULL);
printf("%s\n",ctime(&timep));
char buf[100];
memset(buf,0,sizeof(buf));//要规范,记得memset去清零buf
ctime_r(&timep,buf);//buf本来就是一个数组,数组名也是一个指针,如果在前面加了&变成&buf会报错滴,因为我试过。。。
printf("the time is %s\n",buf);
#endif
这里就补充了一截ctime_r的代码
结果如上。
gmtime
gmtime的原型
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
返回值是一个指针,指向struct tm这样的一个结构体。
Broken-down time is stored in the structure tm, which is defined in
<time.h> as follows:
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};
还有很多,对tim_sec、tm_min等等的解释,没粘贴上来,具体看man手册
//gmtime
#if 0
time_t timep;
time_t temper = time(&timep);
struct tm *a;
a = gmtime(&temper);
printf("UTC时间:%d年%d月%d日 %d:%d:%d\n",
a->tm_year + 1900,a->tm_mon + 1,a->tm_mday,
a->tm_hour,a->tm_min,a->tm_sec);
#endif
// gmtime_r
#if 0
time_t tloc = 0;
printf("tloc %ld\n",tloc);
time(&tloc);
printf("tloc %lds\n",tloc);
/*If tloc is non-NULL, the return value is also stored in the memory
pointed to by tloc.*/
struct tm result;
memset(&result,0,sizeof(result));
gmtime_r(&tloc,&result);
printf("UTC时间:%d年%d月%d日 %d:%d:%d\n",
result.tm_year + 1900,result.tm_mon + 1,result.tm_mday,
result.tm_hour,result.tm_min,result.tm_sec);
#endif
gmtime跟gmtime_r的不同也是在于gmtime_r有个buf,可以把内容输出到buf里面供使用。注意,gmtime的年份是当前年份-1900年的,所以要获得当前年份的话必须要加上1900.
localtime
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
localtime的话跟gmtime的用法是一样的,包括localtime_r,两者不同之处在于,gmtime的话是UTC时间,而localtime的话是本地时间
//localtime
#if 0
time_t tlocc = 0;
time(&tlocc);
struct tm *p;
p = localtime(&tlocc);
printf("本地时间:%d年%d月%d日 %d:%d:%d\n",
p->tm_year + 1900,p->tm_mon + 1,p->tm_mday,
p->tm_hour,p->tm_min,p->tm_sec);
//localtime_r
struct tm res;
memset(&res,0,sizeof(res));
localtime_r(&tlocc,&res);
printf("本地时间:%d年%d月%d日 %d:%d:%d\n",
res.tm_year + 1900,res.tm_mon + 1,res.tm_mday,
res.tm_hour,res.tm_min,res.tm_sec);
//mktime
/*mktime的作用就是把struct tm
这个结构体的时间转换成之前的以秒位单位的time的数字。*/
time_t aaa = mktime(&res);
printf("%ld\n",aaa);
#endif
asctime
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
asctime的话,通过描述可以看到,他是起着从struct tm转换成char类型的作用。
可重入的一个相关演示
其实这里我也不是特地的想去演示的,只是在写的时候突然间发现了这个问题。
//asctime
char buffer1[100];
char buffer2[100];
memset(buffer1,0,sizeof(buffer1));
memset(buffer2,0,sizeof(buffer2));
char *pp1 = buffer1;
char *pp2 = buffer2;
time_t temp1 ;
time(&temp1);
time_t temp2 ;
time(&temp2);
struct tm *pointer1;
struct tm *pointer2;
pointer1 = gmtime(&temp1);
pp1 = asctime(pointer1);
printf("%s",pp1);
pointer2 = localtime(&temp2);
//可重入和不可重入的例子 很鲜明了
pp2 = asctime(pointer2);
printf("%s",pp2);
在这个例子里面,我把localtime和gmtime都运用上了,然后用asctime来转换成字符型,然后printf %s打印出来。
在这里,如果我把printf放到最后的话,两个打印出来的信息都会一样了,为什么,因为gmtime和localtime其实都是依靠在同样的struct tm里面,这个结构体是一样的,应该说是只有一个struct tm,如果你不及时打印出来的话,在下一步执行另外一个(例如之前先执行了gmtime,然后再执行localtime)的话,这样struct tm里面的内容就会被后者替代了,所以printf的信息也会是后者的信息。
这时候的话,需要有一个缓冲区来把数据进行一个暂存。
就像这样:
//asctime_r
char bufasc1[100];
char bufasc2[100];
memset(bufasc1,0,sizeof(bufasc1));
memset(bufasc2,0,sizeof(bufasc2));
time_t t1;
time(&t1);
struct tm *p1;
struct tm *p2;
p1 = gmtime(&t1);
asctime_r(p1,bufasc1);
p2 = localtime(&t1);
asctime_r(p2,bufasc2);
printf("_r:%s",bufasc1);
printf("_r:%s",bufasc2);
这样的好处就是我已经把信息传到不同的buffer里面了,我想什么时候用都可以,另外还有一个好处就是,虽然asctime跟asctime_r都是返回一个char *类型的指针,但是asctime_r还返回到一个buffer里面,所以我想用的时候我不需要先定义一个指针然后指向buffer,而是直接取buffer里面的数值就可以了,不需要多定义一个指针那么麻烦。
mktime
time_t mktime(struct tm *tm);
mktime还是挺简单的,就是time_t类型和struct tm类型的转换。
//mktime
#if 1
time_t tlocc = 0;
time(&tlocc);
struct tm *p;
p = localtime(&tlocc);
printf("本地时间:%d年%d月%d日 %d:%d:%d\n",
p->tm_year + 1900,p->tm_mon + 1,p->tm_mday,
p->tm_hour,p->tm_min,p->tm_sec);
time_t mkt = -1;
mkt = mktime(p);
if(-1 == mkt)
{
perror("mktime");
}
else
{
printf("%ld/s\n",mkt);
}
#endif
strftime
//strftime
#if 0
time_t tloc;
tloc = time(NULL);
struct tm loc;
memset(&loc,0,sizeof(loc));
localtime_r(&tloc,&loc);
char buf[100];
memset(buf, 0, sizeof(buf));
strftime(buf, sizeof(buf), "%y-%m-%d, %H:%M:%S.\n",\
&loc);
printf("%s",buf);
#endif
strftime的原型是这样的
size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);
、
讲真,刚开始不看例子代码我这的有点蒙,我是看了例子代码之后看description才明白的。
The strftime() function formats the broken-down time tm according to the format specification format and places the result in the character array s of size max. The broken-down time structure tm is defined in <time.h>. See also ctime(3).
他就是说这个strftime的功能是可以用户自己把这个struct tm结构体里面的时间自己排版并且放到size max大的一个矩阵a里面,也就是自己设置时间的表现形式lor。当然man手册里面有很多format的说明的,这里就不贴上来了。
gettimeofday
#if 1
struct timeval tv = {0};
struct timezone tz = {0} ;
int ret = -1;
ret = gettimeofday(&tv,&tz);
if (-1 == ret )
{
perror("gettimeofday");
return -1;
}
printf("U-SECOND = %ld\n",tv.tv_usec);
printf("TIMEZONE = %d\n",tz.tz_minuteswest);
printf("TIMEZONE = %d\n",tz.tz_dsttime);
#endif
gettimeofday的话可以获得微秒以及时区以及夏令时等信息。记得它要包含有 #include <sys/time.h> 这个头文件的时候才可以用struct timeval 和struct timezone这两个类型。
usec的话真的只是表示微秒的那一截信息,而不是全部秒数用微秒来表示
这里表示280975个微秒。
然后关于timezone的问题,我也不知道为啥都显示是0,我直接把课堂代码放到我这里编译运行也不行。。。
总结
好多,感觉有点杂乱,到后面也总结得有点随意,不过我确实把我遇到的问题都point了出来,希望以后能对自己有帮助吧。关于timezone的这个如果有人看到的话就帮我指出来吧,科科,估计写得这么烂也就自己会看了。以后解决了这个再回来补吧。