当前位置: 技术问答>linux和unix
有问题不明白,半天没弄明白,请求坛子兄弟帮忙。
来源: 互联网 发布时间:2016-08-08
本文导语: #include #include #include int main() { struct tm *tm_ptr, timestruct; time_t the_time; char buf[256]; char *result; (void) time(&the_time); tm_ptr = localtime(&the_time); strftime(buf, 256, “%A %d %B, %I:%S %p”, tm...
#include
#include
#include
int main()
{
struct tm *tm_ptr, timestruct;
time_t the_time;
char buf[256];
char *result;
(void) time(&the_time);
tm_ptr = localtime(&the_time);
strftime(buf, 256, “%A %d %B, %I:%S %p”, tm_ptr);
printf(“strftime gives: %sn”, buf);
strcpy(buf,”Thu 26 July 2007, 17:53 will do fine”);
printf(“calling strptime with: %sn”, buf);
tm_ptr = ×truct;
result = strptime(buf,”%a %d %b %Y, %R”, tm_ptr);
printf(“strptime consumed up to: %sn”, result);
printf(“strptime gives:n”);
printf(“date: %02d/%02d/%02dn”,
tm_ptr->tm_year % 100, tm_ptr->tm_mon+1, tm_ptr->tm_mday);
printf(“time: %02d:%02dn”,
tm_ptr->tm_hour, tm_ptr->tm_min);
exit(0);
}
下面是运行结果
strftime gives: Saturday 09 June, 08:16 AM
calling strptime with: Thu 26 July 2007, 17:53 will do fine
strptime consumed up to: will do fine
strptime gives:
date: 07/07/26
time: 17:53
下面是我不明白的地方
tm_ptr = ×truct;不明白什么意思,特别是timestruct这个结构不知道怎么回事。
#include
#include
int main()
{
struct tm *tm_ptr, timestruct;
time_t the_time;
char buf[256];
char *result;
(void) time(&the_time);
tm_ptr = localtime(&the_time);
strftime(buf, 256, “%A %d %B, %I:%S %p”, tm_ptr);
printf(“strftime gives: %sn”, buf);
strcpy(buf,”Thu 26 July 2007, 17:53 will do fine”);
printf(“calling strptime with: %sn”, buf);
tm_ptr = ×truct;
result = strptime(buf,”%a %d %b %Y, %R”, tm_ptr);
printf(“strptime consumed up to: %sn”, result);
printf(“strptime gives:n”);
printf(“date: %02d/%02d/%02dn”,
tm_ptr->tm_year % 100, tm_ptr->tm_mon+1, tm_ptr->tm_mday);
printf(“time: %02d:%02dn”,
tm_ptr->tm_hour, tm_ptr->tm_min);
exit(0);
}
下面是运行结果
strftime gives: Saturday 09 June, 08:16 AM
calling strptime with: Thu 26 July 2007, 17:53 will do fine
strptime consumed up to: will do fine
strptime gives:
date: 07/07/26
time: 17:53
下面是我不明白的地方
tm_ptr = ×truct;不明白什么意思,特别是timestruct这个结构不知道怎么回事。
|
同上
看manual手册即可
The strptime() function is the converse function to strftime() and converts the character string pointed to by s
to values which are stored in the tm structure pointed to by tm, using the format specified by format. Here for-
mat is a character string that consists of field descriptors and text characters, reminiscent of scanf(3). Each
field descriptor consists of a % character followed by another character that specifies the replacement for the
field descriptor. All other characters in the format string must have a matching character in the input string,
except for whitespace, which matches zero or more whitespace characters in the input string. There should be
whitespace or other alphanumeric characters between any two field descriptors.
将字符串转换成一个tm结构,并且存放起来
这个tm结构的地址由该函数的第三个参数指出
如果
tm_ptr = ×truct 则表示使用的是timestruct的空间
如果没有这句则表示使用的是 tm_ptr = localtime(&the_time);的空间
具体的说:localtime的函数实现中申请了一个static变量,也就是这个变量所用的空间
相关的请参考
struct tm结构体
localtime源码实现
这些都能google到的
看manual手册即可
The strptime() function is the converse function to strftime() and converts the character string pointed to by s
to values which are stored in the tm structure pointed to by tm, using the format specified by format. Here for-
mat is a character string that consists of field descriptors and text characters, reminiscent of scanf(3). Each
field descriptor consists of a % character followed by another character that specifies the replacement for the
field descriptor. All other characters in the format string must have a matching character in the input string,
except for whitespace, which matches zero or more whitespace characters in the input string. There should be
whitespace or other alphanumeric characters between any two field descriptors.
将字符串转换成一个tm结构,并且存放起来
这个tm结构的地址由该函数的第三个参数指出
如果
tm_ptr = ×truct 则表示使用的是timestruct的空间
如果没有这句则表示使用的是 tm_ptr = localtime(&the_time);的空间
具体的说:localtime的函数实现中申请了一个static变量,也就是这个变量所用的空间
相关的请参考
struct tm结构体
localtime源码实现
这些都能google到的
|
在这里赋值的:
result = strptime(buf,”%a %d %b %Y, %R”, tm_ptr);
因为,前面已经有了
tm_ptr = ×truct
所以,给tm_ptr的内存内部赋值,和直接给×truct是一致的。
换句话说,把上一句改成:
result = strptime(buf,”%a %d %b %Y, %R”, ×truct);
也是可以的
result = strptime(buf,”%a %d %b %Y, %R”, tm_ptr);
因为,前面已经有了
tm_ptr = ×truct
所以,给tm_ptr的内存内部赋值,和直接给×truct是一致的。
换句话说,把上一句改成:
result = strptime(buf,”%a %d %b %Y, %R”, ×truct);
也是可以的
|
tm_ptr 是一个 tm的指针变量,它需要接收一个地址!
& 操作符是用来取地址的, ×truct 就表示取timestruct 的地址.
所以tm_ptr = ×truct 就是将地址赋给tm_ptr了!
& 操作符是用来取地址的, ×truct 就表示取timestruct 的地址.
所以tm_ptr = ×truct 就是将地址赋给tm_ptr了!
|
去掉这个时,打印的是tm_ptr原先存储的信息
(void) time(&the_time);
tm_ptr = localtime(&the_time);
这里是tm_ptr第一次赋值的地方,去掉后打印的就是time()给出的当前的时间,而不是
Thu 26 July 2007, 17:53这个时间。