【问题标题】:Wrong computations for struct and time_tstruct 和 time_t 的计算错误
【发布时间】:2017-03-29 03:48:37
【问题描述】:

您好,我正在实现一种算法,用于捕获市场数据并根据时间将其存储在数组中。我在某些地方出错了,无法弄清楚在哪里。此功能在每个市场数据报价上运行。这个想法是在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 事情上犯了什么错误吗?请帮忙 提前问好

【问题讨论】:

  • 什么是nsString
  • "I'm going wrong somehwere" 肯定是对您的问题的神秘描述。我建议您“搜索某处”以找到问题。或者,使用调试器或使用调试日志文件来更好地了解算法中发生的事情。
  • 阅读documentation for ctime,特别是关于返回值部分的第二句。 (“POSIX 标记此功能已过时并建议使用std::strftime”的位也很相关。)

标签: c++ c time struct


【解决方案1】:

在您对 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 的调用会修改 单个静态分配的缓冲区”,因此您只需用另一个调用 ctime 的结果。

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

【讨论】:

    猜你喜欢
    • 2011-08-02
    • 2012-08-27
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2018-06-04
    相关资源
    最近更新 更多