本地时间()函数显示二等于日期 [英] localtime() function shows two equals date
本文介绍了本地时间()函数显示二等于日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么当我试图表明2日期与不同的参数,我投入本地时间()函数,控制台显示2等于日期?
这是我的code:
#包括LT&;&stdio.h中GT;
#包括LT&;&CONIO.H GT;
#包括LT&;&time.h中GT;诠释主(){
time_t的时间1,时间2;
结构TM * timeinfo1,* timeinfo2;
字符* time1str,* time2str; 时间1 = 3600;
时间2 = 3720;
timeinfo1 =本地时间(安培;时间1);
timeinfo2 =本地时间(安培;时间2); time1str = asctime(timeinfo1);
time2str = asctime(timeinfo2);
看跌期权(time1str);
看跌期权(time2str); 残培();
返回0;
}
解决方案
数据不会两次调用本地时间
或 asctime 之间仍然存在code>。你必须从某个地方复制数据。下面是纠正例子(仍然有函数strncpy有点问题):
的#include<&stdio.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;&string.h中GT;诠释主(){
time_t的时间1,时间2;
结构TM timeinfo1,timeinfo2,* TI;
炭time1str [256],time2str [256],* TSTR; 时间1 = 3600;
时间2 = 3720;
TI =本地时间(安培;时间1);
的memcpy(安培; timeinfo1,TI的sizeof(* TI));
TI =本地时间(安培;时间2);
的memcpy(安培; timeinfo2,TI的sizeof(* TI)); TSTR = asctime(安培; timeinfo1);
函数strncpy(time1str,TSTR,sizeof的(time1str) - 1);
TSTR = asctime(安培; timeinfo2);
函数strncpy(time2str,TSTR,sizeof的(time1str) - 1); 看跌期权(time1str);
看跌期权(time2str); 返回0;
}
Why when i try to show 2 dates with different arguments, that i put into localtime() function, console show 2 equal dates?
This is my code:
#include<stdio.h>
#include<conio.h>
#include<time.h>
int main() {
time_t time1, time2;
struct tm *timeinfo1, *timeinfo2;
char *time1str, *time2str;
time1 = 3600;
time2 = 3720;
timeinfo1 = localtime(&time1);
timeinfo2 = localtime(&time2);
time1str = asctime(timeinfo1);
time2str = asctime(timeinfo2);
puts(time1str);
puts(time2str);
getch();
return 0;
}
解决方案
Data wouldn't persist between two calls to localtime
or asctime
. You have to copy data somewhere. Here is corrected example (still have little issue with strncpy):
#include <stdio.h>
#include <time.h>
#include <string.h>
int main() {
time_t time1, time2;
struct tm timeinfo1, timeinfo2, *ti;
char time1str[256], time2str[256], *tstr;
time1 = 3600;
time2 = 3720;
ti = localtime(&time1);
memcpy(&timeinfo1, ti, sizeof(*ti));
ti = localtime(&time2);
memcpy(&timeinfo2, ti, sizeof(*ti));
tstr = asctime(&timeinfo1);
strncpy(time1str, tstr, sizeof(time1str) - 1);
tstr = asctime(&timeinfo2);
strncpy(time2str, tstr, sizeof(time1str) - 1);
puts(time1str);
puts(time2str);
return 0;
}
这篇关于本地时间()函数显示二等于日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文