我写了一个函数,大致
struct tm when ;
time_t t;
char str1[40],str2[40],strData[40]="20030220";memcpy(str1,strData,4);
when.tmyear = atoi(str1);
...t.tDate = mktime(&when);t.tDate总是-1???

解决方案 »

  1.   

    #include <time.h>
    #include <stdio.h>void main( void )
    {
       struct tm when;
       time_t now, result;
       int    days;   time( &now );
       when = *localtime( &now );
       printf( "Current time is %s\n", asctime( &when ) );
       printf( "How many days to look ahead: " );
       scanf( "%d", &days );   when.tm_mday = when.tm_mday + days;
       if( (result = mktime( &when )) != (time_t)-1 )
          printf( "In %d days the time will be %s\n", 
                  days, asctime( &when ) );
       else
          perror( "mktime failed" );
    }
    mktime()返回值,应该是t,而不是t.tDate
      

  2.   

    不好意思,上面写错了。是这个:struct tm when ;
    time_t t;
    char str1[40],str2[40],strData[40]="20030220";memcpy(str1,strData,4);
    when.tmyear = atoi(str1);
    ...t = mktime(&when);t总是-1就是楼上说的t
    ???
      

  3.   

    可以反方向来处理:
    #define LASTDAY 20030220
    int get;
    TCHAR day[12];
    CTime date;
    int year,month,day;
    date = CTime::GetCurrentTime();
    year = date.GetYear();
    month  = date.GetMonth();
    day = date.GetDay();
    sprintf(day,"%d%02d%02d",year,month,day);
    get = atoi(day);
      

  4.   

    是的,但转换成time_t呢??
      

  5.   

    int year;
    year=0;
    year=atoi(str1);
    when.tmyear = year-1900;
    ...
    t=mktime(&when);
    //这样上面t的值肯定不会为-1啦!
    sprintf(str2,"%04d%02d%02d",1900+t.tm_year,t.tm_mon+1,t.tm_mday);