C语言中有关mktime函数的位置问题

Xpectations 2018-04-20 10:56:46
struct tm* startt;
struct tm* endt;
time_t t = time(NULL);
time_t starttime;
time_t endtime;
startt = localtime(&t);
endt = localtime(&t);
startt->tm_hour = 0;
startt->tm_min = 0;
startt->tm_sec = 0;
starttime = mktime(startt);
endt->tm_hour = 23;
endt->tm_min = 59;
endt->tm_sec = 59;
endtime = mktime(endt);

为什么如上这样写可以运行,而如下这样写就错了呢?

struct tm* startt;
struct tm* endt;
time_t t = time(NULL);
time_t starttime;
time_t endtime;
billing pbilling;
int nIndex = 0;
startt = localtime(&t);
endt = localtime(&t);
startt->tm_hour = 0;
startt->tm_min = 0;
startt->tm_sec = 0;
endt->tm_hour = 23;
endt->tm_min = 59;
endt->tm_sec = 59;
starttime = mktime(startt);
endtime = mktime(endt);
...全文
742 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
AlbertS 2018-04-23
  • 打赏
  • 举报
回复
引用 6 楼 Mixxx5 的回复:
[quote=引用 5 楼 shihengzhen101 的回复:] 是localtime的锅,每次调用都会返回相同的地址,你必须先取得localtime中的结果,然后再调用下一次 看看运行的例子
这里两个t=time(NULL);的值还是一样的吗?为什么地址改变了呢?[/quote] t的地址肯定是一样的,但是t的值取决于运行时间,因为time(NULL)的单位是秒,如果两次调用处于不同的秒数,则t的值不同
Xpectations 2018-04-21
  • 打赏
  • 举报
回复
引用 5 楼 shihengzhen101 的回复:
是localtime的锅,每次调用都会返回相同的地址,你必须先取得localtime中的结果,然后再调用下一次 看看运行的例子
这里两个t=time(NULL);的值还是一样的吗?为什么地址改变了呢?
赵4老师 2018-04-20
  • 打赏
  • 举报
回复
localtime Converts a time value and corrects for the local time zone. struct tm *localtime( const time_t *timer ); Routine Required Header Compatibility localtime <time.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value localtime returns a pointer to the structure result. If the value in timer represents a date before midnight, January 1, 1970, localtime returns NULL. The fields of the structure type tm store the following values, each of which is an int: tm_sec Seconds after minute (0 – 59) tm_min Minutes after hour (0 – 59) tm_hour Hours after midnight (0 – 23) tm_mday Day of month (1 – 31) tm_mon Month (0 – 11; January = 0) tm_year Year (current year minus 1900) tm_wday Day of week (0 – 6; Sunday = 0) tm_yday Day of year (0 – 365; January 1 = 0) tm_isdst Positive value if daylight saving time is in effect; 0 if daylight saving time is not in effect; negative value if status of daylight saving time is unknown. The C run-time library assumes the United States’s rules for implementing the calculation of Daylight Saving Time (DST). Parameter timer Pointer to stored time Remarks The localtime function converts a time stored as a time_t value and stores the result in a structure of type tm. The long value timer represents the seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). This value is usually obtained from the time function. gmtime, mktime, and localtime all use a single statically allocated tm structure for the conversion. Each call to one of these routines destroys the result of the previous call. localtime corrects for the local time zone if the user first sets the global environment variable TZ. When TZ is set, three other environment variables (_timezone, _daylight, and _tzname) are automatically set as well. See _tzset for a description of these variables. TZ is a Microsoft extension and not part of the ANSI standard definition of localtime. Note The target environment should try to determine whether daylight saving time is in effect. Example /* LOCALTIM.C: This program uses time to get the current time * and then uses localtime to convert this time to a structure * representing the local time. The program converts the result * from a 24-hour clock to a 12-hour clock and determines the * proper extension (AM or PM). */ #include <stdio.h> #include <string.h> #include <time.h> void main( void ) { struct tm *newtime; char am_pm[] = "AM"; time_t long_time; time( &long_time ); /* Get time as long integer. */ newtime = localtime( &long_time ); /* Convert to local time. */ if( newtime->tm_hour > 12 ) /* Set up extension. */ strcpy( am_pm, "PM" ); if( newtime->tm_hour > 12 ) /* Convert from 24-hour */ newtime->tm_hour -= 12; /* to 12-hour clock. */ if( newtime->tm_hour == 0 ) /*Set hour to 12 if midnight. */ newtime->tm_hour = 12; printf( "%.19s %s\n", asctime( newtime ), am_pm ); } Output Tue Mar 23 11:28:17 AM Time Management Routines See Also asctime, ctime, _ftime, gmtime, time, _tzset
paschen 2018-04-20
  • 打赏
  • 举报
回复
因为localtime返回的指针是指向一个静态变量,第二次调用会把第一次调用的值覆盖了
永远的追梦人 2018-04-20
  • 打赏
  • 举报
回复
两次localtime返回的指针是一样的,startt和endt是同一结构体的指针
AlbertS 2018-04-20
  • 打赏
  • 举报
回复
是localtime的锅,每次调用都会返回相同的地址,你必须先取得localtime中的结果,然后再调用下一次
看看运行的例子

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧