/ *从日历time_t日期减去CHGTIME * / time_calendar = time_calendar-(CHG_TIME * 60); tb = *(gmtime(& time_calendar) )); / *计算新时间* / tm =(tb.tm_hour * 100)+ tb.tm_min; / *计算新日期* / dt =(((tb.tm_year%100)* 10000)+((tb.tm_mon + 1)* 100)+(tb .tm_mday)); / *返回新时间&放大器; date * / TIME = tm; DATE = dt; 返回0; } I created a function that breaks down a date into broken down time, Isubtract a certain number of seconds from that, then use mktime() torecompute the calendar time. It works basically except every so often, I get the date 060207 (Feb 7,2006) which is obviously not correct. When it does this it always gives methis date. I tracked it down to my mktime() call, when I get to a certain date, mktimereturns a number in 4000000000, when it should return a number in1000000000. But I am not sure why. The variable in my tm struct all havethe correct date, but mktime returns this. It is happening to me when the date I am changing is: 040727 but it has donethis on others as well. I am thinking that something goes wrong in mktime() to cause it to returnsome default number. Perhaps something wrong in one of my time_brokenmembers and then it gets past to mktime? Suggestions? Thanks a bunch! Here''s my code: int adjust_time(){struct tm time_broken; /* time in broken time */struct tm tb; /* adjusted time */time_t time_calendar=0; /* calendar time as a long int */int sec,min,hour,mday,mon,year;long int dt; /* temp variables */int tm;/* if CHG_TIME==0, no conversion necessary */if(CHG_TIME==0){return 1;} /* parse out time & date info */sec=0;min=TIME%100;hour=TIME/100;mday=DATE%100;mon=((DATE%10000)/100)-1; /* year must be represented as years since 1900 *//* If the year is < 40, we assume it''s in 2000''s *//* If the year is >= 40, we assume it''s in 1900''s */year=(DATE/10000);if(year<40)year=year+100;/* create the struct tm */time_broken.tm_sec=sec;time_broken.tm_min=min;time_broken.tm_hour=hour;time_broken.tm_mday=mday;time_broken.tm_mon=mon;time_broken.tm_year=year; /* get the number of seconds since* Jan 1, 1970 */time_calendar=mktime(&time_broken); /* subtract CHGTIME from calendar time_t date */time_calendar=time_calendar-(CHG_TIME*60);tb=*(gmtime(&time_calendar)); /* calculate new time */tm=(tb.tm_hour*100)+tb.tm_min; /* calculate new date */dt=( ((tb.tm_year%100)*10000) + ((tb.tm_mon+1)*100) + (tb.tm_mday) ); /* return new time & date */TIME=tm;DATE=dt; return 0;}推荐答案 " John Hanley" <公顷**** @ ualberta.ca>在消息中写道 news:11 *************** @ proxy2.srv.ualberta.ca ... "John Hanley" <ha****@ualberta.ca> wrote in messagenews:11***************@proxy2.srv.ualberta.ca...我创建了一个将日期分解为分解时间的函数,我将从中减去一定的秒数,然后使用mktime()来重新计算日历时间。 它基本上除了每隔一段时间工作,我得到的日期是060207(2月7日,2006年),这显然是不正确的。当它执行此操作时,它总是给我这个日期。 我跟踪到我的mktime()调用,当我到达某个日期时, mktime返回一个数字在4000000000,它应该返回一个数字在1000000000.但我不知道为什么。我的tm结构中的变量都有正确的日期,但是mktime会返回这个。 当我改变的日期是040727但它有时发生在我身上这也是在其他人身上完成的。 我认为mktime()中出现问题导致它返回某个默认号码。也许我的time_broken 成员之间出了问题,然后它就过去了mktime? 建议?非常感谢! 这是我的代码: I created a function that breaks down a date into broken down time, I subtract a certain number of seconds from that, then use mktime() to recompute the calendar time. It works basically except every so often, I get the date 060207 (Feb 7, 2006) which is obviously not correct. When it does this it always givesme this date. I tracked it down to my mktime() call, when I get to a certain date,mktime returns a number in 4000000000, when it should return a number in 1000000000. But I am not sure why. The variable in my tm struct all have the correct date, but mktime returns this. It is happening to me when the date I am changing is: 040727 but it hasdone this on others as well. I am thinking that something goes wrong in mktime() to cause it to return some default number. Perhaps something wrong in one of my time_broken members and then it gets past to mktime? Suggestions? Thanks a bunch! Here''s my code: 你没有提供''CHG_TIME''的定义,' 'TIME''或 ''DATE'',因此无法诊断您的问题。试试 发布一个完整的可编译示例,演示 问题。 我不知道是不是一个问题与否,但你提到 的日期是以例如040727让我小心 你,如果那是一个字面数值,请注意任何 数字文字,其第一个数字为零被解释为 由编译器作为八进制(基数为8)表示。 -Mike You don''t provide definitions for ''CHG_TIME'', ''TIME'', or''DATE'', so it''s impossible to diagnose your problem. Tryposting a complete compilable example that demonstratesthe problem. I don''t know if it''s an issue or not, but your reference todates above in the form of e.g. 040727 causes me to cautionyou that if that''s a literal numeric value, note that anynumeric literal whose first digit is zero is interpretedby the compiler as an octal (base eight) representation. -Mike " John Hanley" <公顷**** @ ualberta.ca>在消息中写道 news:11 *************** @ proxy2.srv.ualberta.ca ... "John Hanley" <ha****@ualberta.ca> wrote in messagenews:11***************@proxy2.srv.ualberta.ca...我创建了一个将日期分解为分解时间的函数,我将从中减去一定的秒数,然后使用mktime()来重新计算日历时间。 它基本上除了每隔一段时间工作,我得到的日期是060207(2月7日,2006年),这显然是不正确的。当它执行此操作时,它总是给我这个日期。 我跟踪到我的mktime()调用,当我到达某个日期时, mktime返回一个数字在4000000000,它应该返回一个数字在1000000000.但我不知道为什么。我的tm结构中的变量都有正确的日期,但是mktime会返回这个。 当我改变的日期是040727但它有时发生在我身上这也是在其他人身上完成的。 我认为mktime()中出现问题导致它返回某个默认号码。也许我的time_broken 成员之间出了问题,然后它就过去了mktime? 建议?非常感谢! 这是我的代码: int adjust_time() { struct tm time_broken; / *时间在破碎的时间* / struct tm tb; / *调整时间* / time_t time_calendar = 0; / *日历时间为长int * / int sec,min,hour,mday,mon,year; long int dt; / * temp variables * / int tm; / *如果CHG_TIME == 0,则无需转换* / if(CHG_TIME == 0) { 返回1; } / *解析时间&日期信息* / sec = 0; min = TIME%100; 小时= TIME / 100; mday = DATE%100; mon =(( DATE%10000)/ 100)-1; / *年必须表示为自1900年以来的年份* / / *如果年份是< 40,我们假设它是'2000'的* / / *如果年份> = 40,我们假设它是在1900年的* / 年=( DATE / 10000); if(year< 40) year = year + 100; / *创建struct tm * / time_broken.tm_sec = sec ; time_broken.tm_min = min; time_broken.tm_hour =小时; time_broken.tm_mday = mday; time_broken.tm_mon = mon; time_broken.tm_year =年份; / *获取自1970年1月1日起的秒数* / time_calendar = mktime(& time_broken); I created a function that breaks down a date into broken down time, I subtract a certain number of seconds from that, then use mktime() to recompute the calendar time. It works basically except every so often, I get the date 060207 (Feb 7, 2006) which is obviously not correct. When it does this it always givesme this date. I tracked it down to my mktime() call, when I get to a certain date,mktime returns a number in 4000000000, when it should return a number in 1000000000. But I am not sure why. The variable in my tm struct all have the correct date, but mktime returns this. It is happening to me when the date I am changing is: 040727 but it hasdone this on others as well. I am thinking that something goes wrong in mktime() to cause it to return some default number. Perhaps something wrong in one of my time_broken members and then it gets past to mktime? Suggestions? Thanks a bunch! Here''s my code: int adjust_time() { struct tm time_broken; /* time in broken time */ struct tm tb; /* adjusted time */ time_t time_calendar=0; /* calendar time as a long int */ int sec,min,hour,mday,mon,year; long int dt; /* temp variables */ int tm; /* if CHG_TIME==0, no conversion necessary */ if(CHG_TIME==0) { return 1; } /* parse out time & date info */ sec=0; min=TIME%100; hour=TIME/100; mday=DATE%100; mon=((DATE%10000)/100)-1; /* year must be represented as years since 1900 */ /* If the year is < 40, we assume it''s in 2000''s */ /* If the year is >= 40, we assume it''s in 1900''s */ year=(DATE/10000); if(year<40) year=year+100; /* create the struct tm */ time_broken.tm_sec=sec; time_broken.tm_min=min; time_broken.tm_hour=hour; time_broken.tm_mday=mday; time_broken.tm_mon=mon; time_broken.tm_year=year; /* get the number of seconds since * Jan 1, 1970 */ time_calendar=mktime(&time_broken); 你永远不会给成员``tm_isdst''''分配一个有意义的值。 mktime因此使用一个不确定的值,结果 未定义行为。 - j You never assign a meaningful value to member ``tm_isdst''''.mktime thus uses an indeterminate value and the consequenceis undefined behaviour. --j John Hanley写道: John Hanley wrote:我创建了一个将日期分解为的函数分解时间,我从那减去一定的秒数,然后使用mktime()来重新计算日历时间。 基本上除了每隔一段时间工作一次,我得到日期060207(2月7日, 2006),这显然是不正确的。当它这样做时它总是给我这个日期。 我跟踪到我的mktime()调用,当我到达某个特定日期时,mktime 返回一个数字在4000000000,它应该返回一个数字在1000000000.但我不知道为什么。我的tm结构中的变量都有正确的日期,但是mktime会返回这个。 当我改变的日期是040727但是它已经完成了这也在其他人身上。 我在想mktime()中出现问题导致它返回某个默认号码。也许我的time_broken 成员之间出了问题,然后它就过去了mktime? 建议?非常感谢! 这是我的代码: I created a function that breaks down a date into broken down time, I subtract a certain number of seconds from that, then use mktime() to recompute the calendar time. It works basically except every so often, I get the date 060207 (Feb 7, 2006) which is obviously not correct. When it does this it always gives me this date. I tracked it down to my mktime() call, when I get to a certain date, mktime returns a number in 4000000000, when it should return a number in 1000000000. But I am not sure why. The variable in my tm struct all have the correct date, but mktime returns this. It is happening to me when the date I am changing is: 040727 but it has done this on others as well. I am thinking that something goes wrong in mktime() to cause it to return some default number. Perhaps something wrong in one of my time_broken members and then it gets past to mktime? Suggestions? Thanks a bunch! Here''s my code: 代码不完整,遵循逻辑并不容易。 但我发现你犯了一些错误。显然,你是 ,假设类型time_t是long类型,表示秒。 标准C没有指明这是事实。标准 仅指定time_t是能够表示时间的的算术时间。并且,它没有在 上指定任何工具。因此,为了便携,代码必须不是假设类型为long类型,值为 秒。为了解决这个问题,标准C提供了函数 ,可以让你操作时间。所以,为了纠正 你的函数adjust_time,你需要将 time_t值转换为细分时间并调整结构 成员tm_sec的秒数。然后调用函数 mktime生成一个新的time_t值。 另一个问题:检查你的返回值。 你没有检查返回值你的mktime函数。 time_t'的日期范围是有限的。 函数mktime将返回(time_t)-1如果它不可用 表示该日期。我不能肯定,但是你得到4000000000范围内的值 可能是因为 函数mktime返回一个(time_t)-1值。 一个例子: #include< stdio.h> #include< time.h> ; #include< limits.h> time_t AdjustTime(time_t tvalue,int secs) { struct tm * tp; time_t ret; if((ret =(tvalue!=(time_t)-1))) { tp = localtime(& tvalue); if(secs> INT_MAX - tp-> tm_sec) ret =(time_t)-1; else { tp-> tm_sec + = secs; tp-> tm_isdst = -1; ret = mktime(tp); } } 返回ret; } int main(无效) { time_t date; struct tm t; / *为25DEC2005 12:00:00生成time_t值* / t.tm_year = 2005-1900; t.tm_mon = 11; t.tm_mday = 25; t.tm_hour = 12; t。 tm_min = t.tm_sec = t.tm_isdst = 0; if((date = mktime(& t))==(time_t)-1) puts(时间不可用); 其他 { printf(" date表示%s" 试图减去60秒\ n,ctime(& date)); if((date = AdjustTime(date,-60))!=( time_t)-1) printf(新日期是%s,ctime(& date)); else puts(" new time for new日期不可用); } 返回0; } - Al Bowers 美国佛罗里达州坦帕市 mailto: xa ** ****@myrapidsys.com (删除x以发送电子邮件) http://www.geocities.com/abowers822/ The code is not complete and it is not easy to follow the logic.But I see you are making some errors. Apparently, you areassuming that type time_t is type long representing seconds.Standard C does not specify this to be fact. The Standardonly specifies that time_t be an arithmetic time capable ofrepresenting time. And, it does not specify anything onits instrumentality. So, to be portable, the code must notassume the type to be type long and the values representingseconds. To get around this, Standard C provides functionsthat will allow you to manipulate time. So, to correctyour function adjust_time, you will need to convert thetime_t value to broken down time and the adjust the structmember tm_sec in the number of seconds. Then call functionmktime to generate a new time_t value.Another problem: check your return values.You did not check the return valueof your mktime function. time_t''s range of dates is limited.Function mktime will return (time_t)-1 should it be uncapableof representing that date. I can''t be sure, but the valuesyou are getting in the range of 4000000000 may be result offunction mktime returning a (time_t)-1 value. An example: #include <stdio.h>#include <time.h>#include <limits.h> time_t AdjustTime(time_t tvalue, int secs){struct tm *tp;time_t ret; if((ret = (tvalue != (time_t)-1))){tp = localtime(&tvalue);if(secs > INT_MAX - tp->tm_sec)ret = (time_t)-1;else{tp->tm_sec+=secs;tp->tm_isdst = -1;ret = mktime(tp);}}return ret;} int main(void){time_t date;struct tm t; /* make a time_t value for 25DEC2005 12:00:00 */t.tm_year = 2005-1900;t.tm_mon = 11;t.tm_mday = 25;t.tm_hour = 12;t.tm_min = t.tm_sec = t.tm_isdst = 0; if((date = mktime(&t)) == (time_t)-1)puts("Time is not available");else{printf("date represents %s""Attemping to subtract 60 secs\n",ctime(&date));if((date = AdjustTime(date, -60)) != (time_t)-1)printf("The new date is %s",ctime(&date));else puts("Time for the new date is unavailable");}return 0;}--Al BowersTampa, Fl USAmailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/ 这篇关于使用mktime()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 21:59