char *T_Str = "2010-6-19 17:40:46";求以上时间格转为日历时间(从一个标准时间点到此时的时间经过的秒数),以及秒数转为以上格式的代码,如下:time_t tval= ?????? 由以上格式转换得到;

解决方案 »

  1.   

    //转化为CTimeCString strTime="2008-7-2  21:59:11";
    COleVariant vtime(strTime);
    vtime.ChangeType(VT_DATE);
    COleDateTime cOleTime=vtime;
    SYSTEMTIME systime;
    VariantTimeToSystemTime(cOleTime, &systime);
    CTime cTimeFromDB(systime);
      

  2.   

    CTime 转换处理我这写都收集了。
    http://blog.csdn.net/leitianjun/archive/2010/05/17/5599441.aspx
      

  3.   

    问题我的程序是在SDK下的,没有使用MCF框架中的CString类,用纯C++代码。
      

  4.   

    http://www.vimer.cn/2009/10/%E6%97%B6%E9%97%B4time_t%E5%92%8Cstring%E6%A0%BC%E5%BC%8F%E4%BA%92%E8%BD%AC.htmlint API_StringToTimeEX(const string &strDateStr,time_t &timeData)
    {
        char *pBeginPos = (char*) strDateStr.c_str();
        char *pPos = strstr(pBeginPos,“-”);
        if(pPos == NULL)
        {
            printf(“strDateStr[%s] err \n”, strDateStr.c_str());
            return -1;
        }
        int iYear = atoi(pBeginPos);
        int iMonth = atoi(pPos + 1);
        pPos = strstr(pPos + 1,“-”);
        if(pPos == NULL)
        {
            printf(“strDateStr[%s] err \n”, strDateStr.c_str());
            return -1;
        }
        int iDay = atoi(pPos + 1);
        int iHour=0;
        int iMin=0;
        int iSec=0;
        pPos = strstr(pPos + 1,” “);
        //为了兼容有些没精确到时分秒的
        if(pPos != NULL)
        {
            iHour=atoi(pPos + 1);
            pPos = strstr(pPos + 1,“:”);
            if(pPos != NULL)
            {
                iMin=atoi(pPos + 1);
                pPos = strstr(pPos + 1,“:”);
                if(pPos != NULL)
                {
                    iSec=atoi(pPos + 1);
                }
            }
        }
        struct tm sourcedate;
        bzero((void*)&sourcedate,sizeof(sourcedate));
        sourcedate.tm_sec = iSec;
        sourcedate.tm_min = iMin;
        sourcedate.tm_hour = iHour;
        sourcedate.tm_mday = iDay;
        sourcedate.tm_mon = iMonth - 1;
        sourcedate.tm_year = iYear - 1900;
        timeData = mktime(&sourcedate);
        return 0;
    }
      

  5.   

    自己搞定了,原来不是很难的,有错的请指出!//------------------------------------------------------------------------------
    time_t MY_StrToDate(char *DateStr)
    {
    tm d;
    sscanf(DateStr,"%d-%d-%d %d:%d:%d",&d.tm_year,&d.tm_mon,&d.tm_mday,&d.tm_hour,&d.tm_min,&d.tm_sec);
    d.tm_year-=1900;

    return mktime(&d);
    }
    //------------------------------------------------------------------------------
    char *MY_DateToStr(char *pBuf,time_t tval)
    {
    struct tm *TM;        
    TM = localtime(&tval);
    sprintf(pBuf,"%d-%d-%d %d:%d:%d",TM->tm_year+1900,TM->tm_mon,TM->tm_mday,TM->tm_hour,TM->tm_min,TM->tm_sec);
    return pBuf;
    }