struct和time_t的计算错误

时间:2016-11-15 09:57:21

标签: c++ c time struct

您好我正在实施一种算法,用于捕获市场数据并根据时间将其存储在数组中。我错了,但我不知道在哪里。此功能在每个市场数据标记上运行。我们的想法是在iCandleTime

给出的每个时间间隔计算OHLC值
//Fucntion to print Market Data Parameters
struct tm * timeinfo;
struct tm sStartTime = {00, 16, 15,15,10,116};
struct tm sEndTime = {00, 16, 16,15,10,116};
struct tm sCandleStartTime = {00, 16, 15,15,10,116};
time_t startTime;
time_t endTime;
time_t candleStartTime;
time_t curTime;
int iCandleTime = 120;
int iTimeInterval = 0;
int iNoCandles = 0;
int iCandleNumber = 0;
static float dNormalOpen = 0, dNormalClose = 0;
static float ohlc[375][4];
int openFlag = 0;
void PrintMarketData(void **args,nsString sPfName)
{
    char sMsg[200];
    nsString sFunctionName = "PrintMarketData";
    nsString sToken;
    sToken = GetPfUserParamValue(sPfName,"CashToken");
    double dLtp=GetDoubleValue(GetParam(sToken, 6));

    curTime = time (NULL);
    //timeinfo = localtime (&curTime);
    startTime = mktime(&sStartTime);
    endTime = mktime(&sEndTime);
    candleStartTime = mktime(&sCandleStartTime);
    iTimeInterval = difftime(endTime, startTime);
    iNoCandles = iTimeInterval / iCandleTime;

    if((difftime(curTime,startTime) >=0) && (difftime(curTime,endTime) < 0) && iCandleNumber<iNoCandles)
    {   
        if((difftime(curTime,candleStartTime) >= 0) && (openFlag == 0)) 
        {
            dNormalOpen = dLtp;
            ohlc[iCandleNumber][0] = dNormalOpen;
            openFlag = 1; //Open is populated in the OHLC Candle
            printf("ST:%s",ctime(&candleStartTime));
            printf(">>> Open:%f CurTime:%s > ST:%s",dNormalOpen,ctime(&curTime),ctime(&candleStartTime));
        }
        else
        {
            printf("Not in open interval...already set");
        }
        if(difftime(curTime,(candleStartTime+iCandleTime)) < 0)
        {
            time_t temp = candleStartTime+iCandleTime;
            dNormalClose = dLtp;
            ohlc[iCandleNumber][3] = dNormalClose;
            printf("### Close:%f CurTime:%s < ET:%s",dNormalClose,ctime(&curTime),ctime(&temp));//incorrect print
        }
        else
        {
            //Log("Not in close interval...already set");
        }
        if((difftime(curTime,(candleStartTime+iCandleTime)) >0) && (openFlag == 1))
        {
            time_t temp = candleStartTime+iCandleTime;
            printf("Temp Time %s",ctime(&candleStartTime)); //correct print
            printf("$$$ Candle No:%d, Open:%f Close:%f Open Time:%s Close Time:%s",iCandleNumber,dNormalOpen,dNormalClose,ctime(&candleStartTime),ctime(&temp)); //incorrect print
            openFlag = 0; //Reset flag for next candle
            iCandleNumber = iCandleNumber + 1; //Increment candle number
            candleStartTime = candleStartTime + iCandleTime;
            printf("Incremented Time %s",ctime(&temp));
        }
    }
    else
    {
        printf("Outside Starttime & Endtime ");
        snprintf(sMsg, 200,"Outside Starttime & Endtime ");
        WriteLog(args,sInfo,sFunctionName,sPfName,sMsg);
    }
}

我在两个地方打印的时间是在一个地方,而在另一个地方是错误的。循环也出错了。我在时间结构和time_t事情上犯了什么错误请帮忙 事先提醒

1 个答案:

答案 0 :(得分:2)

在您调用ctime时,例如:

printf("$$$ Candle No:%d, Open:%f Close:%f Open Time:%s Close Time:%s",
    iCandleNumber,dNormalOpen,dNormalClose,
    ctime(&candleStartTime),ctime(&temp)); //incorrect print

你两次致电ctime。但是,ctime的文档说:“对ctime的调用会修改单个静态分配的缓冲区 ”,因此您只是将一次调用的结果覆盖为{{ 1}}与另一次调用ctime的结果。

先调用它们并将结果复制到您自己的缓冲区中。